Skip to content

Commit 2f1cf90

Browse files
committed
Removed dependency on Hashie
1 parent 3fb42a8 commit 2f1cf90

6 files changed

Lines changed: 58 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Changed async reindex to use ranges for numeric primary keys with Active Record
77
- Removed default quantization for `knn` option for Elasticsearch 8.14+
88
- Removed `execute` option and method (no longer needed)
9+
- Removed dependency on Hashie
910
- Deprecated `conversions` option in favor of `conversions_v2`
1011
- Dropped support for Elasticsearch 7 and OpenSearch 1
1112
- Dropped support for Active Record < 7.2

lib/searchkick.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
require "active_support/deprecation"
77
require "active_support/log_subscriber"
88
require "active_support/notifications"
9-
require "hashie"
109

1110
# stdlib
1211
require "forwardable"

lib/searchkick/hash_wrapper.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
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(attributes)
4+
@attributes = attributes
5+
end
6+
7+
def [](name)
8+
@attributes[name.to_s]
9+
end
10+
11+
def to_h
12+
@attributes
13+
end
14+
15+
def method_missing(name, ...)
16+
if @attributes.key?(name.to_s)
17+
self[name]
18+
else
19+
super
20+
end
21+
end
22+
23+
def respond_to_missing?(name, ...)
24+
@attributes.key?(name.to_s) || super
25+
end
26+
27+
def inspect
28+
attributes = @attributes.reject { |k, v| k[0] == "_" }.map { |k, v| "#{k}: #{v.inspect}" }
29+
attributes.unshift(attributes.pop) # move id to start
30+
"#<#{self.class.name} #{attributes.join(", ")}>"
31+
end
1132
end
1233
end

searchkick.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@ Gem::Specification.new do |spec|
1616
spec.required_ruby_version = ">= 3.2"
1717

1818
spec.add_dependency "activemodel", ">= 7.2"
19-
spec.add_dependency "hashie"
2019
end

test/load_test.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
class LoadTest < Minitest::Test
44
def test_default
55
store_names ["Product A"]
6-
assert_kind_of Product, Product.search("product").first
6+
product = Product.search("product").first
7+
assert_kind_of Product, product
8+
assert_match "#<Product id: ", product.inspect
9+
assert_equal "Product A", product.name
10+
assert_equal "Product A", product[:name]
11+
assert_equal "Product A", product["name"]
12+
refute product.respond_to?(:missing)
13+
assert_nil product[:missing]
714
end
815

916
def test_false
1017
store_names ["Product A"]
11-
assert_kind_of Searchkick::HashWrapper, Product.search("product", load: false).first
18+
product = Product.search("product", load: false).first
19+
assert_kind_of Searchkick::HashWrapper, product
20+
assert_match "#<Searchkick::HashWrapper id: ", product.inspect
21+
assert_equal "Product A", product.name
22+
assert_equal "Product A", product[:name]
23+
assert_equal "Product A", product["name"]
24+
refute product.respond_to?(:missing)
25+
assert_nil product[:missing]
1226
end
1327

1428
def test_false_methods

test/select_test.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ class SelectTest < Minitest::Test
44
def test_basic
55
store [{name: "Product A", store_id: 1}]
66
result = Product.search("product", load: false, select: [:name, :store_id]).first
7-
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
7+
assert_equal %w(id name store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
88
assert_equal "Product A", result.name
99
assert_equal 1, result.store_id
1010
end
1111

1212
def test_relation
1313
store [{name: "Product A", store_id: 1}]
1414
result = Product.search("product", load: false).select(:name, :store_id).first
15-
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
15+
assert_equal %w(id name store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
1616
assert_equal "Product A", result.name
1717
assert_equal 1, result.store_id
1818
end
@@ -32,15 +32,15 @@ def test_block_arguments
3232
def test_multiple
3333
store [{name: "Product A", store_id: 1}]
3434
result = Product.search("product", load: false).select(:name).select(:store_id).first
35-
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
35+
assert_equal %w(id name store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
3636
assert_equal "Product A", result.name
3737
assert_equal 1, result.store_id
3838
end
3939

4040
def test_reselect
4141
store [{name: "Product A", store_id: 1}]
4242
result = Product.search("product", load: false).select(:name).reselect(:store_id).first
43-
assert_equal %w(id store_id), result.keys.reject { |k| k.start_with?("_") }.sort
43+
assert_equal %w(id store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
4444
assert_equal 1, result.store_id
4545
end
4646

@@ -53,9 +53,9 @@ def test_array
5353
def test_single_field
5454
store [{name: "Product A", store_id: 1}]
5555
result = Product.search("product", load: false, select: :name).first
56-
assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
56+
assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
5757
assert_equal "Product A", result.name
58-
assert_nil result.store_id
58+
refute result.respond_to?(:store_id)
5959
end
6060

6161
def test_all
@@ -76,15 +76,15 @@ def test_none
7676
def test_includes
7777
store [{name: "Product A", user_ids: [1, 2]}]
7878
result = Product.search("product", load: false, select: {includes: [:name]}).first
79-
assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
79+
assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort
8080
assert_equal "Product A", result.name
81-
assert_nil result.store_id
81+
refute result.respond_to?(:store_id)
8282
end
8383

8484
def test_excludes
8585
store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
8686
result = Product.search("product", load: false, select: {excludes: [:name]}).first
87-
assert_nil result.name
87+
refute result.respond_to?(:name)
8888
assert_equal [1, 2], result.user_ids
8989
assert_equal 1, result.store_id
9090
end
@@ -94,7 +94,7 @@ def test_include_and_excludes
9494
store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
9595
result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first
9696
assert_equal 1, result.store_id
97-
assert_nil result.name
98-
assert_nil result.user_ids
97+
refute result.respond_to?(:name)
98+
refute result.respond_to?(:user_ids)
9999
end
100100
end

0 commit comments

Comments
 (0)