Skip to content

Commit 4d24ecd

Browse files
committed
[MODEL] Fixed incorrect delegation to @Result Hash in Response::Result
1 parent 75fa0ad commit 4d24ecd

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def initialize(attributes={})
2222
def method_missing(method_name, *arguments)
2323
case
2424
when @result.respond_to?(method_name.to_sym)
25-
@result[method_name.to_sym]
25+
@result.__send__ method_name.to_sym, *arguments
2626
when @result._source && @result._source.respond_to?(method_name.to_sym)
27-
@result._source[method_name.to_sym]
27+
@result._source.__send__ method_name.to_sym, *arguments
2828
else
2929
super
3030
end
@@ -33,8 +33,8 @@ def method_missing(method_name, *arguments)
3333
# Respond to methods from `@result` or `@result._source`
3434
#
3535
def respond_to?(method_name, include_private = false)
36-
@result.has_key?(method_name.to_sym) || \
37-
@result._source && @result._source.has_key?(method_name.to_sym) || \
36+
@result.respond_to?(method_name.to_sym) || \
37+
@result._source && @result._source.respond_to?(method_name.to_sym) || \
3838
super
3939
end
4040

elasticsearch-model/test/unit/response_result_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,18 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
2727
assert_equal 'baz', result.bar
2828
end
2929

30+
should "delegate methods to @result" do
31+
result = Elasticsearch::Model::Response::Result.new foo: 'bar'
32+
33+
assert_equal 'bar', result.foo
34+
assert_equal 'bar', result.fetch('foo')
35+
assert_equal 'moo', result.fetch('NOT_EXIST', 'moo')
36+
37+
assert_respond_to result, :to_hash
38+
assert_equal({'foo' => 'bar'}, result.to_hash)
39+
40+
assert_raise(NoMethodError) { result.does_not_exist }
41+
end
42+
3043
end
3144
end

0 commit comments

Comments
 (0)