Class: BatchLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_loader.rb,
lib/batch_loader/version.rb,
lib/batch_loader/executor.rb

Defined Under Namespace

Classes: Executor

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item:) ⇒ BatchLoader

Returns a new instance of BatchLoader.



22
23
24
# File 'lib/batch_loader.rb', line 22

def initialize(item:)
  @item = item
end

Class Method Details

.for(item) ⇒ Object



5
6
7
# File 'lib/batch_loader.rb', line 5

def self.for(item)
  new(item: item)
end

.sync!(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/batch_loader.rb', line 9

def self.sync!(value)
  case value
  when Array
    value.map { |v| sync!(v) }
  when Hash
    value.each { |k, v| value[k] = sync!(v) }
  when BatchLoader
    sync!(value.sync)
  else
    value
  end
end

Instance Method Details

#batch(&batch_block) ⇒ Object



26
27
28
29
30
# File 'lib/batch_loader.rb', line 26

def batch(&batch_block)
  @batch_block = batch_block
  executor.add_item(@item, &@batch_block)
  self
end

#load(item, loaded_item) ⇒ Object



32
33
34
# File 'lib/batch_loader.rb', line 32

def load(item, loaded_item)
  executor.save(item, loaded_item, &@batch_block)
end

#syncObject



36
37
38
39
40
41
42
43
# File 'lib/batch_loader.rb', line 36

def sync
  unless executor.saved?(&@batch_block)
    items = executor.items(&@batch_block)
    @batch_block.call(items, self)
  end

  executor.find(@item, &@batch_block)
end