Class: BatchLoader::Executor

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

Constant Summary collapse

NAMESPACE =
:batch_loader

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



13
14
15
16
# File 'lib/batch_loader/executor.rb', line 13

def initialize
  @items_by_batch_block = Hash.new { |hash, key| hash[key] = [] }
  @loaded_items_by_batch_block = Hash.new { |hash, key| hash[key] = {} }
end

Class Method Details

.clear_currentObject



9
10
11
# File 'lib/batch_loader/executor.rb', line 9

def self.clear_current
  Thread.current[NAMESPACE] = nil
end

.ensure_currentObject



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

def self.ensure_current
  Thread.current[NAMESPACE] = Thread.current[NAMESPACE] || new
end

Instance Method Details

#add_item(item, &batch_block) ⇒ Object



18
19
20
# File 'lib/batch_loader/executor.rb', line 18

def add_item(item, &batch_block)
  @items_by_batch_block[batch_block.source_location] << item
end

#find(item, &batch_block) ⇒ Object



34
35
36
# File 'lib/batch_loader/executor.rb', line 34

def find(item, &batch_block)
  @loaded_items_by_batch_block.dig(batch_block.source_location, item)
end

#items(&batch_block) ⇒ Object



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

def items(&batch_block)
  @items_by_batch_block[batch_block.source_location]
end

#save(item, loaded_item, &batch_block) ⇒ Object



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

def save(item, loaded_item, &batch_block)
  @loaded_items_by_batch_block[batch_block.source_location][item] = loaded_item
end

#saved?(&batch_block) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/batch_loader/executor.rb', line 30

def saved?(&batch_block)
  @loaded_items_by_batch_block.key?(batch_block.source_location)
end