Module: Disposable::Twin::Coercion::ClassMethods

Defined in:
lib/disposable/twin/coercion.rb

Instance Method Summary collapse

Instance Method Details

#coercing_setter!(name, type) ⇒ Object



12
13
14
15
16
17
# File 'lib/disposable/twin/coercion.rb', line 12

def coercing_setter!(name, type)
  mod = Module.new do
    define_method("#{name}=") { |value| super Virtus::Attribute.build(type).coerce(value) }
  end
  include mod
end

#property(name, options = {}, &block) ⇒ Object



6
7
8
9
10
# File 'lib/disposable/twin/coercion.rb', line 6

def property(name, options={}, &block)
  super(name, options, &block).tap do
    coercing_setter!(name, options[:type])  # define coercing setter after twin.
  end
end