queryable_hash 0.0.1 → 0.0.2

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: eae6be91fce7bc5ee219a1b1fc1af4ed19e21ae8
4
- data.tar.gz: 53b42bdfd7e52c98a95925420e5e580bd57b6a3e
3
+ metadata.gz: 11acee1b3ab7cc1a5e72a3dd41e60237f35e4b48
4
+ data.tar.gz: cadeeaecf05682fd868c6d6bf7a80b7fa782d5a7
5
5
  SHA512:
6
- metadata.gz: 4fa48f8de809bbdb9250bafdb1ec7af868ffba82985daf4983aaa575ee789b3bce9126f12e6cd10c3af08e7b28423c220eb648f173955e59138d5eaaab0f7564
7
- data.tar.gz: 1456438f377219c2f59996ec8e1867dba6c060d085c9a07184c8fb633a6bf3c4045889e91a753d69bc520091ffd406ea46b5f3f10667a42f5d5ffd549c12afa4
6
+ metadata.gz: 8d43064e8d08fd036bc796579dd748e918b39f7ec4219d75a0ae9a3507360385c311c3ef8f48651449c395ef61b972dc4a88dd3d3b5e35ad8b6630a5c7b55221
7
+ data.tar.gz: 01de95f0c6edc25d80f683f6b66a881cfc100f922a2705edf2fc9ed252f4c6d37baf02a13b4bd06bc77085a1014cddcc134d36d48e2808551e85adf71209cc04
@@ -0,0 +1,4 @@
1
+ module QueryableHash
2
+ class NotFoundError < StandardError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module QueryableHash
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -19,13 +19,18 @@ module QueryableHash
19
19
  end
20
20
  end
21
21
 
22
- def find_first(*queries, nil_value: nil)
22
+ def find_first(*queries, nil_value: nil, raise_when_nil: false)
23
23
  first = find_all(*queries, nil_value: nil_value).find do |result|
24
24
  result != nil_value
25
25
  end
26
- first || nil_value
26
+ first ||= nil_value
27
+
28
+ raise NotFoundError if raise_when_nil && first == nil_value
29
+ first
27
30
  end
28
31
 
32
+ alias_method :find, :find_first
33
+
29
34
  def to_hash
30
35
  @original_hash
31
36
  end
@@ -1,4 +1,5 @@
1
1
  require_relative "queryable_hash/version"
2
+ require_relative "queryable_hash/not_found_error"
2
3
  require_relative "queryable_hash/wrapper"
3
4
 
4
5
  module QueryableHash
data/test/wrapper_test.rb CHANGED
@@ -49,6 +49,15 @@ module QueryableHash
49
49
  assert @queryable.find_first(query) == "SGML"
50
50
  end
51
51
 
52
+ test "find_first with multiple queries" do
53
+ result = @queryable.find_first(
54
+ "glossary.title",
55
+ "glossary.gloss_div.gloss_list.gloss_entry.gloss_term",
56
+ "glossary.gloss_div.gloss_list.gloss_entry.gloss_def.para"
57
+ )
58
+ assert result == "example glossary"
59
+ end
60
+
52
61
  test "find_first with missing key" do
53
62
  query = "this.key.does.not.exist"
54
63
  assert @queryable.find_first(query).nil?
@@ -59,7 +68,7 @@ module QueryableHash
59
68
  assert @queryable.find_first(query, nil_value: "missing") == "missing"
60
69
  end
61
70
 
62
- test "find_first with multiple queries" do
71
+ test "find_first with multiple queries some invalid" do
63
72
  term = @queryable.find_first(
64
73
  "glossary.div.list.entry.term",
65
74
  "glossary.gloss_div.list.entry.term",
@@ -70,6 +79,16 @@ module QueryableHash
70
79
  assert term == "Standard Generalized Markup Language"
71
80
  end
72
81
 
82
+ test "find_first with raise" do
83
+ query = "nate.this.key.does.not.exist"
84
+ begin
85
+ @queryable.find_first(query, raise_when_nil: true)
86
+ rescue NotFoundError => e
87
+ end
88
+
89
+ assert e
90
+ end
91
+
73
92
  test "to_hash" do
74
93
  assert @queryable.to_hash == @data
75
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queryable_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-23 00:00:00.000000000 Z
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - lib/queryable_hash.rb
91
+ - lib/queryable_hash/not_found_error.rb
91
92
  - lib/queryable_hash/version.rb
92
93
  - lib/queryable_hash/wrapper.rb
93
94
  - test/test_helper.rb