Class: Mongoid::Relations::Proxy

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

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.



116
117
118
# File 'lib/mongoid/relations/proxy.rb', line 116

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

Instance Attribute Details

#baseObject

Returns the value of attribute base.



18
19
20
# File 'lib/mongoid/relations/proxy.rb', line 18

def base
  @base
end

#loadedObject

Returns the value of attribute loaded.



18
19
20
# File 'lib/mongoid/relations/proxy.rb', line 18

def loaded
  @loaded
end

#metadataObject

Returns the value of attribute metadata.



18
19
20
# File 'lib/mongoid/relations/proxy.rb', line 18

def 
  @metadata
end

#targetObject

Returns the value of attribute target.



18
19
20
# File 'lib/mongoid/relations/proxy.rb', line 18

def target
  @target
end

Class Method Details

.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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/mongoid/relations/proxy.rb', line 162

def eager_load_ids(, ids)
  klass, foreign_key = .klass, .foreign_key
  eager_loaded = klass.any_in(foreign_key => ids).entries
  unless eager_loaded.empty?
    eager_loaded.each do |doc|
      base_id = doc.__send__(foreign_key)
      yield(doc, { foreign_key => base_id })
    end
  else
    ids.each do |id|
      IdentityMap.clear_many(klass, { foreign_key => id })
    end
  end
end

Instance Method Details

#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



36
37
38
39
40
# File 'lib/mongoid/relations/proxy.rb', line 36

def init(base, target, )
  @base, @target, @metadata = base, target, 
  yield(self) if block_given?
  extend_proxy(.extension) if .extension?
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



51
52
53
# File 'lib/mongoid/relations/proxy.rb', line 51

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



79
80
81
82
# File 'lib/mongoid/relations/proxy.rb', line 79

def with(options)
  Threaded.set_persistence_options(klass, options)
  self
end