Class: Danica::VariablesHolder::VariablesBuilder

Inherits:
Object
  • Object
show all
Includes:
DSL, Wrapper
Defined in:
lib/danica/variables_holder/variables_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wrapper

wrap_hash, wrap_numeric, wrap_value, #wrap_value

Methods included from DSL

build, #function, register, register_class, register_operator, register_wrapper

Constructor Details

#initialize(attr_names, instance) ⇒ VariablesBuilder

Returns a new instance of VariablesBuilder.



10
11
12
13
# File 'lib/danica/variables_holder/variables_builder.rb', line 10

def initialize(attr_names, instance)
  @instance = instance
  @attr_names = attr_names
end

Instance Attribute Details

#attr_namesObject (readonly)

Returns the value of attribute attr_names.



8
9
10
# File 'lib/danica/variables_holder/variables_builder.rb', line 8

def attr_names
  @attr_names
end

#instanceObject (readonly)

Returns the value of attribute instance.



8
9
10
# File 'lib/danica/variables_holder/variables_builder.rb', line 8

def instance
  @instance
end

Instance Method Details

#buildObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/danica/variables_holder/variables_builder.rb', line 15

def build
  names_hash = attr_names.extract_options!.symbolize_keys

  attr_names.map(&:to_sym).each do |name|
    add_setter(name)
    add_reader(name)
    instance.variables_hash[name] = wrap_value(name)
  end

  names_hash.each do |name, default|
    add_setter(name)
    add_reader(name)
    instance.variables_hash[name] = wrap_value(default)
  end
end