Commit 1a99c9f16d46034c5f07eae5c384303b100d3115

Authored by Andrew Kane
1 parent 176afba3
Exists in less_deps

Less deps [skip ci]

Showing 2 changed files with 36 additions and 19 deletions   Show diff stats
lib/searchkick/hash_wrapper.rb
1 1 module Searchkick
2   - # Subclass of `Hashie::Mash` to wrap Hash-like structures
3   - # (responses from Elasticsearch)
4   - #
5   - # The primary goal of the subclass is to disable the
6   - # warning being printed by Hashie for re-defined
7   - # methods, such as `sort`.
8   - #
9   - class HashWrapper < ::Hashie::Mash
10   - disable_warnings if respond_to?(:disable_warnings)
  2 + class HashWrapper
  3 + def initialize(data)
  4 + @data = data
  5 + end
  6 +
  7 + def method_missing(m, *args, &block)
  8 + if @data.key?(m.to_s)
  9 + raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" if args.any?
  10 + @data[m.to_s]
  11 + else
  12 + super
  13 + end
  14 + end
  15 +
  16 + def respond_to?(m, include_private = false)
  17 + @data.key?(m.to_s) || super
  18 + end
  19 +
  20 + def to_h
  21 + @data
  22 + end
  23 +
  24 + def inspect
  25 + str = @data.map { |k, v| "#{k}=#{v.inspect}" }.join(" ")
  26 + "#<Searchkick::HashWrapper #{str}>"
  27 + end
11 28 end
12 29 end
... ...
test/sql_test.rb
... ... @@ -68,7 +68,7 @@ class SqlTest &lt; Minitest::Test
68 68  
69 69 def test_load_false
70 70 store_names ["Product A"]
71   - assert_kind_of Hash, Product.search("product", load: false).first
  71 + assert_kind_of Searchkick::HashWrapper, Product.search("product", load: false).first
72 72 end
73 73  
74 74 def test_load_false_methods
... ... @@ -78,7 +78,7 @@ class SqlTest &lt; Minitest::Test
78 78  
79 79 def test_load_false_with_includes
80 80 store_names ["Product A"]
81   - assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first
  81 + assert_kind_of Searchkick::HashWrapper, Product.search("product", load: false, includes: [:store]).first
82 82 end
83 83  
84 84 def test_load_false_nested_object
... ... @@ -92,7 +92,7 @@ class SqlTest &lt; Minitest::Test
92 92 def test_select
93 93 store [{name: "Product A", store_id: 1}]
94 94 result = Product.search("product", load: false, select: [:name, :store_id]).first
95   - assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
  95 + assert_equal %w(id name store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
96 96 assert_equal "Product A", result.name
97 97 assert_equal 1, result.store_id
98 98 end
... ... @@ -106,9 +106,9 @@ class SqlTest &lt; Minitest::Test
106 106 def test_select_single_field
107 107 store [{name: "Product A", store_id: 1}]
108 108 result = Product.search("product", load: false, select: :name).first
109   - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
  109 + assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
110 110 assert_equal "Product A", result.name
111   - assert_nil result.store_id
  111 + assert !result.respond_to?(:store_id)
112 112 end
113 113  
114 114 def test_select_all
... ... @@ -129,15 +129,15 @@ class SqlTest &lt; Minitest::Test
129 129 def test_select_includes
130 130 store [{name: "Product A", user_ids: [1, 2]}]
131 131 result = Product.search("product", load: false, select: {includes: [:name]}).first
132   - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
  132 + assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
133 133 assert_equal "Product A", result.name
134   - assert_nil result.store_id
  134 + assert !result.respond_to?(:store_id)
135 135 end
136 136  
137 137 def test_select_excludes
138 138 store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
139 139 result = Product.search("product", load: false, select: {excludes: [:name]}).first
140   - assert_nil result.name
  140 + assert !result.respond_to?(:name)
141 141 assert_equal [1, 2], result.user_ids
142 142 assert_equal 1, result.store_id
143 143 end
... ... @@ -147,8 +147,8 @@ class SqlTest &lt; Minitest::Test
147 147 store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
148 148 result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first
149 149 assert_equal 1, result.store_id
150   - assert_nil result.name
151   - assert_nil result.user_ids
  150 + assert !result.respond_to?(:name)
  151 + assert !result.respond_to?(:user_ids)
152 152 end
153 153  
154 154 # nested
... ...