Skip to content

Commit

Permalink
less magic: dont auto push to default value
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Bessey committed Oct 30, 2017
1 parent f7d1e94 commit 7b8cfa6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 35 deletions.
11 changes: 1 addition & 10 deletions lib/batch_loader/executor_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class ExecutorProxy

def initialize(default_value, &block)
@default_value = default_value
@value_appendable = @default_value.respond_to?(:push)
@block = block
@block_hash_key = block.source_location
@global_executor = BatchLoader::Executor.ensure_current
Expand All @@ -27,11 +26,7 @@ def delete(items:)
end

def load(item:, value:)
loaded[item] = if value_appendable?
loaded_value(item: item).push(value)
else
value
end
loaded[item] = value
end

def loaded_value(item:)
Expand All @@ -50,10 +45,6 @@ def unload_value(item:)
loaded.delete(item)
end

def value_appendable?
@value_appendable
end

private

def items_to_load
Expand Down
31 changes: 6 additions & 25 deletions spec/batch_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,18 @@
expect(lazy).to eq(123)
end

it 'appends to the default value when possible' do
lazy = BatchLoader.for(1).batch(default_value: [1]) do |nums, loader|
nums.each { |num| loader.call(num, 123) }
end

expect(lazy).to eq([1, 123])
end

it 'supports setting the value from return of loader.call block' do
lazy = BatchLoader.for(1).batch(default_value: {}) do |nums, loader|
it 'supports memoizing repeated calls to the same item, via a block' do
lazy = BatchLoader.for(1).batch(default_value: []) do |nums, loader|
nums.each do |num|
loader.call(num) { |vector| vector[num] = "ok"; vector }
loader.call(num) { |memo| memo.push(num) }
loader.call(num) { |memo| memo.push(num + 1) }
loader.call(num) { |memo| memo.push(num + 2) }
end
end

expect(lazy).to eq(1 => "ok")
expect(lazy).to eq([1,2,3])
end


context "called with block and value syntax" do
it 'raises ArgumentError' do
lazy = BatchLoader.for(1).batch(default_value: {}) do |nums, loader|
Expand All @@ -137,18 +130,6 @@
expect { lazy.sync }.to raise_error(ArgumentError)
end
end

context "called with neither block nor value syntax" do
it 'raises ArgumentError' do
lazy = BatchLoader.for(1).batch do |nums, loader|
nums.each do |num|
loader.call(num)
end
end

expect { lazy.sync }.to raise_error(ArgumentError)
end
end
end

describe '#inspect' do
Expand Down

0 comments on commit 7b8cfa6

Please sign in to comment.