Module: CouchRest::More::Typecast

Included in:
CouchRest::Mixins::Properties
Defined in:
lib/couchrest/typecast.rb

Instance Method Summary collapse

Instance Method Details

#typecast_value(value, klass, init_method) ⇒ Object



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

def typecast_value(value, klass, init_method)
  return nil if value.nil?
  klass = klass.constantize unless klass.is_a?(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
    init_method.is_a?(Proc) ? init_method.call(value) : klass.send(init_method, value)
  end
end