Class: Pipeloader::Batch::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeloader/batch/fetcher.rb

Overview

A class-level relationship definition. ‘fetch` gathers `owner`’s sibling group, runs the loader ONCE for all their keys, then distributes each slice into the siblings’ FetcherStates — the configured ‘default` when a key has no value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_class, name, getter, loader, default: nil) ⇒ Fetcher

Returns a new instance of Fetcher.



11
12
13
14
15
16
17
# File 'lib/pipeloader/batch/fetcher.rb', line 11

def initialize(target_class, name, getter, loader, default: nil)
  @target_class = target_class
  @name = name
  @getter = getter
  @loader = loader
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/pipeloader/batch/fetcher.rb', line 9

def default
  @default
end

#getterObject (readonly)

Returns the value of attribute getter.



9
10
11
# File 'lib/pipeloader/batch/fetcher.rb', line 9

def getter
  @getter
end

#loaderObject (readonly)

Returns the value of attribute loader.



9
10
11
# File 'lib/pipeloader/batch/fetcher.rb', line 9

def loader
  @loader
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/pipeloader/batch/fetcher.rb', line 9

def name
  @name
end

#target_classObject (readonly)

Returns the value of attribute target_class.



9
10
11
# File 'lib/pipeloader/batch/fetcher.rb', line 9

def target_class
  @target_class
end

Instance Method Details

#fetch(owner) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/pipeloader/batch/fetcher.rb', line 19

def fetch(owner)
  siblings = owner._pipeloader_batch_context.all(target_class)
  ids = siblings.map { |sibling| sibling.send(getter) }
  results = loader.call(ids, self)
  siblings.each_with_index do |sibling, i|
    value = results[ids[i]]
    sibling._fetcher_state(name).results = value.nil? ? @default : value
  end
end