Module: Mongoid::Serialization
- Extended by:
- ActiveSupport::Concern
- Included in:
- Components
- Defined in:
- lib/mongoid/serialization.rb
Overview
This module provides the extra behaviour for including relations in JSON and XML serialization.
Class Method Summary collapse
-
.mongoize(object, klass = Object) ⇒ Object
Serialize the provided object into a Mongo friendly value, using the field serialization method for the passed in type.
Instance Method Summary collapse
-
#serializable_hash(options = nil) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.
Class Method Details
.mongoize(object, klass = Object) ⇒ Object
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.
66 67 68 |
# File 'lib/mongoid/serialization.rb', line 66 def mongoize(object, klass = Object) Fields::Mappings.for(klass).instantiate(:mongoize).serialize(object) end |
Instance Method Details
#serializable_hash(options = nil) ⇒ Hash
Gets the document as a serializable hash, used by ActiveModel’s JSON serializer.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mongoid/serialization.rb', line 28 def serializable_hash( = nil) ||= {} only = Array.wrap([:only]).map(&:to_s) except = Array.wrap([: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([: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, ) if [:include] end end |