Module: Mongoid::MapReduce::Serialization

Included in:
Formula::AggregateFields, Formula::ArrayValues
Defined in:
lib/mongoid/mapreduce/serialization.rb

Instance Method Summary collapse

Instance Method Details

#serialize(obj, klass) ⇒ Object

Serialize an object to the specified class

obj - Object to serialize klass - Class to prefer

Returns serialized object or nil



11
12
13
14
15
16
# File 'lib/mongoid/mapreduce/serialization.rb', line 11

def serialize(obj, klass)
  return nil if obj.blank?
  obj = obj.is_a?(Boolean) ? (obj ? 1 : 0) : obj
  obj = obj.to_s =~ /(^[-+]?[0-9]+$)|(\.0+)$/ ? Integer(obj) : Float(obj)
  Mongoid::Fields::Mappings.for(klass).allocate.serialize(obj)
end