Skip to content

Commit d04318c

Browse files
committed
Add compatibility with Ruby 3 for 6.x
1 parent d6c88ce commit d04318c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

elasticsearch-model/lib/elasticsearch/model/proxy.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,18 @@ def initialize(target)
9090
@target = target
9191
end
9292

93-
# Delegate methods to `@target`
93+
def ruby2_keywords(*) # :nodoc:
94+
end if RUBY_VERSION < "2.7"
95+
96+
# 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`.
9497
#
95-
def method_missing(method_name, *arguments, &block)
98+
ruby2_keywords def method_missing(method_name, *arguments, &block)
9699
target.respond_to?(method_name) ? target.__send__(method_name, *arguments, &block) : super
97100
end
98101

99102
# Respond to methods from `@target`
100103
#
101-
def respond_to?(method_name, include_private = false)
104+
def respond_to_missing?(method_name, include_private = false)
102105
target.respond_to?(method_name) || super
103106
end
104107

elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def bar
1414
'insta barr'
1515
end
1616

17+
def keyword_method(foo: 'default value')
18+
foo
19+
end
20+
1721
def as_json(options)
1822
{foo: 'bar'}
1923
end
@@ -104,4 +108,8 @@ def changes_to_save
104108
expect(duplicate).to eq(duplicate_target)
105109
end
106110
end
111+
112+
it 'forwards keyword arguments to target methods' do
113+
expect(DummyProxyModel.new.__elasticsearch__.keyword_method(foo: 'bar')).to eq('bar')
114+
end
107115
end

0 commit comments

Comments
 (0)