Class: Mongoid::Relations::Constraint
- Inherits:
-
Object
- Object
- Mongoid::Relations::Constraint
- 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
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#convert(object) ⇒ Object
Convert the supplied object to the appropriate type to set as the foreign key for a relation.
-
#initialize(metadata) ⇒ Constraint
constructor
Create the new constraint with the metadata.
Constructor Details
#initialize(metadata) ⇒ Constraint
Create the new constraint with the metadata.
21 22 23 |
# File 'lib/mongoid/relations/constraint.rb', line 21 def initialize() @metadata = end |
Instance Attribute Details
#metadata ⇒ Object (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.
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? Moped::BSON::ObjectId.mongoize(object) elsif object.is_a?(::Array) object.map!{ |obj| field.mongoize(obj) } else field.mongoize(object) end end |