Skip to content

Commit 3f2c03f

Browse files
emptyflaskestolfo
authored andcommitted
respond_to_missing? to silence Ruby 2.4 warnings (#838)
I ran into a bunch of warnings when using Elastic results with Ruby 2.4+ and trailblazer/cells, because `#method_missing?` without `#respond_to_missing?` makes Forwardable think all of the Result attributes are private methods. https://robots.thoughtbot.com/always-define-respond-to-missing-when-overriding
1 parent 93a0da0 commit 3f2c03f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

elasticsearch-model/lib/elasticsearch/model/response/result.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def method_missing(name, *arguments)
4646

4747
# Respond to methods from `@result` or `@result._source`
4848
#
49-
def respond_to?(method_name, include_private = false)
49+
def respond_to_missing?(method_name, include_private = false)
5050
@result.respond_to?(method_name.to_sym) || \
5151
@result._source && @result._source.respond_to?(method_name.to_sym) || \
5252
super

elasticsearch-model/spec/elasticsearch/model/response/result_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
expect(result._source).to eq('bar' => { 'bam' => 'baz' })
5454
end
5555

56+
it 'is recognized by #method' do
57+
expect(result.method :bar).to be_a Method
58+
end
59+
60+
it 'respond_to? still works' do
61+
expect(result.respond_to? :bar).to be true
62+
end
63+
5664
context 'when methods map to keys in subdocuments of the response from Elasticsearch' do
5765

5866
it 'provides access to top level fields via a method' do

0 commit comments

Comments
 (0)