Module: Kiva::DynamicInitializer::ClassMethods

Defined in:
lib/ruby-kiva/dynamic_initializer.rb

Instance Method Summary collapse

Instance Method Details

#typed_attr_accessor(name, klass, factory_method = :new, array = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/ruby-kiva/dynamic_initializer.rb', line 4

def typed_attr_accessor(name, klass, factory_method = :new, array = false)
   define_method("#{name}=") do |value|
    if array
      typed = value.map() {|item| klass.send(factory_method, item)}
    else
      typed = klass.send(factory_method, value)
    end
    instance_variable_set("@#{name}", typed)
  end
  attr_reader name
end