Skip to content

Commit 0043e4d

Browse files
committed
[MODEL] Allow acessing result._source.foo as result.foo directly
1 parent 8bffa19 commit 0043e4d

File tree

1 file changed

+11
-2
lines changed
  • elasticsearch-model/lib/elasticsearch/model/response

1 file changed

+11
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ def initialize(attributes)
1010
# Delegate methods to `@result`
1111
#
1212
def method_missing(method_name, *arguments)
13-
@result.respond_to?(method_name.to_sym) ? @result[method_name.to_sym] : super
13+
case
14+
when @result.respond_to?(method_name.to_sym)
15+
@result[method_name.to_sym]
16+
when @result._source && @result._source.respond_to?(method_name.to_sym)
17+
@result._source[method_name.to_sym]
18+
else
19+
super
20+
end
1421
end
1522

1623
# Respond to methods from `@result`
1724
#
1825
def respond_to?(method_name, include_private = false)
19-
@result.has_key?(method_name.to_sym) || super
26+
@result.has_key?(method_name.to_sym) || \
27+
@result._source && @result._source.has_key?(method_name.to_sym) || \
28+
super
2029
end
2130

2231
# TODO: #to_s, #inspect, with support for Pry

0 commit comments

Comments
 (0)