Module: Mongoid::Fields::Serializable

Extended by:
ActiveSupport::Concern
Included in:
Array, BigDecimal, Binary, Boolean, Date, DateTime, Float, ForeignKeys::Array, ForeignKeys::Object, Hash, Integer, Localized, 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/localized.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, Localized, NilClass, Object, ObjectId, Range, Set, String, Symbol, Time, TimeWithZone

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def default_val
  @default_val
end

#labelObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def label
  @label
end

#localizeObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def localize
  @localize
end

#nameObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def name
  @name
end

#optionsObject

Set readers for the instance variables.



38
39
40
# File 'lib/mongoid/fields/serializable.rb', line 38

def options
  @options
end

Instance Method Details

#constraintConstraint

Get the constraint from the metadata once.

Examples:

Get the constraint.

field.constraint

Returns:

  • (Constraint)

    The relation’s contraint.

Since:

  • 2.1.0



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

Examples:

Deserialize the field.

field.deserialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.1.0



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.

Examples:

Evaluate the default value.

field.eval_default(document)

Parameters:

  • doc (Document)

    The document the field belongs to.

Returns:

  • (Object)

    The serialized default value.

Since:

  • 2.1.8



76
77
78
79
80
81
82
# File 'lib/mongoid/fields/serializable.rb', line 76

def eval_default(doc)
  if default_val.respond_to?(:call)
    serialize(doc.instance_exec(&default_val))
  else
    serialize(default_val.duplicable? ? default_val.dup : default_val)
  end
end

#localized?true, false

Is the field localized or not?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true, false)

    If the field is localized.

Since:

  • 2.3.0



92
93
94
# File 'lib/mongoid/fields/serializable.rb', line 92

def localized?
  !!@localize
end

#metadataMetadata

Get the metadata for the field if its a foreign key.

Examples:

Get the metadata.

field.

Returns:

  • (Metadata)

    The relation metadata.

Since:

  • 2.2.0



104
105
106
# File 'lib/mongoid/fields/serializable.rb', line 104

def 
  @metadata ||= options[:metadata]
end

#object_id_field?true, false

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

  • (true, false)

    If the field is a BSON::ObjectId.

Since:

  • 2.2.0



116
117
118
# File 'lib/mongoid/fields/serializable.rb', line 116

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.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.1.0



131
# File 'lib/mongoid/fields/serializable.rb', line 131

def serialize(object); object; end

#typeClass

Get the type of this field - inferred from the class name.

Examples:

Get the type.

field.type

Returns:

  • (Class)

    The name of the class.

Since:

  • 2.1.0



141
142
143
# File 'lib/mongoid/fields/serializable.rb', line 141

def type
  @type ||= options[:type] || Object
end

#versioned?true, false

Is this field included in versioned attributes?

Examples:

Is the field versioned?

field.versioned?

Returns:

  • (true, false)

    If the field is included in versioning.

Since:

  • 2.1.0



153
154
155
# File 'lib/mongoid/fields/serializable.rb', line 153

def versioned?
  @versioned ||= (options[:versioned].nil? ? true : options[:versioned])
end