nested_lookup 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/README.md +67 -9
- data/lib/lookup_api.rb +118 -0
- data/lib/nested_lookup.rb +5 -0
- data/lib/nested_lookup/version.rb +1 -1
- data/nested_lookup.gemspec +1 -2
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6375c79eb43aa42a0cc87dac7c2c698a14b72bea
|
4
|
+
data.tar.gz: a642e98b2083a1f8b5f33198a28ad8492fc88968
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe7cc244265a0c8a93acaf1736ec3ede23c196a889836f06f45de5f1a67439513ed393e09356081fbc1a4d941c996f2cb3aafb60740ad7923f5c77c403dfd5f5
|
7
|
+
data.tar.gz: ede8c61e70832728424903e049bddf45993c211650714f95b3d53c772fd38c9058b62d12eaa06458acc1ef615fd1ceb45adc607ad9aceb27c8935e9ab86cbd8a
|
data/.travis.yml
CHANGED
@@ -8,6 +8,8 @@ deploy:
|
|
8
8
|
api_key:
|
9
9
|
secure: F/wADg5teclCHTkQHVL413+Ow3/XGkAgSMsWJOKKc+vJqdBTckz60U9ghUL36RF1XX7ZcZZ8biUb2hM1mGUPtG/C6uML2tWy2KBbHNf8yleGL9C68cWM+AicQ5yF3Ug8GRKd/XaM4uxh3gCv1QSqOez8beYVIjjw2U+otper/5hTYzgMoKYCSN9+z0bUhrEbEMull5WgG+RF+3ulHThW96fD3LhetnJOEewHhHbmr2fO4QUAF2trLxVKczYeAOf1KrEicbxgTM9CEIgYRyGSTAyfKAKB8fW87hcn2xxJi6OkZOVZSw1HPnhS9GtfMyGAC+wAEY540Q03NHutwvdARZoylbBKnuuGCrZUSv0215ueCrBSvdBR1vLXkiDXpi02LHYo7nrq6sy6e7WC9UDwGTMSDlZDJaSPiXqY7YIAzyapbaCU+FzUVSoN8ZI6cNZx0sKrR/h+sAZlfnVEPG7wkK+jaMh0Lo3JW3Yzn92vsFbq04sirZ82WrxNHaw9vHZd/jO0Xuj8hkF8tI0YDnlm8H41mbFlue2j7eaXFFkU3o6/P1YwdMqu7q/B+MAl41jfgRDsolv8XZ9jlt7QE1C3ESJxI/aJqyALfzTiNZHhHvKckUzHtTWGndeVkm0g1AWP+fbUw1qTjooWriYm7/L6uOBzqmVipzVnV1dDDEiXQGw=
|
10
10
|
gem: nested_lookup
|
11
|
+
on:
|
12
|
+
tags: true
|
11
13
|
|
12
14
|
env:
|
13
15
|
global:
|
data/README.md
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
# NestedLookup [![Build Status](https://travis-ci.com/rameshrvr/nested_lookup.svg?branch=master)](https://travis-ci.com/rameshrvr/nested_lookup) [![Coverage Status](https://codecov.io/gh/rameshrvr/nested_lookup/badge.svg?branch=master)](https://codecov.io/gh/rameshrvr/nested_lookup?branch=master) [![Gem Version](https://badge.fury.io/rb/nested_lookup.svg)](https://badge.fury.io/rb/nested_lookup)
|
2
2
|
|
3
|
-
|
3
|
+
Ruby library which enables key/value lookup, update, delete on deeply nested documents (Arrays and Hashes)
|
4
4
|
|
5
|
-
Features: (document might be Array of Hashes/Hash
|
6
|
-
1. key lookups on deeply nested document.
|
7
|
-
2. fetching all keys from a nested document.
|
8
|
-
3. get the number of occurrences of a key/value from a nested document
|
5
|
+
Features: (document might be Array of Hashes/Hash, Arrays/nested Arrays/nested Hashes etc)
|
6
|
+
1. (nested_lookup) key lookups on deeply nested document.
|
7
|
+
2. (get_all_keys) fetching all keys from a nested document.
|
8
|
+
3. (get_occurrence_of_key/get_occurrence_of_value) get the number of occurrences of a key/value from a nested document
|
9
|
+
4. (nested_get) Get a value in a nested document using its key
|
10
|
+
5. (nested_update) Update a value in a nested document using its key
|
11
|
+
6. (nested_delete) Delete a key->value pair in nested document using its key
|
9
12
|
|
10
13
|
Documents may be built out of nested Hashes and/or Arrays.
|
11
14
|
|
@@ -33,8 +36,6 @@ Or install it yourself as:
|
|
33
36
|
Rameshs-MacBook-Pro:nested_lookup rameshrv$ irb
|
34
37
|
irb(main):001:0> require 'nested_lookup'
|
35
38
|
=> true
|
36
|
-
irb(main):002:0> include NestedLookup
|
37
|
-
=> Object
|
38
39
|
irb(main):003:0> sample_data = {
|
39
40
|
irb(main):004:1* "hardware_details": {
|
40
41
|
irb(main):005:2* "model_name": 'MacBook Pro',
|
@@ -80,8 +81,18 @@ irb(main):036:0> sample_data.get_occurrence_of_value('memory')
|
|
80
81
|
# Get occurrence of value 'Intel Core i7'
|
81
82
|
irb(main):037:0> sample_data.get_occurrence_of_value('Intel Core i7')
|
82
83
|
=> 1
|
83
|
-
|
84
|
-
|
84
|
+
# Get value for the key 'memory', 'l2_cache(per_core)'
|
85
|
+
irb(main):032:0* sample_data.nested_get('memory')
|
86
|
+
=> "16 GB"
|
87
|
+
irb(main):033:0> sample_data.nested_get('l2_cache(per_core)')
|
88
|
+
=> "256 KB"
|
89
|
+
# Delete a key in nested document (key -> 'hardware_details')
|
90
|
+
irb(main):034:0> sample_data.nested_delete('hardware_details')
|
91
|
+
=> {:dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
92
|
+
# Update a key in nested document (key -> 'hardware_details')
|
93
|
+
irb(main):035:0> sample_data.nested_update(key: 'hardware_details', value: 'Test')
|
94
|
+
=> {:hardware_details=>"Test", :dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
95
|
+
irb(main):036:0>
|
85
96
|
```
|
86
97
|
|
87
98
|
### longer tutorial
|
@@ -167,6 +178,53 @@ irb(main):065:0> sample_data.get_occurrence_of_value('256 KB')
|
|
167
178
|
irb(main):072:0>
|
168
179
|
```
|
169
180
|
|
181
|
+
To Get / Delete / Update a key->value pair in a nested document
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
irb(main):001:0> require 'nested_lookup'
|
185
|
+
=> true
|
186
|
+
irb(main):066:0* sample_data.nested_get('os_details')
|
187
|
+
=> {:product_version=>"10.13.6", :build_version=>"17G65"}
|
188
|
+
irb(main):067:0> sample_data.nested_get('memory')
|
189
|
+
=> "16 GB"
|
190
|
+
irb(main):068:0> sample_data.nested_delete('processor_details')
|
191
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
192
|
+
irb(main):069:0> sample_data.nested_update(key: 'processor_details', value: 'Test')
|
193
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>"Test", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
194
|
+
irb(main):070:0>
|
195
|
+
```
|
196
|
+
|
197
|
+
Delete key->value pair (Normal and bang menthod)
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
# Normal method (Returns a document that includes everything but the given key)
|
201
|
+
irb(main):068:0> sample_data.nested_delete('processor_details')
|
202
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
203
|
+
irb(main):071:0* sample_data
|
204
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>[{:processor_name=>"Intel Core i7", :processor_speed=>"2.7 GHz", :core_details=>{:total_numberof_cores=>"4", :"l2_cache(per_core)"=>"256 KB"}}], :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
205
|
+
# Bang method (Replaces the document without the given key)
|
206
|
+
irb(main):074:0* sample_data.nested_delete!('processor_details')
|
207
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
208
|
+
irb(main):075:0> sample_data
|
209
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}}
|
210
|
+
```
|
211
|
+
|
212
|
+
Update value for the given key (Normal and bang method)
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# Normal method (Returns a document that has updated key, value pair)
|
216
|
+
irb(main):109:0* sample_data.nested_update(key: 'processor_details', value: 'Test')
|
217
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>"Test", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}, :dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
218
|
+
irb(main):110:0> sample_data
|
219
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>[{:processor_name=>"Intel Core i7", :processor_speed=>"2.7 GHz", :core_details=>{:total_numberof_cores=>"4", :"l2_cache(per_core)"=>"256 KB"}}], :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}, :dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
220
|
+
# Replaces the document with the updated key, value pair
|
221
|
+
irb(main):114:0* sample_data.nested_update!(key: 'processor_details', value: 'Test')
|
222
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>"Test", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}, :dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
223
|
+
irb(main):115:0> sample_data
|
224
|
+
=> {:hardware_details=>{:model_name=>"MacBook Pro", :processor_details=>"Test", :os_details=>{:product_version=>"10.13.6", :build_version=>"17G65"}, :memory=>"16 GB"}, :dup_hardware_details=>{:model_name=>"MacBook Pro - 1", :os_details=>{:product_version=>"10.14.0", :build_version=>"17G65"}}}
|
225
|
+
|
226
|
+
```
|
227
|
+
|
170
228
|
## Development
|
171
229
|
|
172
230
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/lookup_api.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# Module that holds methods Get, Delete, Update
|
2
|
+
module LookUpApi
|
3
|
+
@result = nil
|
4
|
+
# Private method to lookup inside a document
|
5
|
+
def _get(key:, document:)
|
6
|
+
if document.instance_of? Array
|
7
|
+
document.each do |array_element|
|
8
|
+
_get(key: key, document: array_element)
|
9
|
+
end
|
10
|
+
elsif document.instance_of? Hash
|
11
|
+
document.each do |hash_key, hash_value|
|
12
|
+
@result = hash_value if hash_key.to_s.eql?(key)
|
13
|
+
_get(key: key, document: hash_value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
# Return NIL if the key is not present in the document
|
17
|
+
@result
|
18
|
+
end
|
19
|
+
|
20
|
+
# Method to get the value from a deeply nested document
|
21
|
+
#
|
22
|
+
# @param document Might be Array of Hashes (or)
|
23
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
24
|
+
# @param key: Key to lookup
|
25
|
+
#
|
26
|
+
# @return Value if found else NULL
|
27
|
+
def nested_get(key)
|
28
|
+
_get(key: key, document: dup)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a document that includes everything but the given key.
|
32
|
+
# document = { a: 1, b: 2, c: 3 }
|
33
|
+
# document.nested_delete('c') # => { a: 1, b: 2 }
|
34
|
+
# document # => { a: 1, b: 2, c: 3 }
|
35
|
+
#
|
36
|
+
# @param document Might be Array of Hashes (or)
|
37
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
38
|
+
# @param key: Key to delete
|
39
|
+
#
|
40
|
+
# @return Result document
|
41
|
+
def nested_delete(key)
|
42
|
+
# Create deep copy of the object
|
43
|
+
temp = Marshal.load(Marshal.dump(self))
|
44
|
+
temp.nested_delete!(key)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Replaces the document without the given key.
|
48
|
+
# document = { a: 1, b: 2, c: 3 }
|
49
|
+
# document.nested_delete!('c') # => { a: 1, b: 2 }
|
50
|
+
# document # => { a: 1, b: 2 }
|
51
|
+
#
|
52
|
+
# @param document Might be Array of Hashes (or)
|
53
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
54
|
+
# @param key: Key to delete
|
55
|
+
#
|
56
|
+
# @return self
|
57
|
+
def nested_delete!(key)
|
58
|
+
if instance_of? Array
|
59
|
+
each do |array_element|
|
60
|
+
array_element.nested_delete!(key)
|
61
|
+
end
|
62
|
+
elsif instance_of? Hash
|
63
|
+
each do |hash_key, hash_value|
|
64
|
+
if hash_key.to_s.eql?(key)
|
65
|
+
delete(hash_key)
|
66
|
+
else
|
67
|
+
hash_value.nested_delete!(key)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns a document that has updated key, value pair.
|
75
|
+
# document = { a: 1, b: 2, c: 3 }
|
76
|
+
# document.nested_update(key: 'c', value: 4) # => { a: 1, b: 2 , c: 4}
|
77
|
+
# document # => { a: 1, b: 2, c: 3 }
|
78
|
+
#
|
79
|
+
# @param document Might be Array of Hashes (or)
|
80
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
81
|
+
# @param key: Key to Update
|
82
|
+
# @param value: Value to be updated
|
83
|
+
#
|
84
|
+
# @return Result document
|
85
|
+
def nested_update(key:, value:)
|
86
|
+
# Create deep copy of the object
|
87
|
+
temp = Marshal.load(Marshal.dump(self))
|
88
|
+
temp.nested_update!(key: key, value: value)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Replaces the document with the updated key, value pair.
|
92
|
+
# document = { a: 1, b: 2, c: 3 }
|
93
|
+
# document.nested_update!(key: 'c', value: 4) # => { a: 1, b: 2 , c: 4}
|
94
|
+
# document # => { a: 1, b: 2, c: 4 }
|
95
|
+
#
|
96
|
+
# @param document Might be Array of Hashes (or)
|
97
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
98
|
+
# @param key: Key to Update
|
99
|
+
# @param value: Value to be updated
|
100
|
+
#
|
101
|
+
# @return self
|
102
|
+
def nested_update!(key:, value:)
|
103
|
+
if instance_of? Array
|
104
|
+
each do |array_element|
|
105
|
+
array_element.nested_update!(key: key, value: value)
|
106
|
+
end
|
107
|
+
elsif instance_of? Hash
|
108
|
+
each do |hash_key, hash_value|
|
109
|
+
if hash_key.to_s.eql?(key)
|
110
|
+
self[hash_key] = value
|
111
|
+
else
|
112
|
+
hash_value.nested_update!(key: key, value: value)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
self
|
117
|
+
end
|
118
|
+
end
|
data/lib/nested_lookup.rb
CHANGED
data/nested_lookup.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{Ruby gem which enables key/value lookups on deeply nested documents (Arrays and Hashes)}
|
13
13
|
spec.description = %q{A small ruby library which enables key/value lookups on deeply nested documents (Arrays and Hashes)}
|
14
|
-
spec.homepage =
|
14
|
+
spec.homepage = 'https://rubygems.org/gems/nested_lookup'
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
@@ -21,7 +21,6 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.metadata["homepage_uri"] = spec.homepage
|
23
23
|
spec.metadata["source_code_uri"] = 'https://github.com/rameshrvr/nested_lookup'
|
24
|
-
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
24
|
else
|
26
25
|
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
26
|
"public gem pushes."
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_lookup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramesh RV
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,17 +70,17 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|
73
|
+
- lib/lookup_api.rb
|
73
74
|
- lib/nested_lookup.rb
|
74
75
|
- lib/nested_lookup/version.rb
|
75
76
|
- nested_lookup.gemspec
|
76
|
-
homepage: https://
|
77
|
+
homepage: https://rubygems.org/gems/nested_lookup
|
77
78
|
licenses:
|
78
79
|
- MIT
|
79
80
|
metadata:
|
80
81
|
allowed_push_host: https://rubygems.org
|
81
|
-
homepage_uri: https://
|
82
|
+
homepage_uri: https://rubygems.org/gems/nested_lookup
|
82
83
|
source_code_uri: https://github.com/rameshrvr/nested_lookup
|
83
|
-
changelog_uri: 'TODO: Put your gem''s CHANGELOG.md URL here.'
|
84
84
|
post_install_message:
|
85
85
|
rdoc_options: []
|
86
86
|
require_paths:
|