Class: Pipeloader::Batch::Fetcher
- Inherits:
-
Object
- Object
- Pipeloader::Batch::Fetcher
- 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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#getter ⇒ Object
readonly
Returns the value of attribute getter.
-
#loader ⇒ Object
readonly
Returns the value of attribute loader.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#target_class ⇒ Object
readonly
Returns the value of attribute target_class.
Instance Method Summary collapse
- #fetch(owner) ⇒ Object
-
#initialize(target_class, name, getter, loader, default: nil) ⇒ Fetcher
constructor
A new instance of Fetcher.
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
#default ⇒ Object (readonly)
Returns the value of attribute default.
9 10 11 |
# File 'lib/pipeloader/batch/fetcher.rb', line 9 def default @default end |
#getter ⇒ Object (readonly)
Returns the value of attribute getter.
9 10 11 |
# File 'lib/pipeloader/batch/fetcher.rb', line 9 def getter @getter end |
#loader ⇒ Object (readonly)
Returns the value of attribute loader.
9 10 11 |
# File 'lib/pipeloader/batch/fetcher.rb', line 9 def loader @loader end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/pipeloader/batch/fetcher.rb', line 9 def name @name end |
#target_class ⇒ Object (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 |