queryable_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eae6be91fce7bc5ee219a1b1fc1af4ed19e21ae8
4
+ data.tar.gz: 53b42bdfd7e52c98a95925420e5e580bd57b6a3e
5
+ SHA512:
6
+ metadata.gz: 4fa48f8de809bbdb9250bafdb1ec7af868ffba82985daf4983aaa575ee789b3bce9126f12e6cd10c3af08e7b28423c220eb648f173955e59138d5eaaab0f7564
7
+ data.tar.gz: 1456438f377219c2f59996ec8e1867dba6c060d085c9a07184c8fb633a6bf3c4045889e91a753d69bc520091ffd406ea46b5f3f10667a42f5d5ffd549c12afa4
@@ -0,0 +1,3 @@
1
+ module QueryableHash
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ require "delegate"
2
+
3
+ module QueryableHash
4
+ class Wrapper < SimpleDelegator
5
+
6
+ def initialize(hash)
7
+ @original_hash = hash
8
+ super hash
9
+ end
10
+
11
+ def find_all(*queries, nil_value: nil)
12
+ queries.reduce([]) do |memo, query|
13
+ context = self
14
+ query.split(".").each do |name|
15
+ break if context.nil?
16
+ context = context[name.to_sym] || context[name]
17
+ end
18
+ memo << (context || nil_value)
19
+ end
20
+ end
21
+
22
+ def find_first(*queries, nil_value: nil)
23
+ first = find_all(*queries, nil_value: nil_value).find do |result|
24
+ result != nil_value
25
+ end
26
+ first || nil_value
27
+ end
28
+
29
+ def to_hash
30
+ @original_hash
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "queryable_hash/version"
2
+ require_relative "queryable_hash/wrapper"
3
+
4
+ module QueryableHash
5
+ def self.wrap(hash)
6
+ Wrapper.new hash
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ require "simplecov"
2
+
3
+ if ENV["CI"]
4
+ require "coveralls"
5
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
6
+ end
7
+
8
+ SimpleCov.command_name "pry-test"
9
+ SimpleCov.start do
10
+ add_filter "/test/"
11
+ end
12
+
13
+ Coveralls.wear! if ENV["CI"]
14
+
15
+ require_relative "../lib/queryable_hash"
@@ -0,0 +1,77 @@
1
+ require_relative "test_helper"
2
+
3
+ module QueryableHash
4
+ class QueryableHashTest < PryTest::Test
5
+ before do
6
+ @data = {
7
+ glossary: {
8
+ title: "example glossary",
9
+ gloss_div: {
10
+ title: "S",
11
+ gloss_list: {
12
+ gloss_entry: {
13
+ id: "SGML",
14
+ sort_as: "SGML",
15
+ gloss_term: "Standard Generalized Markup Language",
16
+ acronym: "SGML",
17
+ abbrev: "ISO 8879:1986",
18
+ gloss_def: {
19
+ para: "A meta-markup language, used to create markup languages such as DocBook.",
20
+ gloss_see_also: ["GML", "XML"]
21
+ },
22
+ gloss_see: "markup"
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
28
+ @queryable = QueryableHash.wrap(@data)
29
+ end
30
+
31
+ test "find_all" do
32
+ query = "glossary.gloss_div.gloss_list.gloss_entry.id"
33
+ assert @queryable.find_all(query) == ["SGML"]
34
+ end
35
+
36
+ test "find_all with multiple queries" do
37
+ title, term, para = @queryable.find_all(
38
+ "glossary.title",
39
+ "glossary.gloss_div.gloss_list.gloss_entry.gloss_term",
40
+ "glossary.gloss_div.gloss_list.gloss_entry.gloss_def.para"
41
+ )
42
+ assert title == "example glossary"
43
+ assert term == "Standard Generalized Markup Language"
44
+ assert para == "A meta-markup language, used to create markup languages such as DocBook."
45
+ end
46
+
47
+ test "find_first" do
48
+ query = "glossary.gloss_div.gloss_list.gloss_entry.id"
49
+ assert @queryable.find_first(query) == "SGML"
50
+ end
51
+
52
+ test "find_first with missing key" do
53
+ query = "this.key.does.not.exist"
54
+ assert @queryable.find_first(query).nil?
55
+ end
56
+
57
+ test "find_first with missing key and nil value" do
58
+ query = "nate.this.key.does.not.exist"
59
+ assert @queryable.find_first(query, nil_value: "missing") == "missing"
60
+ end
61
+
62
+ test "find_first with multiple queries" do
63
+ term = @queryable.find_first(
64
+ "glossary.div.list.entry.term",
65
+ "glossary.gloss_div.list.entry.term",
66
+ "glossary.gloss_div.gloss_list.entry.term",
67
+ "glossary.gloss_div.gloss_list.gloss_entry.term",
68
+ "glossary.gloss_div.gloss_list.gloss_entry.gloss_term"
69
+ )
70
+ assert term == "Standard Generalized Markup Language"
71
+ end
72
+
73
+ test "to_hash" do
74
+ assert @queryable.to_hash == @data
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queryable_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Safely & easily find data in Hashes using dot notation queries.
84
+ email:
85
+ - natehop@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/queryable_hash.rb
91
+ - lib/queryable_hash/version.rb
92
+ - lib/queryable_hash/wrapper.rb
93
+ - test/test_helper.rb
94
+ - test/wrapper_test.rb
95
+ homepage: https://github.com/hopsoft/queryable_hash
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.5
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Safely & easily find data in Hashes using dot notation queries.
119
+ test_files:
120
+ - test/test_helper.rb
121
+ - test/wrapper_test.rb
122
+ has_rdoc: