Module: Representable::Coercion::ClassMethods

Defined in:
lib/representable/coercion.rb

Instance Method Summary collapse

Instance Method Details

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/representable/coercion.rb', line 25

def property(name, options={})
  return super unless options[:type]

  representable_attrs.inheritable_array(:coercer_class).first.attribute(name, options[:type])

  # By using :getter we "pre-occupy" this directive, but we avoid creating accessors, which i find is the cleaner way.
  options[:exec_context] = :decorator
  options[:getter] = lambda { |*| coercer.coerce(name, represented.send(name)) }
  options[:setter] = lambda { |v,*| represented.send("#{name}=", coercer.coerce(name, v)) }

  super
end