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

Inherits:
Object
  • Object
show all
Defined in:
build/mongoid-6.4/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:

  • associations (Array<Metadata>)

    Relations to eager load

  • docs (Array<Document>)

    Documents to preload the relations

Since:

  • 4.0.0



21
22
23
24
25
# File 'build/mongoid-6.4/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



74
75
76
77
78
79
80
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 74

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

Returns:

  • (Symbol)

    Key to group by the current documents.

Raises:

  • (NotImplementedError)

Since:

  • 4.0.0



133
134
135
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 133

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 grouped documents.

Since:

  • 4.0.0



105
106
107
108
109
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 105

def grouped_docs
  @grouped_docs[@metadata.name] ||= @docs.group_by do |doc|
    doc.send(group_by_key) if doc.respond_to?(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



119
120
121
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 119

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



64
65
66
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 64

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
53
54
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 47

def run
  @loaded = []
  while 
    preload
    @loaded << @docs.collect { |d| d.send(@metadata.name) if d.respond_to?(@metadata.name) }
  end
  @loaded.flatten
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:

  • id (ObjectId)

    parent`s id

  • element (Document, Array)

    to push into the parent

Since:

  • 4.0.0



91
92
93
94
95
# File 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 91

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 'build/mongoid-6.4/lib/mongoid/relations/eager/base.rb', line 35

def 
  @metadata = @associations.shift
end