Class: Mongoid::Relations::Eager::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/relations/eager/base.rb

Overview

Base class for eager load preload functions.

Since:

  • 4.0.0

Direct Known Subclasses

BelongsTo, HasAndBelongsToMany, HasMany, HasOne

Instance Method Summary collapse

Constructor Details

#initialize(associations, docs) ⇒ Base

Instantiate the eager load class.

Examples:

Create the new belongs to eager load preloader.

BelongsTo.new(, parent_docs)

Since:

  • 4.0.0

Parameters:

  • to eager load

  • to preload the relations



21
22
23
24
25
# File 'lib/mongoid/relations/eager/base.rb', line 21

def initialize(associations, docs)
  @associations = associations
  @docs = docs
  @grouped_docs = {}
end

Instance Method Details

#each_loaded_documentObject

Run the preloader.

Examples:

Iterate over the documents loaded for the current relation

loader.each_loaded_document { |doc| }

Since:

  • 4.0.0



72
73
74
75
76
77
78
# File 'lib/mongoid/relations/eager/base.rb', line 72

def each_loaded_document
  criteria = .klass.any_in(key => keys_from_docs)
  criteria.inclusions = criteria.inclusions - [  ]
  criteria.each do |doc|
    yield doc
  end
end

#group_by_keySymbol

Return the key to group the current documents.

This method should be implemented in the subclass

Examples:

Return the key for group

loader.group_by_key

Raises:

Since:

  • 4.0.0

Returns:

  • Key to group by the current documents.



131
132
133
# File 'lib/mongoid/relations/eager/base.rb', line 131

def group_by_key
  raise NotImplementedError
end

#grouped_docsHash

Return a hash with the current documents grouped by key.

Examples:

Return a hash with the current documents grouped by key.

loader.grouped_docs

Since:

  • 4.0.0

Returns:

  • hash with groupd documents.



103
104
105
106
107
# File 'lib/mongoid/relations/eager/base.rb', line 103

def grouped_docs
  @grouped_docs[.name] ||= @docs.group_by do |doc|
    doc.send(group_by_key)
  end
end

#keys_from_docsArray

Group the documents and return the keys

Examples:

loader.keys_from_docs

Since:

  • 4.0.0

Returns:

  • keys, ids



117
118
119
# File 'lib/mongoid/relations/eager/base.rb', line 117

def keys_from_docs
  grouped_docs.keys
end

#preloadObject

Preload the current relation.

This method should be implemented in the subclass

Examples:

Preload the current relation into the documents.

loader.preload

Raises:

Since:

  • 4.0.0



62
63
64
# File 'lib/mongoid/relations/eager/base.rb', line 62

def preload
  raise NotImplementedError
end

#runArray

Run the preloader.

Examples:

Preload the relations into the documents.

loader.run

Since:

  • 4.0.0

Returns:

  • The list of documents given.



47
48
49
50
51
52
# File 'lib/mongoid/relations/eager/base.rb', line 47

def run
  while 
    preload
  end
  @docs
end

#set_on_parent(id, element) ⇒ Object

Set the pre-loaded document into its parent.

Examples:

Set docs into parent with pk = "foo"

loader.set_on_parent("foo", docs)

Since:

  • 4.0.0

Parameters:

  • id

  • to push into the parent



89
90
91
92
93
# File 'lib/mongoid/relations/eager/base.rb', line 89

def set_on_parent(id, element)
  grouped_docs[id].each do |d|
    set_relation(d, element)
  end
end

#shift_metadataObject

Shift the current relation metadata

Examples:

Shift the current metadata.

loader.

Since:

  • 4.0.0

Returns:

  • The relation metadata object.



35
36
37
# File 'lib/mongoid/relations/eager/base.rb', line 35

def 
   = @associations.shift
end