Module: Mongoid::Fields::Serializable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Array, BigDecimal, Binary, Boolean, Date, DateTime, Float, ForeignKeys::Array, ForeignKeys::Object, Hash, Integer, NilClass, Object, ObjectId, Range, Set, String, Symbol, Time, TimeWithZone
- Defined in:
- lib/mongoid/fields/serializable.rb,
lib/mongoid/fields/serializable/set.rb,
lib/mongoid/fields/serializable/date.rb,
lib/mongoid/fields/serializable/hash.rb,
lib/mongoid/fields/serializable/time.rb,
lib/mongoid/fields/serializable/array.rb,
lib/mongoid/fields/serializable/float.rb,
lib/mongoid/fields/serializable/range.rb,
lib/mongoid/fields/serializable/bignum.rb,
lib/mongoid/fields/serializable/binary.rb,
lib/mongoid/fields/serializable/fixnum.rb,
lib/mongoid/fields/serializable/object.rb,
lib/mongoid/fields/serializable/string.rb,
lib/mongoid/fields/serializable/symbol.rb,
lib/mongoid/fields/serializable/boolean.rb,
lib/mongoid/fields/serializable/integer.rb,
lib/mongoid/fields/serializable/date_time.rb,
lib/mongoid/fields/serializable/nil_class.rb,
lib/mongoid/fields/serializable/object_id.rb,
lib/mongoid/fields/serializable/big_decimal.rb,
lib/mongoid/fields/serializable/timekeeping.rb,
lib/mongoid/fields/serializable/time_with_zone.rb,
lib/mongoid/fields/serializable/foreign_keys/array.rb,
lib/mongoid/fields/serializable/foreign_keys/object.rb
Overview
:nodoc:
Defined Under Namespace
Modules: ClassMethods, ForeignKeys, Timekeeping Classes: Array, BigDecimal, Bignum, Binary, Boolean, Date, DateTime, Fixnum, Float, Hash, Integer, NilClass, Object, ObjectId, Range, Set, String, Symbol, Time, TimeWithZone
Instance Attribute Summary collapse
-
#default ⇒ Object
Set readers for the instance variables.
-
#label ⇒ 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
-
#cast_on_read? ⇒ true, false
When reading the field do we need to cast the value? This holds true when times are stored or for big decimals which are stored as strings.
-
#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.
-
#metadata ⇒ Metadata
Get the metadata for the field if its a foreign key.
-
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?.
-
#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 ⇒ Object
Set readers for the instance variables.
29 30 31 |
# File 'lib/mongoid/fields/serializable.rb', line 29 def default @default end |
#label ⇒ Object
Set readers for the instance variables.
29 30 31 |
# File 'lib/mongoid/fields/serializable.rb', line 29 def label @label end |
#name ⇒ Object
Set readers for the instance variables.
29 30 31 |
# File 'lib/mongoid/fields/serializable.rb', line 29 def name @name end |
#options ⇒ Object
Set readers for the instance variables.
29 30 31 |
# File 'lib/mongoid/fields/serializable.rb', line 29 def @options end |
Instance Method Details
#cast_on_read? ⇒ true, false
When reading the field do we need to cast the value? This holds true when times are stored or for big decimals which are stored as strings.
40 41 42 43 44 45 46 |
# File 'lib/mongoid/fields/serializable.rb', line 40 def cast_on_read? return @cast_on_read if defined?(@cast_on_read) @cast_on_read = self.class.public_instance_methods(false).map do |m| m.to_sym end.include?(:deserialize) end |
#constraint ⇒ Constraint
Get the constraint from the metadata once.
56 57 58 |
# File 'lib/mongoid/fields/serializable.rb', line 56 def constraint @constraint ||= .constraint end |
#deserialize(object) ⇒ Object
Deserialize this field from the type stored in MongoDB to the type defined on the model
71 |
# File 'lib/mongoid/fields/serializable.rb', line 71 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.
84 85 86 87 88 89 90 |
# File 'lib/mongoid/fields/serializable.rb', line 84 def eval_default(doc) if default.respond_to?(:call) serialize(doc.instance_exec(&default)) else serialize(default.duplicable? ? default.dup : default) end end |
#metadata ⇒ Metadata
Get the metadata for the field if its a foreign key.
100 101 102 |
# File 'lib/mongoid/fields/serializable.rb', line 100 def @metadata ||= [:metadata] end |
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?
112 113 114 |
# File 'lib/mongoid/fields/serializable.rb', line 112 def object_id_field? @object_id_field ||= (type == BSON::ObjectId) end |
#serialize(object) ⇒ Object
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
127 |
# File 'lib/mongoid/fields/serializable.rb', line 127 def serialize(object); object; end |
#type ⇒ Class
Get the type of this field - inferred from the class name.
137 138 139 |
# File 'lib/mongoid/fields/serializable.rb', line 137 def type @type ||= [:type] || Object end |
#versioned? ⇒ true, false
Is this field included in versioned attributes?
149 150 151 |
# File 'lib/mongoid/fields/serializable.rb', line 149 def versioned? @versioned ||= ([:versioned].nil? ? true : [:versioned]) end |