Module: CouchRest::Mixins::Typecast

Included in:
Property
Defined in:
lib/couchrest/mixins/typecast.rb

Instance Method Summary collapse

Instance Method Details

#typecast_value(value, property) ⇒ Object

klass, init_method)



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couchrest/mixins/typecast.rb', line 28

def typecast_value(value, property) # klass, init_method)
  return nil if value.nil?
  klass = property.type_class
  if value.instance_of?(klass) || klass == Object
    value
  elsif [String, TrueClass, Integer, Float, BigDecimal, DateTime, Time, Date, Class].include?(klass)
    send('typecast_to_'+klass.to_s.downcase, value)
  else
    # Allow the init_method to be defined as a Proc for advanced conversion
    property.init_method.is_a?(Proc) ? property.init_method.call(value) : klass.send(property.init_method, value)
  end
end