queryable_hash 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11acee1b3ab7cc1a5e72a3dd41e60237f35e4b48
4
- data.tar.gz: cadeeaecf05682fd868c6d6bf7a80b7fa782d5a7
3
+ metadata.gz: ff91274d23e311980e0074abb7ccdcb236bd93cb
4
+ data.tar.gz: 95f0bbb76cba4ad34bf990e7461c1af30865c5a2
5
5
  SHA512:
6
- metadata.gz: 8d43064e8d08fd036bc796579dd748e918b39f7ec4219d75a0ae9a3507360385c311c3ef8f48651449c395ef61b972dc4a88dd3d3b5e35ad8b6630a5c7b55221
7
- data.tar.gz: 01de95f0c6edc25d80f683f6b66a881cfc100f922a2705edf2fc9ed252f4c6d37baf02a13b4bd06bc77085a1014cddcc134d36d48e2808551e85adf71209cc04
6
+ metadata.gz: c4ae0c5ab6fba94bbdc74f74310ccd2d4e250e98f004be01952e33543b501f5a242adaa0c5e78650a816eae5056d2fbb29cc7fd5cb2e29c632e15507e70e678f
7
+ data.tar.gz: 84478f96b70c495e61d084f9b29ffb1baaa7b156519d3bbdf18ce3ea67faca7cb26e0649a081aa580381023c1bcbf94b3597db685c2f6b19dfe82841d3f54da5
@@ -1,3 +1,3 @@
1
1
  module QueryableHash
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,9 +2,12 @@ require "delegate"
2
2
 
3
3
  module QueryableHash
4
4
  class Wrapper < SimpleDelegator
5
+ attr_reader :nil_value, :raise_when_nil
5
6
 
6
- def initialize(hash)
7
+ def initialize(hash, nil_value: nil, raise_when_nil: false)
7
8
  @original_hash = hash
9
+ @nil_value = nil_value
10
+ @raise_when_nil = raise_when_nil
8
11
  super hash
9
12
  end
10
13
 
@@ -19,7 +22,10 @@ module QueryableHash
19
22
  end
20
23
  end
21
24
 
22
- def find_first(*queries, nil_value: nil, raise_when_nil: false)
25
+ def find_first(*queries, nil_value: nil, raise_when_nil: nil)
26
+ nil_value = @nil_value if nil_value.nil?
27
+ raise_when_nil = @raise_when_nil if raise_when_nil.nil?
28
+
23
29
  first = find_all(*queries, nil_value: nil_value).find do |result|
24
30
  result != nil_value
25
31
  end
@@ -3,7 +3,7 @@ require_relative "queryable_hash/not_found_error"
3
3
  require_relative "queryable_hash/wrapper"
4
4
 
5
5
  module QueryableHash
6
- def self.wrap(hash)
7
- Wrapper.new hash
6
+ def self.wrap(hash, **args)
7
+ Wrapper.new hash, **args
8
8
  end
9
9
  end
data/test/wrapper_test.rb CHANGED
@@ -63,11 +63,17 @@ module QueryableHash
63
63
  assert @queryable.find_first(query).nil?
64
64
  end
65
65
 
66
- test "find_first with missing key and nil value" do
66
+ test "find_first with missing key and nil_value" do
67
67
  query = "nate.this.key.does.not.exist"
68
68
  assert @queryable.find_first(query, nil_value: "missing") == "missing"
69
69
  end
70
70
 
71
+ test "find_first with missing key and nil_value set by instance" do
72
+ query = "nate.this.key.does.not.exist"
73
+ queryable = QueryableHash.wrap(@data, nil_value: "missing")
74
+ assert queryable.find_first(query) == "missing"
75
+ end
76
+
71
77
  test "find_first with multiple queries some invalid" do
72
78
  term = @queryable.find_first(
73
79
  "glossary.div.list.entry.term",
@@ -79,7 +85,7 @@ module QueryableHash
79
85
  assert term == "Standard Generalized Markup Language"
80
86
  end
81
87
 
82
- test "find_first with raise" do
88
+ test "find_first with raise_when_nil" do
83
89
  query = "nate.this.key.does.not.exist"
84
90
  begin
85
91
  @queryable.find_first(query, raise_when_nil: true)
@@ -89,6 +95,16 @@ module QueryableHash
89
95
  assert e
90
96
  end
91
97
 
98
+ test "find_first with raise_when_nil set by instance" do
99
+ query = "nate.this.key.does.not.exist"
100
+ begin
101
+ queryable = QueryableHash.wrap(@data, raise_when_nil: true)
102
+ queryable.find_first(query)
103
+ rescue NotFoundError => e
104
+ end
105
+ assert e
106
+ end
107
+
92
108
  test "to_hash" do
93
109
  assert @queryable.to_hash == @data
94
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queryable_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins