Class: Mongoid::Relations::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/relations/constraint.rb

Overview

Note:

Durran: The name of this class is this way to match the metadata getter, and foreign_key was already taken there.

Used for converting foreign key values to the correct type based on the types of ids that the document stores.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ Constraint

Create the new constraint with the metadata.

Examples:

Instantiate the constraint.

Constraint.new(metdata)

Parameters:

  • metadata (Metadata)

    The metadata of the relation.

Since:

  • 2.0.0.rc.7



21
22
23
# File 'lib/mongoid/relations/constraint.rb', line 21

def initialize()
  @metadata = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



11
12
13
# File 'lib/mongoid/relations/constraint.rb', line 11

def 
  @metadata
end

Instance Method Details

#convert(object) ⇒ Object

Convert the supplied object to the appropriate type to set as the foreign key for a relation.

Examples:

Convert the object.

constraint.convert("12345")

Parameters:

  • object (Object)

    The object to convert.

Returns:

  • (Object)

    The object cast to the correct type.

Since:

  • 2.0.0.rc.7



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongoid/relations/constraint.rb', line 36

def convert(object)
  return object if .polymorphic?
  klass, field = .klass, .klass.fields["_id"]
  if klass.using_object_ids?
    BSON::ObjectId.mongoize(object)
  elsif object.is_a?(::Array)
    object.map!{ |obj| field.mongoize(obj) }
  else
    field.mongoize(object)
  end
end