Module: CouchRest::Model::Typecast

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) typecast_value(value, property)

klass, init_method)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/couchrest/model/typecast.rb', line 5

def typecast_value(value, property) # klass, init_method)
  return nil if value.nil?
  klass = property.type_class
  if value.instance_of?(klass) || klass == Object
    if klass == Time && !value.utc?
      value.utc # Ensure Time is always in UTC
    else
      value
    end
  elsif [String, TrueClass, Integer, Float, BigDecimal, DateTime, Time, Date, Class].include?(klass)
    send('typecast_to_'+klass.to_s.downcase, value)
  else
    property.build(value)
  end
end