class Mongoid::Relations::Constraint
Used for converting foreign key values to the correct type based on the types of ids that the document stores.
@note Durran: The name of this class is this way to match the metadata
getter, and foreign_key was already taken there.
Attributes
metadata[R]
Public Class Methods
new(metadata)
click to toggle source
Create the new constraint with the metadata.
@example Instantiate the constraint.
Constraint.new(metdata)
@param [ Metadata ] metadata The metadata of the relation.
@since 2.0.0.rc.7
# File lib/mongoid/relations/constraint.rb, line 20 def initialize(metadata) @metadata = metadata end
Public Instance Methods
convert(object)
click to toggle source
Convert the supplied object to the appropriate type to set as the foreign key for a relation.
@example Convert the object.
constraint.convert("12345")
@param [ Object ] object The object to convert.
@return [ Object ] The object cast to the correct type.
@since 2.0.0.rc.7
# File lib/mongoid/relations/constraint.rb, line 35 def convert(object) return object if metadata.polymorphic? klass, field = metadata.klass, metadata.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