Class: COS::Struct::Base

Inherits:
Object
  • Object
show all
Extended by:
AttrHelper
Defined in:
lib/cos/struct.rb

Direct Known Subclasses

Checkpoint, Config, ResourceOperator, Tree

Defined Under Namespace

Modules: AttrHelper

Instance Method Summary collapse

Methods included from AttrHelper

optional_attrs, required_attrs

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cos/struct.rb', line 24

def initialize(options = {})
  # 意外参数检测
  unless optional_attrs.include?(:SKIP_EXTRA)
    extra_keys = options.keys - required_attrs - optional_attrs
    unless extra_keys.empty?
      raise AttrError, "Unexpected extra keys: #{extra_keys.join(', ')}"
    end
  end

  # 必选参数检测
  required_keys = required_attrs - options.keys
  unless required_keys.empty?
    raise AttrError, "Keys: #{required_keys.join(', ')} is Required"
  end

  # 动态创建实例变量
  (required_attrs + optional_attrs).each do |attr|
    instance_variable_set("@#{attr}", options[attr])
  end
end