From 1a99c9f16d46034c5f07eae5c384303b100d3115 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 18 Jun 2020 08:18:35 -0700 Subject: [PATCH] Less deps [skip ci] --- lib/searchkick/hash_wrapper.rb | 35 ++++++++++++++++++++++++++--------- test/sql_test.rb | 20 ++++++++++---------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/searchkick/hash_wrapper.rb b/lib/searchkick/hash_wrapper.rb index a5eb37f..a86b263 100644 --- a/lib/searchkick/hash_wrapper.rb +++ b/lib/searchkick/hash_wrapper.rb @@ -1,12 +1,29 @@ module Searchkick - # Subclass of `Hashie::Mash` to wrap Hash-like structures - # (responses from Elasticsearch) - # - # The primary goal of the subclass is to disable the - # warning being printed by Hashie for re-defined - # methods, such as `sort`. - # - class HashWrapper < ::Hashie::Mash - disable_warnings if respond_to?(:disable_warnings) + class HashWrapper + def initialize(data) + @data = data + end + + def method_missing(m, *args, &block) + if @data.key?(m.to_s) + raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0)" if args.any? + @data[m.to_s] + else + super + end + end + + def respond_to?(m, include_private = false) + @data.key?(m.to_s) || super + end + + def to_h + @data + end + + def inspect + str = @data.map { |k, v| "#{k}=#{v.inspect}" }.join(" ") + "#" + end end end diff --git a/test/sql_test.rb b/test/sql_test.rb index abd42db..2de1b37 100644 --- a/test/sql_test.rb +++ b/test/sql_test.rb @@ -68,7 +68,7 @@ class SqlTest < Minitest::Test def test_load_false store_names ["Product A"] - assert_kind_of Hash, Product.search("product", load: false).first + assert_kind_of Searchkick::HashWrapper, Product.search("product", load: false).first end def test_load_false_methods @@ -78,7 +78,7 @@ class SqlTest < Minitest::Test def test_load_false_with_includes store_names ["Product A"] - assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first + assert_kind_of Searchkick::HashWrapper, Product.search("product", load: false, includes: [:store]).first end def test_load_false_nested_object @@ -92,7 +92,7 @@ class SqlTest < Minitest::Test def test_select store [{name: "Product A", store_id: 1}] result = Product.search("product", load: false, select: [:name, :store_id]).first - assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal %w(id name store_id), result.to_h.keys.reject { |k| k.start_with?("_") }.sort assert_equal "Product A", result.name assert_equal 1, result.store_id end @@ -106,9 +106,9 @@ class SqlTest < Minitest::Test def test_select_single_field store [{name: "Product A", store_id: 1}] result = Product.search("product", load: false, select: :name).first - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort assert_equal "Product A", result.name - assert_nil result.store_id + assert !result.respond_to?(:store_id) end def test_select_all @@ -129,15 +129,15 @@ class SqlTest < Minitest::Test def test_select_includes store [{name: "Product A", user_ids: [1, 2]}] result = Product.search("product", load: false, select: {includes: [:name]}).first - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal %w(id name), result.to_h.keys.reject { |k| k.start_with?("_") }.sort assert_equal "Product A", result.name - assert_nil result.store_id + assert !result.respond_to?(:store_id) end def test_select_excludes store [{name: "Product A", user_ids: [1, 2], store_id: 1}] result = Product.search("product", load: false, select: {excludes: [:name]}).first - assert_nil result.name + assert !result.respond_to?(:name) assert_equal [1, 2], result.user_ids assert_equal 1, result.store_id end @@ -147,8 +147,8 @@ class SqlTest < Minitest::Test store [{name: "Product A", user_ids: [1, 2], store_id: 1}] result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first assert_equal 1, result.store_id - assert_nil result.name - assert_nil result.user_ids + assert !result.respond_to?(:name) + assert !result.respond_to?(:user_ids) end # nested -- libgit2 0.21.0