Class: Mongoid::Relations::Referenced::In

Inherits:
One show all
Defined in:
lib/mongoid/relations/referenced/in.rb

Overview

This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is store on this side of the relation and the reference is to document(s) in another collection.

Instance Attribute Summary

Attributes inherited from Proxy

#base, #loaded, #metadata, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

#load!

Methods inherited from Proxy

#init

Constructor Details

#initialize(base, target, metadata) ⇒ In

Instantiate a new referenced_in relation.

Examples:

Create the new relation.

Referenced::In.new(game, person, )

Parameters:

  • base (Document)

    The document this relation hangs off of.

  • target (Document, Array<Document>)

    The target (parent) of the relation.

  • metadata (Metadata)

    The relation’s metadata.



42
43
44
45
46
# File 'lib/mongoid/relations/referenced/in.rb', line 42

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy

Class Method Details

.builder(meta, object) ⇒ Builder

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Referenced::In.builder(meta, object)

Parameters:

  • meta (Metadata)

    The metadata of the relation.

  • object (Document, Hash)

    A document or attributes to build with.

Returns:

  • (Builder)

    A new builder object.

Since:

  • 2.0.0.rc.1



122
123
124
# File 'lib/mongoid/relations/referenced/in.rb', line 122

def builder(meta, object)
  Builders::Referenced::In.new(meta, object)
end

.embedded?false

Returns true if the relation is an embedded one. In this case always false.

Examples:

Is this relation embedded?

Referenced::In.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



135
136
137
# File 'lib/mongoid/relations/referenced/in.rb', line 135

def embedded?
  false
end

.foreign_key_defaultnil

Get the default value for the foreign key.

Examples:

Get the default.

Referenced::In.foreign_key_default

Returns:

  • (nil)

    Always nil.

Since:

  • 2.0.0.rc.1



147
148
149
# File 'lib/mongoid/relations/referenced/in.rb', line 147

def foreign_key_default
  nil
end

.foreign_key_suffixString

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::In.foreign_key_suffix

Returns:

Since:

  • 2.0.0.rc.1



159
160
161
# File 'lib/mongoid/relations/referenced/in.rb', line 159

def foreign_key_suffix
  "_id"
end

.macroSymbol

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Referenced::In.macro

Returns:



170
171
172
# File 'lib/mongoid/relations/referenced/in.rb', line 170

def macro
  :referenced_in
end

.nested_builder(metadata, attributes, options) ⇒ NestedBuilder

Return the nested builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the nested builder.

Referenced::In.builder(attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes to build with.

  • options (Hash)

    The options for the builder.

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Returns:

  • (NestedBuilder)

    A newly instantiated nested builder object.

Since:

  • 2.0.0.rc.1



196
197
198
# File 'lib/mongoid/relations/referenced/in.rb', line 196

def nested_builder(, attributes, options)
  Builders::NestedAttributes::One.new(, attributes, options)
end

.stores_foreign_key?true

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Referenced::In.stores_foreign_key?

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0.rc.1



209
210
211
# File 'lib/mongoid/relations/referenced/in.rb', line 209

def stores_foreign_key?
  true
end

Instance Method Details

#bind(options = {}) ⇒ Object

Binds the base object to the inverse of the relation. This is so we are referenced to the actual objects themselves and dont hit the database twice when setting the relations up.

This is called after first creating the relation, or if a new object is set on the relation.

Examples:

Bind the relation.

game.person.bind

Parameters:

  • options (Hash) (defaults to: {})

    The options to bind with.

Options Hash (options):

  • :binding (true, false)

    Are we in build mode?

  • :continue (true, false)

    Continue binding the inverse?

Since:

  • 2.0.0.rc.1



29
30
31
# File 'lib/mongoid/relations/referenced/in.rb', line 29

def bind(options = {})
  binding.bind(options)
end

#substitute(new_target, options = {}) ⇒ In?

Substitutes the supplied target documents for the existing document in the relation.

Examples:

Substitute the relation.

name.substitute(new_name)

Parameters:

  • new_target (Document, Array<Document>)

    The replacement.

  • building (true, false)

    Are we in build mode?

Returns:

  • (In, nil)

    The relation or nil.

Since:

  • 2.0.0.rc.1



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid/relations/referenced/in.rb', line 60

def substitute(new_target, options = {})
  old_target = target
  tap do |relation|
    relation.target = new_target
    if new_target
      bind(options)
    else
      unbind(old_target, options)
      nil
    end
  end
end

#unbind(old_target, options = {}) ⇒ Object

Unbinds the base object to the inverse of the relation. This occurs when setting a side of the relation to nil.

Examples:

Unbind the relation.

game.person.unbind

Parameters:

  • old_target (Document, Array<Document>)

    The previous target.

  • options (Hash) (defaults to: {})

    The options to bind with.

Options Hash (options):

  • :binding (true, false)

    Are we in build mode?

  • :continue (true, false)

    Continue binding the inverse?

Since:

  • 2.0.0.rc.1



87
88
89
# File 'lib/mongoid/relations/referenced/in.rb', line 87

def unbind(old_target, options = {})
  binding(old_target).unbind(options)
end