From d04318c82b3a8d4b1cac3b921f59f10de8098bb1 Mon Sep 17 00:00:00 2001 From: Mark Tityuk Date: Tue, 14 Mar 2023 20:23:59 +0600 Subject: [PATCH] Add compatibility with Ruby 3 for 6.x --- elasticsearch-model/lib/elasticsearch/model/proxy.rb | 9 ++++++--- .../spec/elasticsearch/model/proxy_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/elasticsearch-model/lib/elasticsearch/model/proxy.rb b/elasticsearch-model/lib/elasticsearch/model/proxy.rb index a1c89ca95..f7aca4e12 100644 --- a/elasticsearch-model/lib/elasticsearch/model/proxy.rb +++ b/elasticsearch-model/lib/elasticsearch/model/proxy.rb @@ -90,15 +90,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 diff --git a/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb b/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb index 6e484f896..6f48764fe 100644 --- a/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb +++ b/elasticsearch-model/spec/elasticsearch/model/proxy_spec.rb @@ -14,6 +14,10 @@ def bar 'insta barr' end + def keyword_method(foo: 'default value') + foo + end + def as_json(options) {foo: 'bar'} end @@ -104,4 +108,8 @@ 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