Class: Mongoid::Relations::Proxy
- Inherits:
-
Object
- Object
- Mongoid::Relations::Proxy
- Includes:
- Marshalable, Threaded::Lifecycle
- Defined in:
- lib/mongoid/relations/proxy.rb
Overview
This class is the superclass for all relation proxy objects, and contains common behaviour for all of them.
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#loaded ⇒ Object
Returns the value of attribute loaded.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.apply_ordering(criteria, metadata) ⇒ Criteria
Apply ordering to the criteria if it was defined on the relation.
-
.eager_load_ids(metadata, ids) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
Instance Method Summary collapse
-
#extend_proxies(*extension) ⇒ Object
Allow extension to be an array and extend each module.
-
#init(base, target, metadata) {|_self| ... } ⇒ Object
Convenience for setting the target and the metadata properties since all proxies will need to do this.
-
#klass ⇒ Class
Get the class from the metadata, or return nil if no metadata present.
-
#reset_unloaded ⇒ Object
Resets the criteria inside the relation proxy.
-
#substitutable ⇒ Object
The default substitutable object for a relation proxy is the clone of the target.
-
#with(options) ⇒ Document
Tell the next persistance operation to store in a specific collection, database or session.
Methods included from Marshalable
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
Default behavior of method missing should be to delegate all calls to the target of the proxy. This can be overridden in special cases.
148 149 150 |
# File 'lib/mongoid/relations/proxy.rb', line 148 def method_missing(name, *args, &block) target.send(name, *args, &block) end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
21 22 23 |
# File 'lib/mongoid/relations/proxy.rb', line 21 def base @base end |
#loaded ⇒ Object
Returns the value of attribute loaded.
21 22 23 |
# File 'lib/mongoid/relations/proxy.rb', line 21 def loaded @loaded end |
#metadata ⇒ Object
Returns the value of attribute metadata.
21 22 23 |
# File 'lib/mongoid/relations/proxy.rb', line 21 def @metadata end |
#target ⇒ Object
Returns the value of attribute target.
21 22 23 |
# File 'lib/mongoid/relations/proxy.rb', line 21 def target @target end |
Class Method Details
.apply_ordering(criteria, metadata) ⇒ Criteria
Apply ordering to the criteria if it was defined on the relation.
236 237 238 |
# File 'lib/mongoid/relations/proxy.rb', line 236 def apply_ordering(criteria, ) .order ? criteria.order_by(.order) : criteria end |
.eager_load_ids(metadata, ids) ⇒ Criteria
Get the criteria that is used to eager load a relation of this type.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/mongoid/relations/proxy.rb', line 252 def eager_load_ids(, ids) klass, foreign_key = .klass, .foreign_key eager_loaded = klass.any_in(foreign_key => ids).entries ids.each do |id| sel = { foreign_key => id } sel.merge!(.type_relation) if klass.hereditary? IdentityMap.clear_many(klass, sel) end eager_loaded.each do |doc| base_id = doc.__send__(foreign_key) sel = { foreign_key => base_id } sel.merge!(.type_relation) if klass.hereditary? yield(doc, sel) end end |
Instance Method Details
#extend_proxies(*extension) ⇒ Object
Allow extension to be an array and extend each module
46 47 48 |
# File 'lib/mongoid/relations/proxy.rb', line 46 def extend_proxies(*extension) extension.flatten.each {|ext| extend_proxy(ext) } end |
#init(base, target, metadata) {|_self| ... } ⇒ Object
Convenience for setting the target and the metadata properties since all proxies will need to do this.
39 40 41 42 43 |
# File 'lib/mongoid/relations/proxy.rb', line 39 def init(base, target, ) @base, @target, @metadata = base, target, yield(self) if block_given? extend_proxies(.extension) if .extension? end |
#klass ⇒ Class
Get the class from the metadata, or return nil if no metadata present.
58 59 60 |
# File 'lib/mongoid/relations/proxy.rb', line 58 def klass ? .klass : nil end |
#reset_unloaded ⇒ Object
Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.
69 70 71 |
# File 'lib/mongoid/relations/proxy.rb', line 69 def reset_unloaded target.reset_unloaded(criteria) end |
#substitutable ⇒ Object
The default substitutable object for a relation proxy is the clone of the target.
82 83 84 |
# File 'lib/mongoid/relations/proxy.rb', line 82 def substitutable target end |
#with(options) ⇒ Document
Tell the next persistance operation to store in a specific collection, database or session.
110 111 112 113 |
# File 'lib/mongoid/relations/proxy.rb', line 110 def with() @persistence_options = self end |