Module: Mongoid::Fields::Serializable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Internal::Array, Internal::BigDecimal, Internal::Binary, Internal::Boolean, Internal::Date, Internal::DateTime, Internal::Float, Internal::ForeignKeys::Array, Internal::ForeignKeys::Object, Internal::Hash, Internal::Integer, Internal::Localized, Internal::NilClass, Internal::Object, Internal::ObjectId, Internal::Range, Internal::Set, Internal::String, Internal::Symbol, Internal::Time, Internal::TimeWithZone
- Defined in:
- lib/mongoid/fields/serializable.rb
Overview
Defines the behaviour for defined fields in the document.
For people who want to have custom field types in their applications and want control over the serialization process to and from the domain model and MongoDB you will need to include this module in your custom type class. You will also need to define either a #serialize and #deserialize instance method, where previously these were a .set and .get class method respectively.
class MyCustomType
include Mongoid::Fields::Serializable
def deserialize(object)
# Do something to convert it from Mongo to my type.
end
def serialize(object)
# Do something to convert from my type to MongoDB friendly.
end
end
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#default_val ⇒ Object
Set readers for the instance variables.
-
#label ⇒ Object
Set readers for the instance variables.
-
#localize ⇒ Object
Set readers for the instance variables.
-
#name ⇒ Object
Set readers for the instance variables.
-
#options ⇒ Object
Set readers for the instance variables.
Instance Method Summary collapse
-
#constraint ⇒ Constraint
Get the constraint from the metadata once.
-
#deserialize(object) ⇒ Object
Deserialize this field from the type stored in MongoDB to the type defined on the model.
-
#eval_default(doc) ⇒ Object
Evaluate the default value and return it.
-
#foreign_key? ⇒ true, false
Is this field a foreign key?.
-
#localized? ⇒ true, false
Is the field localized or not?.
-
#metadata ⇒ Metadata
Get the metadata for the field if its a foreign key.
-
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?.
-
#resizable? ⇒ false
Can the field vary in size, similar to arrays.
-
#selection(object) ⇒ Object
Convert the provided object to a Mongoid criteria friendly value.
-
#serialize(object) ⇒ Object
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
-
#type ⇒ Class
Get the type of this field - inferred from the class name.
-
#versioned? ⇒ true, false
Is this field included in versioned attributes?.
Instance Attribute Details
#default_val ⇒ Object
Set readers for the instance variables.
38 39 40 |
# File 'lib/mongoid/fields/serializable.rb', line 38 def default_val @default_val end |
#label ⇒ Object
Set readers for the instance variables.
38 39 40 |
# File 'lib/mongoid/fields/serializable.rb', line 38 def label @label end |
#localize ⇒ Object
Set readers for the instance variables.
38 39 40 |
# File 'lib/mongoid/fields/serializable.rb', line 38 def localize @localize end |
#name ⇒ Object
Set readers for the instance variables.
38 39 40 |
# File 'lib/mongoid/fields/serializable.rb', line 38 def name @name end |
#options ⇒ Object
Set readers for the instance variables.
38 39 40 |
# File 'lib/mongoid/fields/serializable.rb', line 38 def @options end |
Instance Method Details
#constraint ⇒ Constraint
Get the constraint from the metadata once.
48 49 50 |
# File 'lib/mongoid/fields/serializable.rb', line 48 def constraint @constraint ||= .constraint end |
#deserialize(object) ⇒ Object
Deserialize this field from the type stored in MongoDB to the type defined on the model
63 |
# File 'lib/mongoid/fields/serializable.rb', line 63 def deserialize(object); object; end |
#eval_default(doc) ⇒ Object
Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.
76 77 78 79 80 81 82 |
# File 'lib/mongoid/fields/serializable.rb', line 76 def eval_default(doc) if fields = Threaded.selection evaluated_default(doc) if included?(fields) else evaluated_default(doc) end end |
#foreign_key? ⇒ true, false
Is this field a foreign key?
92 93 94 |
# File 'lib/mongoid/fields/serializable.rb', line 92 def foreign_key? !![:identity] end |
#localized? ⇒ true, false
Is the field localized or not?
104 105 106 |
# File 'lib/mongoid/fields/serializable.rb', line 104 def localized? !!@localize end |
#metadata ⇒ Metadata
Get the metadata for the field if its a foreign key.
116 117 118 |
# File 'lib/mongoid/fields/serializable.rb', line 116 def @metadata ||= [:metadata] end |
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?
128 129 130 |
# File 'lib/mongoid/fields/serializable.rb', line 128 def object_id_field? @object_id_field ||= (type == BSON::ObjectId) end |
#resizable? ⇒ false
Can the field vary in size, similar to arrays.
140 |
# File 'lib/mongoid/fields/serializable.rb', line 140 def resizable?; false; end |
#selection(object) ⇒ Object
Convert the provided object to a Mongoid criteria friendly value.
165 |
# File 'lib/mongoid/fields/serializable.rb', line 165 def selection(object); object; end |
#serialize(object) ⇒ Object
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
153 |
# File 'lib/mongoid/fields/serializable.rb', line 153 def serialize(object); object; end |
#type ⇒ Class
Get the type of this field - inferred from the class name.
175 176 177 |
# File 'lib/mongoid/fields/serializable.rb', line 175 def type @type ||= [:type] || Object end |
#versioned? ⇒ true, false
Is this field included in versioned attributes?
187 188 189 |
# File 'lib/mongoid/fields/serializable.rb', line 187 def versioned? @versioned ||= ([:versioned].nil? ? true : [:versioned]) end |