This module provides the extra behaviour for including relations in JSON and XML serialization.
Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.
@example Get the serializable hash.
document.serializable_hash
@example Get the serializable hash with options.
document.serializable_hash(:include => :addresses)
@param [ Hash ] options The options to pass.
@option options [ Symbol ] :include What relations to include. @option options [ Symbol ] :only Limit the fields to only these. @option options [ Symbol ] :except Dont include these fields. @option options [ Symbol ] :methods What methods to include.
@return [ Hash ] The document, ready to be serialized.
@since 2.0.0.rc.6
# File lib/mongoid/serialization.rb, line 27 def serializable_hash(options = nil) options ||= {} only = Array.wrap(options[:only]).map(&:to_s) except = Array.wrap(options[:except]).map(&:to_s) except |= ['_type'] field_names = fields.keys.map { |field| field.to_s } attribute_names = (attributes.keys + field_names).sort if only.any? attribute_names &= only elsif except.any? attribute_names -= except end method_names = Array.wrap(options[:methods]).map { |n| n.to_s if respond_to?(n.to_s) }.compact Hash[(attribute_names + method_names).map { |n| [n, send(n)] }].tap do |attrs| serialize_relations(attrs, options) if options[:include] end end
Serialize the provided object into a Mongo friendly value, using the field serialization method for the passed in type. If no type is given then we assume generic object serialization, which just returns the value itself.
@example Mongoize the object.
Mongoid::Serialization.mongoize(time, Time)
@param [ Object ] object The object to convert. @param [ Class ] klass The type of the object.
@return [ Object ] The converted object.
@since 2.1.0
# File lib/mongoid/serialization.rb, line 65 def mongoize(object, klass = Object) Fields::Mappings.for(klass).instantiate(:mongoize).serialize(object) end
Generated with the Darkfish Rdoc Generator 2.