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)

Parameters:

  • Relations (Array<Metadata>)

    to eager load

  • Documents (Array<Document>)

    to preload the relations

Since:

  • 4.0.0



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 loadded for the current relation

loader.each_loaded_document { |doc| }

Since:

  • 4.0.0



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

def each_loaded_document
  @metadata.klass.any_in(key => keys_from_docs).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

Returns:

  • (Symbol)

    Key to group by the current documents.

Raises:

  • (NotImplementedError)

Since:

  • 4.0.0



129
130
131
# File 'lib/mongoid/relations/eager/base.rb', line 129

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

Returns:

  • (Hash)

    hash with groupd documents.

Since:

  • 4.0.0



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

def grouped_docs
  @grouped_docs[@metadata.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

Returns:

  • (Array)

    keys, ids

Since:

  • 4.0.0



115
116
117
# File 'lib/mongoid/relations/eager/base.rb', line 115

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:

  • (NotImplementedError)

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

Returns:

  • (Array)

    The list of documents given.

Since:

  • 4.0.0



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)

Parameters:

  • parent`s (ObjectId)

    id

  • element (Document, Array)

    to push into the parent

Since:

  • 4.0.0



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

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.

Returns:

  • (Object)

    The relation metadata object.

Since:

  • 4.0.0



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

def 
  @metadata = @associations.shift
end