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

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

Instance Method Summary collapse

Instance Method Details

#coercing_setter!(name, type, nilify = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/disposable/twin/coercion.rb', line 15

def coercing_setter!(name, type, nilify=false)
 type = type ? (Types::Form::Nil | type) : Types::Form::Nil if nilify

  mod = Module.new do
    define_method("#{name}=") do |value|
      super type.call(value)
    end
  end
  include mod
end

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



9
10
11
12
13
# File 'lib/disposable/twin/coercion.rb', line 9

def property(name, options={}, &block)
  super(name, options, &block).tap do
    coercing_setter!(name, options[:type], options[:nilify]) if options[:type] || options[:nilify]
  end
end