Class: Mongoid::Relations::Proxy

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Many, One

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Marshalable

#marshal_dump, #marshal_load

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.

Parameters:

  • name (String, Symbol)

    The name of the method.

  • *args (Array)

    The arguments passed to the method.


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

#baseObject

Returns the value of attribute base


21
22
23
# File 'lib/mongoid/relations/proxy.rb', line 21

def base
  @base
end

#loadedObject

Returns the value of attribute loaded


21
22
23
# File 'lib/mongoid/relations/proxy.rb', line 21

def loaded
  @loaded
end

#metadataObject

Returns the value of attribute metadata


21
22
23
# File 'lib/mongoid/relations/proxy.rb', line 21

def 
  @metadata
end

#targetObject

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.

Examples:

Apply the ordering.

Proxy.apply_ordering(criteria, )

Parameters:

  • criteria (Criteria)

    The criteria to modify.

  • metadata (Metadata)

    The relation metadata.

Returns:

Since:

  • 3.0.6


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.

Examples:

Get the eager load criteria.

Proxy.eager_load(, criteria)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • ids (Array<Object>)

    The ids of the base docs.

Returns:

  • (Criteria)

    The criteria to eager load the relation.

Since:

  • 2.2.0


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.

Examples:

Initialize the proxy.

proxy.init(person, name, )

Parameters:

  • base (Document)

    The base document on the proxy.

  • target (Document, Array<Document>)

    The target of the proxy.

  • metadata (Metadata)

    The relation's metadata.

Yields:

  • (_self)

Yield Parameters:

Since:

  • 2.0.0.rc.1


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

#klassClass

Get the class from the metadata, or return nil if no metadata present.

Examples:

Get the class.

proxy.klass

Returns:

  • (Class)

    The relation class.

Since:

  • 3.0.15


58
59
60
# File 'lib/mongoid/relations/proxy.rb', line 58

def klass
   ? .klass : nil
end

#reset_unloadedObject

Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.

Examples:

Reset the relation criteria.

person.preferences.reset_relation_criteria

Since:

  • 3.0.14


69
70
71
# File 'lib/mongoid/relations/proxy.rb', line 69

def reset_unloaded
  target.reset_unloaded(criteria)
end

#substitutableObject

The default substitutable object for a relation proxy is the clone of the target.

Examples:

Get the substitutable.

proxy.substitutable

Returns:

  • (Object)

    A clone of the target.

Since:

  • 2.1.6


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.

Examples:

Save the current document to a different collection.

model.with(collection: "secondary").save

Save the current document to a different database.

model.with(database: "secondary").save

Save the current document to a different session.

model.with(session: "replica_set").save

Save with a combination of options.

model.with(session: "sharded", database: "secondary").save

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :session (String, Symbol)

    The session name.

Returns:

Since:

  • 3.0.0


110
111
112
113
# File 'lib/mongoid/relations/proxy.rb', line 110

def with(options)
  @persistence_options = options
  self
end