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 +4 -4
- data/lib/queryable_hash/version.rb +1 -1
- data/lib/queryable_hash/wrapper.rb +8 -2
- data/lib/queryable_hash.rb +2 -2
- data/test/wrapper_test.rb +18 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff91274d23e311980e0074abb7ccdcb236bd93cb
|
4
|
+
data.tar.gz: 95f0bbb76cba4ad34bf990e7461c1af30865c5a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4ae0c5ab6fba94bbdc74f74310ccd2d4e250e98f004be01952e33543b501f5a242adaa0c5e78650a816eae5056d2fbb29cc7fd5cb2e29c632e15507e70e678f
|
7
|
+
data.tar.gz: 84478f96b70c495e61d084f9b29ffb1baaa7b156519d3bbdf18ce3ea67faca7cb26e0649a081aa580381023c1bcbf94b3597db685c2f6b19dfe82841d3f54da5
|
@@ -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:
|
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
|
data/lib/queryable_hash.rb
CHANGED
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
|
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
|
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
|