Skip to content

Commit

Permalink
Merge branch 'jamesbrooks-additional-ruby3-kwargs-fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Nov 23, 2023
2 parents e8148d8 + 251a93e commit 30c5fb1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/batch_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def __loaded_value
@cache ? @loaded_value : result
end

def method_missing(method_name, *args, &block)
__sync!.public_send(method_name, *args, &block)
def method_missing(method_name, *args, **kwargs, &block)
__sync!.public_send(method_name, *args, **kwargs, &block)
end

def __sync!
Expand Down Expand Up @@ -120,8 +120,8 @@ class << self ; self ; end
def __replace_with!(value)
__singleton_class.class_eval do
(value.methods - LEFT_INSTANCE_METHODS).each do |method_name|
define_method(method_name) do |*args, &block|
value.public_send(method_name, *args, &block)
define_method(method_name) do |*args, **kwargs, &block|
value.public_send(method_name, *args, **kwargs, &block)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/batch_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@
end
end

context 'instance' do
it 'works for methods with kwargs in Ruby 3' do
user = User.save(id: 1)
post = Post.new(user_id: user.id)

batch_loader = post.user_lazy

expect(batch_loader.method_with_arg_kwarg_and_block(1, b: 2) { |a, b| a + b }).to eq(3)
end
end

describe '#inspect' do
it 'returns BatchLoader without syncing and delegates #inspect after' do
user = User.save(id: 1)
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def eql?(other)
other.is_a?(User) && id == other.id
end

def method_with_arg_kwarg_and_block(a, b:, &block)
block.call(a, b)
end

private

def some_private_method
Expand Down

0 comments on commit 30c5fb1

Please sign in to comment.