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
-
#builder(object) ⇒ 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 ⇒ Object
-
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not.
-
#extension ⇒ Proc
Returns the extension of the relation.
-
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
-
#foreign_key ⇒ String
Handles all the logic for figuring out what the foreign_key is for each relations query.
-
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
-
#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_foreign_key ⇒ String
Used for relational many to many only.
-
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
-
#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.
-
#nested_builder(attributes, options) ⇒ NestedBuilder
Gets a relation nested builder associated with the relation this metadata is for.
-
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
-
#setter ⇒ String
Gets the method name used to set this relation.
-
#validate? ⇒ true, false
Are we validating this relation automatically?.
Methods included from Extensions::Hash::Scoping
Methods included from Extensions::Hash::CriteriaHelpers
Constructor Details
#initialize(properties = {}) ⇒ Metadata
Instantiate new metadata for a relation.
151 152 153 |
# File 'lib/mongoid/relations/metadata.rb', line 151 def initialize(properties = {}) merge!(properties) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
Handles two different cases - the first is a convenience for JSON like access to the hash instead of having to call []. The second is a delegation of the “*?” methods to has_key? as a convenience to check for existence of a value.
533 534 535 536 |
# File 'lib/mongoid/relations/metadata.rb', line 533 def method_missing(name, *args) method = name.to_s method =~ /\?/ ? has_key?(method.sub('?', '').to_sym) : self[name] end |
Instance Method Details
#builder(object) ⇒ Builder
Gets a relation builder associated with the relation this metadata is for.
22 23 24 |
# File 'lib/mongoid/relations/metadata.rb', line 22 def builder(object) relation.builder(self, object) end |
#cascade_strategy ⇒ Object
Returns the name of the strategy used for handling dependent relations.
34 35 36 37 38 39 40 41 42 |
# File 'lib/mongoid/relations/metadata.rb', line 34 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.
54 55 56 |
# File 'lib/mongoid/relations/metadata.rb', line 54 def class_name @class_name ||= (self[:class_name] || classify) end |
#constraint ⇒ Object
58 59 60 |
# File 'lib/mongoid/relations/metadata.rb', line 58 def constraint @constraint ||= Constraint.new(self) end |
#embedded? ⇒ true, false
Will determine if the relation is an embedded one or not. Currently only checks against embeds one and many.
71 72 73 |
# File 'lib/mongoid/relations/metadata.rb', line 71 def @embedded ||= (macro == :embeds_one || macro == :embeds_many) end |
#extension ⇒ Proc
Returns the extension of the relation. This can be a proc or module.
83 84 85 |
# File 'lib/mongoid/relations/metadata.rb', line 83 def extension self[:extend] end |
#extension? ⇒ true, false
Tells whether an extension definition exist for this relation.
95 96 97 |
# File 'lib/mongoid/relations/metadata.rb', line 95 def extension? !!extension 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.
114 115 116 |
# File 'lib/mongoid/relations/metadata.rb', line 114 def foreign_key @foreign_key ||= determine_foreign_key end |
#foreign_key_setter ⇒ String
Returns the name of the method used to set the foreign key on a document.
127 128 129 |
# File 'lib/mongoid/relations/metadata.rb', line 127 def foreign_key_setter @foreign_key_setter ||= "#{foreign_key}=" end |
#indexed? ⇒ true, false
Tells whether a foreign key index exists on the relation.
139 140 141 |
# File 'lib/mongoid/relations/metadata.rb', line 139 def indexed? !!self[: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.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mongoid/relations/metadata.rb', line 165 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" << " inverse_setter: #{inverse_setter},\n" << " inverse_type: #{inverse_type || "N/A"},\n" << " inverse_type_setter: #{inverse_type_setter || "N/A"},\n" << " key: #{key},\n" << " macro: #{macro},\n" << " name: #{name},\n" << " polymorphic: #{polymorphic? ? "Yes" : "No"},\n" << " relation: #{relation},\n" << " setter: #{setter}>\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.
193 194 195 196 197 |
# File 'lib/mongoid/relations/metadata.rb', line 193 def inverse(other = nil) return self[:inverse_of] if inverse_of? return self[:as] || lookup_inverse(other) if polymorphic? @inverse ||= (cyclic? ? cyclic_inverse : inverse_relation) 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.
209 210 211 212 213 |
# File 'lib/mongoid/relations/metadata.rb', line 209 def inverse_foreign_key @inverse_foreign_key ||= ( inverse_of ? inverse_of.to_s.singularize : inverse_class_name.underscore ) << relation.foreign_key_suffix end |
#inverse_klass ⇒ Class
Returns the inverse class of the proxied relation.
223 224 225 |
# File 'lib/mongoid/relations/metadata.rb', line 223 def inverse_klass @inverse_klass ||= inverse_class_name.constantize end |
#inverse_setter(other = nil) ⇒ String
Returns the setter for the inverse side of the relation.
237 238 239 |
# File 'lib/mongoid/relations/metadata.rb', line 237 def inverse_setter(other = nil) inverse(other).to_s << "=" end |
#inverse_type ⇒ String
Returns the name of the field in which to store the name of the class for the polymorphic relation.
250 251 252 253 254 255 256 |
# File 'lib/mongoid/relations/metadata.rb', line 250 def inverse_type if relation.stores_foreign_key? && polymorphic? (polymorphic? ? name.to_s : class_name.underscore) << "_type" else return nil end end |
#inverse_type_setter ⇒ String
Gets the setter for the field that sets the type of document on a polymorphic relation.
267 268 269 |
# File 'lib/mongoid/relations/metadata.rb', line 267 def 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.
281 282 283 |
# File 'lib/mongoid/relations/metadata.rb', line 281 def key @key ||= determine_key end |
#klass ⇒ Class
Returns the class of the proxied relation.
293 294 295 |
# File 'lib/mongoid/relations/metadata.rb', line 293 def klass @klass ||= class_name.constantize end |
#macro ⇒ Symbol
Returns the macro for the relation of this metadata.
305 306 307 |
# File 'lib/mongoid/relations/metadata.rb', line 305 def macro relation.macro 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.
321 322 323 |
# File 'lib/mongoid/relations/metadata.rb', line 321 def nested_builder(attributes, ) relation.nested_builder(self, attributes, ) end |
#polymorphic? ⇒ true, false
Returns true if the relation is polymorphic.
333 334 335 |
# File 'lib/mongoid/relations/metadata.rb', line 333 def polymorphic? @polymorphic ||= (!!self[:as] || !!self[:polymorphic]) end |
#setter ⇒ String
Gets the method name used to set this relation.
346 347 348 |
# File 'lib/mongoid/relations/metadata.rb', line 346 def setter @setter ||= "#{name.to_s}=" end |
#validate? ⇒ true, false
Are we validating this relation automatically?
358 359 360 |
# File 'lib/mongoid/relations/metadata.rb', line 358 def validate? self[:validate] != false end |