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

Add compatibility with Ruby 3 #992

Merged
merged 4 commits into from
Sep 7, 2021
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
9 changes: 6 additions & 3 deletions elasticsearch-model/lib/elasticsearch/model/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ def initialize(target)
@target = target
end

# Delegate methods to `@target`
def ruby2_keywords(*) # :nodoc:
end if RUBY_VERSION < "2.7"

# Delegate methods to `@target`. As per [the Ruby 3.0 explanation for keyword arguments](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/), the only way to work on Ruby <2.7, and 2.7, and 3.0+ is to use `ruby2_keywords`.
#
def method_missing(method_name, *arguments, &block)
ruby2_keywords def method_missing(method_name, *arguments, &block)
target.respond_to?(method_name) ? target.__send__(method_name, *arguments, &block) : super
end

# Respond to methods from `@target`
#
def respond_to?(method_name, include_private = false)
def respond_to_missing?(method_name, include_private = false)
target.respond_to?(method_name) || super
end

Expand Down
10 changes: 9 additions & 1 deletion elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def bar
'insta barr'
end

def keyword_method(foo: 'default value')
foo
end

def as_json(options)
{foo: 'bar'}
end
Expand Down Expand Up @@ -98,7 +102,6 @@ def changes_to_save
end

context 'when instances are cloned' do

let!(:model) do
DummyProxyModel.new
end
Expand All @@ -121,4 +124,9 @@ def changes_to_save
expect(duplicate).to eq(duplicate_target)
end
end

it 'forwards keyword arguments to target methods' do
expect(DummyProxyModel.new.__elasticsearch__.keyword_method(foo: 'bar')).to eq('bar')
end

end