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.



149
150
151
# File 'lib/mongoid/relations/proxy.rb', line 149

def method_missing(name, *args, &block)
  target.send(name, *args, &block)
end

Instance Attribute Details

#__metadataObject Also known as: relation_metadata

Returns the value of attribute __metadata



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

def 
  @__metadata
end

#baseObject

Returns the value of attribute base



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

def base
  @base
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



238
239
240
# File 'lib/mongoid/relations/proxy.rb', line 238

def apply_ordering(criteria, )
  .order ? criteria.order_by(.order) : criteria
end

Instance Method Details

#extend_proxies(*extension) ⇒ Object

Allow extension to be an array and extend each module



47
48
49
# File 'lib/mongoid/relations/proxy.rb', line 47

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



40
41
42
43
44
# File 'lib/mongoid/relations/proxy.rb', line 40

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



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

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



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

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



83
84
85
# File 'lib/mongoid/relations/proxy.rb', line 83

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



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

def with(options)
  @persistence_options = options
  self
end