Class: Mongoid::Relations::Metadata
- Defined in:
- lib/mongoid/relations/metadata.rb
Overview
The “Grand Poobah” of information about any relation is this class. It contains everything you could ever possible want to know.
Instance Method Summary collapse
-
#as ⇒ true, false
Returns the as option of the relation.
-
#as? ⇒ true, false
Tells whether an as option exists.
-
#autosave ⇒ true, false
Returns the autosave option of the relation.
-
#autosave? ⇒ true, false
Does the metadata have a autosave option?.
-
#builder(object, loading = false) ⇒ Builder
Gets a relation builder associated with the relation this metadata is for.
-
#cascade_strategy ⇒ Object
Returns the name of the strategy used for handling dependent relations.
-
#class_name ⇒ String
Returns the name of the class that this relation contains.
-
#constraint ⇒ Constraint
Get the foreign key contraint for the metadata.
-
#criteria(object, type = nil) ⇒ Criteria
Get the criteria that is used to query for this metadata’s relation.
-
#cyclic ⇒ true, false
Returns the cyclic option of the relation.
-
#cyclic? ⇒ true, false
Does the metadata have a cyclic option?.
-
#dependent ⇒ Symbol
Returns the dependent option of the relation.
-
#dependent? ⇒ true, false
Does the metadata have a dependent option?.
-
#destructive? ⇒ true, false
Does the relation have a destructive dependent option specified.
-
#eager_load(criteria) ⇒ Criteria
Get the criteria needed to eager load this relation.
-
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not.
-
#extension ⇒ Module
Returns the extension of the relation.
-
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
-
#forced_nil_inverse? ⇒ true, false
Does this metadata have a forced nil inverse_of defined.
-
#foreign_key ⇒ String
Handles all the logic for figuring out what the foreign_key is for each relations query.
-
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
-
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
-
#index ⇒ true, false
Returns the index option of the relation.
-
#indexed? ⇒ true, false
Tells whether a foreign key index exists on the relation.
-
#initialize(properties = {}) ⇒ Metadata
constructor
Instantiate new metadata for a relation.
-
#inspect ⇒ String
Since a lot of the information from the metadata is inferred and not explicitly stored in the hash, the inspection needs to be much more detailed.
-
#inverse(other = nil) ⇒ Symbol
Get the name of the inverse relation if it exists.
-
#inverse_class_name ⇒ true, false
Returns the inverse_class_name option of the relation.
-
#inverse_class_name? ⇒ true, false
Returns the if the inverse class name option exists.
-
#inverse_foreign_key ⇒ String
Used for relational many to many only.
-
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
-
#inverse_metadata(document) ⇒ Metadata
Get the metadata for the inverse relation.
-
#inverse_of ⇒ true, false
Returns the inverse_of option of the relation.
-
#inverse_of? ⇒ true, false
Does the metadata have a inverse_of option?.
-
#inverse_setter(other = nil) ⇒ String
Returns the setter for the inverse side of the relation.
-
#inverse_type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
-
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
-
#key ⇒ String
This returns the key that is to be used to grab the attributes for the relation or the foreign key or id that a referenced relation will use to query for the object.
-
#klass ⇒ Class
Returns the class of the proxied relation.
-
#macro ⇒ Symbol
Returns the macro for the relation of this metadata.
-
#many? ⇒ true, false
Is this metadata representing a one to many or many to many relation?.
-
#name ⇒ Symbol
Get the name associated with this metadata.
-
#name? ⇒ true, false
Is the name defined?.
-
#nested_builder(attributes, options) ⇒ NestedBuilder
Gets a relation nested builder associated with the relation this metadata is for.
-
#order ⇒ Criterion::Complex?
Returns default order for this association.
-
#order? ⇒ true, false
Is a default order set?.
-
#path(document) ⇒ Object
Get the path calculator for the supplied document.
-
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
-
#relation ⇒ Proxy
Get the relation associated with this metadata.
-
#setter ⇒ String
Gets the method name used to set this relation.
-
#type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
-
#type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
-
#validate? ⇒ true, false
Are we validating this relation automatically?.
-
#versioned? ⇒ true, false
Is this relation using Mongoid’s internal versioning system?.
Methods included from Extensions::Hash::Scoping
Methods included from Extensions::Hash::CriteriaHelpers
Constructor Details
Instance Method Details
#as ⇒ true, false
Returns the as option of the relation.
19 20 21 |
# File 'lib/mongoid/relations/metadata.rb', line 19 def as self[:as] end |
#as? ⇒ true, false
Tells whether an as option exists.
31 32 33 |
# File 'lib/mongoid/relations/metadata.rb', line 31 def as? !!as end |
#autosave ⇒ true, false
Returns the autosave option of the relation.
43 44 45 |
# File 'lib/mongoid/relations/metadata.rb', line 43 def autosave self[:autosave] end |
#autosave? ⇒ true, false
Does the metadata have a autosave option?
55 56 57 |
# File 'lib/mongoid/relations/metadata.rb', line 55 def autosave? !!autosave end |
#builder(object, loading = false) ⇒ Builder
Gets a relation builder associated with the relation this metadata is for.
70 71 72 |
# File 'lib/mongoid/relations/metadata.rb', line 70 def builder(object, loading = false) relation.builder(self, object, loading) end |
#cascade_strategy ⇒ Object
Returns the name of the strategy used for handling dependent relations.
82 83 84 85 86 87 88 89 90 |
# File 'lib/mongoid/relations/metadata.rb', line 82 def cascade_strategy if dependent? strategy = %{Mongoid::Relations::Cascading::#{dependent.to_s.classify}} strategy.constantize else return nil end end |
#class_name ⇒ String
Returns the name of the class that this relation contains. If the class_name was provided as an option this will return that, otherwise it will determine the name from the name property.
102 103 104 |
# File 'lib/mongoid/relations/metadata.rb', line 102 def class_name @class_name ||= (self[:class_name] || classify) end |
#constraint ⇒ Constraint
Get the foreign key contraint for the metadata.
114 115 116 |
# File 'lib/mongoid/relations/metadata.rb', line 114 def constraint @constraint ||= Constraint.new(self) end |
#criteria(object, type = nil) ⇒ Criteria
Get the criteria that is used to query for this metadata’s relation.
128 129 130 131 |
# File 'lib/mongoid/relations/metadata.rb', line 128 def criteria(object, type = nil) query = relation.criteria(self, object, type) order ? query.order_by(order) : query end |
#cyclic ⇒ true, false
Returns the cyclic option of the relation.
141 142 143 |
# File 'lib/mongoid/relations/metadata.rb', line 141 def cyclic self[:cyclic] end |
#cyclic? ⇒ true, false
Does the metadata have a cyclic option?
153 154 155 |
# File 'lib/mongoid/relations/metadata.rb', line 153 def cyclic? !!cyclic end |
#dependent ⇒ Symbol
Returns the dependent option of the relation.
165 166 167 |
# File 'lib/mongoid/relations/metadata.rb', line 165 def dependent self[:dependent] end |
#dependent? ⇒ true, false
Does the metadata have a dependent option?
177 178 179 |
# File 'lib/mongoid/relations/metadata.rb', line 177 def dependent? !!dependent end |
#destructive? ⇒ true, false
Does the relation have a destructive dependent option specified. This is true for :dependent => :delete and :dependent => :destroy.
583 584 585 |
# File 'lib/mongoid/relations/metadata.rb', line 583 def destructive? @destructive ||= (dependent == :delete || dependent == :destroy) end |
#eager_load(criteria) ⇒ Criteria
Get the criteria needed to eager load this relation.
191 192 193 |
# File 'lib/mongoid/relations/metadata.rb', line 191 def eager_load(criteria) relation.eager_load(self, criteria.clone) end |
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not. Currently only checks against embeds one and many.
204 205 206 |
# File 'lib/mongoid/relations/metadata.rb', line 204 def @embedded ||= (macro == :embeds_one || macro == :embeds_many) end |
#extension ⇒ Module
Returns the extension of the relation.
216 217 218 |
# File 'lib/mongoid/relations/metadata.rb', line 216 def extension self[:extend] end |
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
228 229 230 |
# File 'lib/mongoid/relations/metadata.rb', line 228 def extension? !!extension end |
#forced_nil_inverse? ⇒ true, false
Does this metadata have a forced nil inverse_of defined. (Used in many to manies)
241 242 243 |
# File 'lib/mongoid/relations/metadata.rb', line 241 def forced_nil_inverse? has_key?(:inverse_of) && inverse_of.nil? end |
#foreign_key ⇒ String
Handles all the logic for figuring out what the foreign_key is for each relations query. The logic is as follows:
-
If the developer defined a custom key, use that.
-
If the relation stores a foreign key, use the class_name_id strategy.
-
If the relation does not store the key, use the inverse_class_name_id strategy.
260 261 262 |
# File 'lib/mongoid/relations/metadata.rb', line 260 def foreign_key @foreign_key ||= determine_foreign_key end |
#foreign_key_check ⇒ String
Get the name of the method to check if the foreign key has changed.
272 273 274 |
# File 'lib/mongoid/relations/metadata.rb', line 272 def foreign_key_check @foreign_key_check ||= "#{foreign_key}_changed?" end |
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
285 286 287 |
# File 'lib/mongoid/relations/metadata.rb', line 285 def foreign_key_setter @foreign_key_setter ||= "#{foreign_key}=" end |
#index ⇒ true, false
Returns the index option of the relation.
297 298 299 |
# File 'lib/mongoid/relations/metadata.rb', line 297 def index self[:index] end |
#indexed? ⇒ true, false
Tells whether a foreign key index exists on the relation.
309 310 311 |
# File 'lib/mongoid/relations/metadata.rb', line 309 def indexed? !!index end |
#inspect ⇒ String
Since a lot of the information from the metadata is inferred and not explicitly stored in the hash, the inspection needs to be much more detailed.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/mongoid/relations/metadata.rb', line 336 def inspect "#<Mongoid::Relations::Metadata\n" << " class_name: #{class_name},\n" << " cyclic: #{cyclic || "No"},\n" << " dependent: #{dependent || "None"},\n" << " inverse_of: #{inverse_of || "N/A"},\n" << " key: #{key},\n" << " macro: #{macro},\n" << " name: #{name},\n" << " order: #{order.inspect || "No"},\n" << " polymorphic: #{polymorphic? || "No"},\n" << " relation: #{relation},\n" << " setter: #{setter},\n" << " versioned: #{versioned? || "No"}>\n" end |
#inverse(other = nil) ⇒ Symbol
Get the name of the inverse relation if it exists. If this is a polymorphic relation then just return the :as option that was defined.
363 364 365 366 367 |
# File 'lib/mongoid/relations/metadata.rb', line 363 def inverse(other = nil) return self[:inverse_of] if has_key?(:inverse_of) return self[:as] || lookup_inverse(other) if polymorphic? @inverse ||= (cyclic? ? cyclic_inverse : inverse_relation) end |
#inverse_class_name ⇒ true, false
Returns the inverse_class_name option of the relation.
377 378 379 |
# File 'lib/mongoid/relations/metadata.rb', line 377 def inverse_class_name self[:inverse_class_name] end |
#inverse_class_name? ⇒ true, false
Returns the if the inverse class name option exists.
389 390 391 |
# File 'lib/mongoid/relations/metadata.rb', line 389 def inverse_class_name? !!inverse_class_name end |
#inverse_foreign_key ⇒ String
Used for relational many to many only. This determines the name of the foreign key field on the inverse side of the relation, since in this case there are keys on both sides.
403 404 405 406 407 |
# File 'lib/mongoid/relations/metadata.rb', line 403 def inverse_foreign_key @inverse_foreign_key ||= ( inverse_of ? inverse_of.to_s.singularize : inverse_class_name.demodulize.underscore ) << relation.foreign_key_suffix end |
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
417 418 419 |
# File 'lib/mongoid/relations/metadata.rb', line 417 def inverse_klass @inverse_klass ||= inverse_class_name.constantize end |
#inverse_metadata(document) ⇒ Metadata
Get the metadata for the inverse relation.
431 432 433 |
# File 'lib/mongoid/relations/metadata.rb', line 431 def (document) document.reflect_on_association(inverse(document)) end |
#inverse_of ⇒ true, false
Returns the inverse_of option of the relation.
443 444 445 |
# File 'lib/mongoid/relations/metadata.rb', line 443 def inverse_of self[:inverse_of] end |
#inverse_of? ⇒ true, false
Does the metadata have a inverse_of option?
455 456 457 |
# File 'lib/mongoid/relations/metadata.rb', line 455 def inverse_of? !!inverse_of end |
#inverse_setter(other = nil) ⇒ String
Returns the setter for the inverse side of the relation.
469 470 471 |
# File 'lib/mongoid/relations/metadata.rb', line 469 def inverse_setter(other = nil) "#{inverse(other)}=" end |
#inverse_type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
482 483 484 485 |
# File 'lib/mongoid/relations/metadata.rb', line 482 def inverse_type @inverse_type ||= relation.stores_foreign_key? && polymorphic? ? "#{name}_type" : nil end |
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
496 497 498 |
# File 'lib/mongoid/relations/metadata.rb', line 496 def inverse_type_setter @inverse_type_setter ||= inverse_type ? "#{inverse_type}=" : nil end |
#key ⇒ String
This returns the key that is to be used to grab the attributes for the relation or the foreign key or id that a referenced relation will use to query for the object.
510 511 512 |
# File 'lib/mongoid/relations/metadata.rb', line 510 def key @key ||= determine_key end |
#klass ⇒ Class
Returns the class of the proxied relation.
522 523 524 |
# File 'lib/mongoid/relations/metadata.rb', line 522 def klass @klass ||= class_name.constantize end |
#macro ⇒ Symbol
Returns the macro for the relation of this metadata.
546 547 548 |
# File 'lib/mongoid/relations/metadata.rb', line 546 def macro relation.macro end |
#many? ⇒ true, false
Is this metadata representing a one to many or many to many relation?
534 535 536 |
# File 'lib/mongoid/relations/metadata.rb', line 534 def many? @many ||= (relation.macro.to_s =~ /many/) end |
#name ⇒ Symbol
Get the name associated with this metadata.
558 559 560 |
# File 'lib/mongoid/relations/metadata.rb', line 558 def name self[:name] end |
#name? ⇒ true, false
Is the name defined?
570 571 572 |
# File 'lib/mongoid/relations/metadata.rb', line 570 def name? !!name end |
#nested_builder(attributes, options) ⇒ NestedBuilder
Gets a relation nested builder associated with the relation this metadata is for. Nested builders are used in conjunction with nested attributes.
599 600 601 |
# File 'lib/mongoid/relations/metadata.rb', line 599 def nested_builder(attributes, ) relation.nested_builder(self, attributes, ) end |
#order ⇒ Criterion::Complex?
Returns default order for this association.
716 717 718 |
# File 'lib/mongoid/relations/metadata.rb', line 716 def order self[:order] end |
#order? ⇒ true, false
Is a default order set?
728 729 730 |
# File 'lib/mongoid/relations/metadata.rb', line 728 def order? !!order end |
#path(document) ⇒ Object
Get the path calculator for the supplied document.
613 614 615 |
# File 'lib/mongoid/relations/metadata.rb', line 613 def path(document) relation.path(document) end |
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
625 626 627 |
# File 'lib/mongoid/relations/metadata.rb', line 625 def polymorphic? @polymorphic ||= (!!self[:as] || !!self[:polymorphic]) end |
#relation ⇒ Proxy
Get the relation associated with this metadata.
637 638 639 |
# File 'lib/mongoid/relations/metadata.rb', line 637 def relation self[:relation] end |
#setter ⇒ String
Gets the method name used to set this relation.
650 651 652 |
# File 'lib/mongoid/relations/metadata.rb', line 650 def setter @setter ||= "#{name.to_s}=" end |
#type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
663 664 665 |
# File 'lib/mongoid/relations/metadata.rb', line 663 def type @type ||= polymorphic? ? "#{as.to_s}_type" : nil end |
#type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
676 677 678 |
# File 'lib/mongoid/relations/metadata.rb', line 676 def type_setter @type_setter ||= type ? "#{type}=" : nil end |
#validate? ⇒ true, false
Are we validating this relation automatically?
688 689 690 691 692 693 694 |
# File 'lib/mongoid/relations/metadata.rb', line 688 def validate? unless self[:validate].nil? self[:validate] else self[:validate] = relation.validation_default end end |
#versioned? ⇒ true, false
Is this relation using Mongoid’s internal versioning system?
704 705 706 |
# File 'lib/mongoid/relations/metadata.rb', line 704 def versioned? !!self[:versioned] end |