Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identify item by key object instead of key string representation #27

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/batch_loader/executor_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ExecutorProxy
def initialize(default_value, key, &block)
@default_value = default_value
@block = block
@block_hash_key = "#{key}#{block.source_location}"
@block_hash_key = [block.source_location, key]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to reverse key and block.source_location, since key is used to subcategorize.

@global_executor = BatchLoader::Executor.ensure_current
end

Expand Down
32 changes: 30 additions & 2 deletions spec/batch_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,42 @@
end
end

laoded_author = batch_loader.call(Author, 1)
loader_author = batch_loader.call(Author, 1)
loader_reader = batch_loader.call(Reader, 2)

expect(Author).to receive(:where).with(id: [1]).once.and_call_original
expect(laoded_author).to eq(author)
expect(loader_author).to eq(author)
expect(Reader).to receive(:where).with(id: [2]).once.and_call_original
expect(loader_reader).to eq(reader)
end

it 'batches multiple items with hash-identical keys' do
user = Author.new(id: 1)
same_user = Reader.new(id: 1)
other_user = Reader.new(id: 2)

post_1 = Post.save(user_id: 1, title: "First post")
post_2 = Post.save(user_id: 1, title: "Second post")
post_3 = Post.save(user_id: 2, title: "First post")

batch_loader = ->(user, title) do
BatchLoader.for(title).batch(key: user) do |titles, loader, args|
args[:key].posts.select { |p| titles.include?(p.title) }.each { |post| loader.call(post.title, post) }
end
end

loader_1 = batch_loader.call(user, "First post")
loader_2 = batch_loader.call(same_user, "Second post")
loader_3 = batch_loader.call(other_user, "First post")

expect(user).to receive(:posts).once.and_call_original
expect(same_user).not_to receive(:posts)
expect(other_user).to receive(:posts).once.and_call_original

expect(loader_1).to eq(post_1)
expect(loader_2).to eq(post_2)
expect(loader_3).to eq(post_3)
end
end

context 'loader' do
Expand Down
21 changes: 17 additions & 4 deletions spec/fixtures/models.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Post
attr_accessor :user_id
attr_accessor :user_id, :title

class << self
def save(user_id:)
def save(user_id:, title: nil)
ensure_init_store
@posts << new(user_id: user_id)
new(user_id: user_id, title: title).tap { |post| @posts << post }
end

def all
Expand All @@ -23,8 +23,9 @@ def ensure_init_store
end
end

def initialize(user_id:)
def initialize(user_id:, title: nil)
self.user_id = user_id
self.title = title || "Untitled"
end

def user_lazy(cache: true)
Expand Down Expand Up @@ -68,6 +69,18 @@ def batch
"Batch from User"
end

def hash
[User, id].hash
end

def posts
Post.all.select { |p| p.user_id == id }
end

def eql?(other)
other.is_a?(User) && id == other.id
end

private

def some_private_method
Expand Down