nested_lookup 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +186 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nested_lookup.rb +170 -0
- data/lib/nested_lookup/version.rb +3 -0
- data/nested_lookup.gemspec +42 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8f0b066a9fadc7710505ae6e7e60b6dbc8c050c
|
4
|
+
data.tar.gz: 15fb97664ee9a09f88a9de46e5b4d085dfb99193
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '049a7d3dec924ce0e2e77f469abd6d7dd123209b06c1e29d735da18bd4fb342b9d81ec97d356e88b4d15d5d0f25d985de9cd903060da48132af380bcf074f14c'
|
7
|
+
data.tar.gz: a2c0cc4ed54fce1c12cb159128cc95c7a04589afe54e2b3b9a8019923f310401096083defaedb71457562177240d3162b4ae7c85c67d1e1b69a0cdc228b60c9c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at rameshrvr@outlook.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ramesh RV
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
# NestedLookup
|
2
|
+
|
3
|
+
A small ruby library which enables key/value lookups on deeply nested documents (Arrays and Hashes)
|
4
|
+
|
5
|
+
Features: (document might be Array of Hashes/Hash or Arrays/nested Arrays/nested Hashes etc)
|
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
|
9
|
+
|
10
|
+
Documents may be built out of dictionaries (dicts) and/or lists.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'nested_lookup'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install nested_lookup
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
### quick tutorial
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Rameshs-MacBook-Pro:nested_lookup rameshrv$ irb
|
34
|
+
irb(main):001:0> require 'nested_lookup'
|
35
|
+
=> true
|
36
|
+
irb(main):002:0> include NestedLookup
|
37
|
+
=> Object
|
38
|
+
irb(main):003:0> sample_data = {
|
39
|
+
irb(main):004:1* "hardware_details": {
|
40
|
+
irb(main):005:2* "model_name": 'MacBook Pro',
|
41
|
+
irb(main):006:2* "processor_details": [
|
42
|
+
irb(main):007:3* {
|
43
|
+
irb(main):008:4* "processor_name": 'Intel Core i7',
|
44
|
+
irb(main):009:4* "processor_speed": '2.7 GHz',
|
45
|
+
irb(main):010:4* "core_details": {
|
46
|
+
irb(main):011:5* "total_numberof_cores": '4',
|
47
|
+
irb(main):012:5* "l2_cache(per_core)": '256 KB'
|
48
|
+
irb(main):013:5> }
|
49
|
+
irb(main):014:4> }
|
50
|
+
irb(main):015:3> ],
|
51
|
+
irb(main):016:2* "os_details": {
|
52
|
+
irb(main):017:3* "product_version": '10.13.6',
|
53
|
+
irb(main):018:3* "build_version": '17G65'
|
54
|
+
irb(main):019:3> },
|
55
|
+
irb(main):020:2* "memory": '16 GB'
|
56
|
+
irb(main):021:2> },
|
57
|
+
irb(main):022:1* "dup_hardware_details": {
|
58
|
+
irb(main):023:2* "model_name": 'MacBook Pro - 1',
|
59
|
+
irb(main):024:2* "os_details": {
|
60
|
+
irb(main):025:3* "product_version": '10.14.0',
|
61
|
+
irb(main):026:3* "build_version": '17G65'
|
62
|
+
irb(main):027:3> },
|
63
|
+
irb(main):028:2* }
|
64
|
+
irb(main):029:1> }
|
65
|
+
=> {: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"}}}
|
66
|
+
irb(main):030:0>
|
67
|
+
irb(main):032:0>
|
68
|
+
# Search for key 'product_version'
|
69
|
+
irb(main):033:0* sample_data.nested_lookup('product_version')
|
70
|
+
=> ["10.13.6", "10.14.0"]
|
71
|
+
# Fetch all keys from the document
|
72
|
+
irb(main):034:0> sample_data.get_all_keys
|
73
|
+
=> ["hardware_details", "model_name", "processor_details", "processor_name", "processor_speed", "core_details", "total_numberof_cores", "l2_cache(per_core)", "os_details", "product_version", "build_version", "memory", "dup_hardware_details", "model_name", "os_details", "product_version", "build_version"]
|
74
|
+
# Get occurrence of key 'model_name' (present in both 'hardware_details', 'dup_hardware_details')
|
75
|
+
irb(main):035:0> sample_data.get_occurrence_of_key('model_name')
|
76
|
+
=> 2
|
77
|
+
# Get occurrence of value 'memory' (It is actually a key, should return 0)
|
78
|
+
irb(main):036:0> sample_data.get_occurrence_of_value('memory')
|
79
|
+
=> 0
|
80
|
+
# Get occurrence of value 'Intel Core i7'
|
81
|
+
irb(main):037:0> sample_data.get_occurrence_of_value('Intel Core i7')
|
82
|
+
=> 1
|
83
|
+
irb(main):038:0>
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
### longer tutorial
|
88
|
+
|
89
|
+
You may control the nested_lookup method's behavior by passing some optional arguments.
|
90
|
+
|
91
|
+
1. wild (defaults to `False`):
|
92
|
+
|
93
|
+
- if `wild` is `True`, treat the given `key` as a case insensitive
|
94
|
+
substring when performing lookups.
|
95
|
+
|
96
|
+
2. with_keys (defaults to `False`):
|
97
|
+
|
98
|
+
- if `with_keys` is `True`, return a Hash of all matched keys
|
99
|
+
and an Array of values.
|
100
|
+
|
101
|
+
For example, given the following document:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
sample_data = {
|
105
|
+
"hardware_details": {
|
106
|
+
"model_name": 'MacBook Pro',
|
107
|
+
"processor_details": [
|
108
|
+
{
|
109
|
+
"processor_name": 'Intel Core i7',
|
110
|
+
"processor_speed": '2.7 GHz',
|
111
|
+
"core_details": {
|
112
|
+
"total_numberof_cores": '4',
|
113
|
+
"l2_cache(per_core)": '256 KB'
|
114
|
+
}
|
115
|
+
}
|
116
|
+
],
|
117
|
+
"os_details": {
|
118
|
+
"product_version": '10.13.6',
|
119
|
+
"build_version": '17G65'
|
120
|
+
},
|
121
|
+
"memory": '16 GB'
|
122
|
+
}
|
123
|
+
}
|
124
|
+
```
|
125
|
+
|
126
|
+
We could act `wild` and find all the version like this:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
irb(main):069:0* sample_data.nested_lookup('version', wild: true)
|
130
|
+
=> ["10.13.6", "17G65"]
|
131
|
+
irb(main):072:0>
|
132
|
+
```
|
133
|
+
|
134
|
+
Additionally, if you also needed the matched key names, you could do this:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
irb(main):071:0* sample_data.nested_lookup('version', wild: true, with_keys: true)
|
138
|
+
=> {"product_version"=>["10.13.6"], "build_version"=>["17G65"]}
|
139
|
+
irb(main):073:0* sample_data.nested_lookup('product_version', with_keys: true)
|
140
|
+
=> {"product_version"=>["10.13.6"]}
|
141
|
+
irb(main):074:0>
|
142
|
+
```
|
143
|
+
|
144
|
+
To get a list of every nested key in a document, run this:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
irb(main):060:0* require 'nested_lookup'
|
148
|
+
=> false
|
149
|
+
irb(main):061:0> include NestedLookup
|
150
|
+
=> Object
|
151
|
+
irb(main):062:0> sample_data.get_all_keys
|
152
|
+
=> ["hardware_details", "model_name", "processor_details", "processor_name", "processor_speed", "core_details", "total_numberof_cores", "l2_cache(per_core)", "os_details", "product_version", "build_version", "memory"]
|
153
|
+
irb(main):063:0>
|
154
|
+
```
|
155
|
+
|
156
|
+
To get the number of occurrence of the given key/value
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
irb(main):060:0* require 'nested_lookup'
|
160
|
+
=> false
|
161
|
+
irb(main):061:0> include NestedLookup
|
162
|
+
=> Object
|
163
|
+
irb(main):064:0* sample_data.get_occurrence_of_key('processor_speed')
|
164
|
+
=> 1
|
165
|
+
irb(main):065:0> sample_data.get_occurrence_of_value('256 KB')
|
166
|
+
=> 1
|
167
|
+
irb(main):072:0>
|
168
|
+
```
|
169
|
+
|
170
|
+
## Development
|
171
|
+
|
172
|
+
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.
|
173
|
+
|
174
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
175
|
+
|
176
|
+
## Contributing
|
177
|
+
|
178
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rameshrvr/nested_lookup. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
179
|
+
|
180
|
+
## License
|
181
|
+
|
182
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
183
|
+
|
184
|
+
## Code of Conduct
|
185
|
+
|
186
|
+
Everyone interacting in the NestedLookup project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rameshrvr/nested_lookup/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nested_lookup"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
require_relative 'nested_lookup/version'
|
2
|
+
|
3
|
+
# module responsible for holding nested_lookup methods
|
4
|
+
module NestedLookup
|
5
|
+
# Method to lookup a key in a deeply nested document
|
6
|
+
#
|
7
|
+
# @param document: Might be Array of Hashes (or)
|
8
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
9
|
+
# @param key: Key to lookup
|
10
|
+
# @param wild: Default disabled, enable it to
|
11
|
+
# perform case insensitive lookup
|
12
|
+
# @param with_keys: Set to true will get the results with keys
|
13
|
+
#
|
14
|
+
# @return (with_keys set to false) Array of values
|
15
|
+
# (with_keys set to true) Hash of keys and values
|
16
|
+
def nested_lookup(
|
17
|
+
key, wild: false, with_keys: false
|
18
|
+
)
|
19
|
+
if with_keys
|
20
|
+
result_hash = {}
|
21
|
+
_nested_lookup(
|
22
|
+
key: key, document: self,
|
23
|
+
wild: wild, with_keys: with_keys
|
24
|
+
).each do |temp_hash|
|
25
|
+
temp_hash.each do |hash_key, hash_value|
|
26
|
+
if !(result_hash[hash_key.to_s])
|
27
|
+
result_hash[hash_key.to_s] = [hash_value]
|
28
|
+
else
|
29
|
+
result_hash[hash_key.to_s].push(hash_value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return result_hash
|
34
|
+
end
|
35
|
+
result_array = _nested_lookup(
|
36
|
+
key: key, document: self, wild: wild,
|
37
|
+
with_keys: with_keys
|
38
|
+
)
|
39
|
+
result_array
|
40
|
+
end
|
41
|
+
|
42
|
+
# Private method to lookup inside a document
|
43
|
+
def _nested_lookup(
|
44
|
+
key:, document:, wild: false,
|
45
|
+
with_keys: false, result: []
|
46
|
+
)
|
47
|
+
if document.instance_of? Array
|
48
|
+
document.each do |array_element|
|
49
|
+
_nested_lookup(
|
50
|
+
key: key, document: array_element,
|
51
|
+
wild: wild, with_keys: with_keys,
|
52
|
+
result: result
|
53
|
+
)
|
54
|
+
end
|
55
|
+
elsif document.instance_of? Hash
|
56
|
+
document.each do |hash_key, hash_value|
|
57
|
+
if hash_key.to_s.eql?(key) || (
|
58
|
+
wild && hash_key.to_s.downcase.include?(key.downcase)
|
59
|
+
)
|
60
|
+
if with_keys
|
61
|
+
result.push("#{hash_key}": hash_value)
|
62
|
+
else
|
63
|
+
result.push(hash_value)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
if hash_value.instance_of? Array
|
67
|
+
_nested_lookup(
|
68
|
+
key: key, document: hash_value,
|
69
|
+
wild: wild, with_keys: with_keys,
|
70
|
+
result: result
|
71
|
+
)
|
72
|
+
elsif hash_value.instance_of? Hash
|
73
|
+
_nested_lookup(
|
74
|
+
key: key, document: hash_value,
|
75
|
+
wild: wild, with_keys: with_keys,
|
76
|
+
result: result
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
result
|
82
|
+
end
|
83
|
+
|
84
|
+
# Method to get all keys from a nested dictionary as a List
|
85
|
+
#
|
86
|
+
# @param document: Might be Array of Hashes (or)
|
87
|
+
# Hash of Arrays (or) Hash of array of hash etc...
|
88
|
+
#
|
89
|
+
# @return Array of keys in the document
|
90
|
+
#
|
91
|
+
# Usage:
|
92
|
+
# => document.get_all_keys() (or) document.get_all_keys
|
93
|
+
def get_all_keys(
|
94
|
+
result_array: []
|
95
|
+
)
|
96
|
+
if instance_of? Array
|
97
|
+
each do |_element|
|
98
|
+
_element.get_all_keys(result_array: result_array)
|
99
|
+
end
|
100
|
+
elsif instance_of? Hash
|
101
|
+
each do |key, value|
|
102
|
+
result_array.push(key.to_s)
|
103
|
+
if value.instance_of? Hash
|
104
|
+
value.get_all_keys(result_array: result_array)
|
105
|
+
elsif value.instance_of? Array
|
106
|
+
value.each do |arr_element|
|
107
|
+
arr_element.get_all_keys(result_array: result_array)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
result_array
|
113
|
+
end
|
114
|
+
|
115
|
+
# Method to get occurrence of key in a deeply nested
|
116
|
+
# document
|
117
|
+
#
|
118
|
+
# @param document: Deeply nested document
|
119
|
+
# @param key: key name to look into the document
|
120
|
+
#
|
121
|
+
# @return Number of occurrence (Integer)
|
122
|
+
def get_occurrence_of_key(key)
|
123
|
+
_get_occurrence(document: self, item: 'key', keyword: key)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Method to get occurrence of value in a deeply nested
|
127
|
+
# document
|
128
|
+
#
|
129
|
+
# @param document: Deeply nested document
|
130
|
+
# @param value: value name to look into the document
|
131
|
+
#
|
132
|
+
# @return Number of occurrence (Integer)
|
133
|
+
def get_occurrence_of_value(value)
|
134
|
+
_get_occurrence(document: self, item: 'value', keyword: value)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Private method to get occurrence of key or value in deeply
|
138
|
+
# nested document
|
139
|
+
def _get_occurrence(
|
140
|
+
document:, item:, keyword:
|
141
|
+
)
|
142
|
+
@result = 0
|
143
|
+
@item = item
|
144
|
+
@keyword = keyword
|
145
|
+
def recrusion(document:)
|
146
|
+
if document.instance_of? Hash
|
147
|
+
if @item == 'key'
|
148
|
+
@result += 1 if document.key?(@keyword.to_sym)
|
149
|
+
elsif document.value?(@keyword)
|
150
|
+
@result += document.values.count(@keyword)
|
151
|
+
end
|
152
|
+
document.each do |_key, value|
|
153
|
+
if value.instance_of? Hash
|
154
|
+
recrusion(document: value)
|
155
|
+
elsif value.instance_of? Array
|
156
|
+
value.each do |element|
|
157
|
+
recrusion(document: element)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
elsif document.instance_of? Array
|
162
|
+
document.each do |element|
|
163
|
+
recrusion(document: element)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
recrusion(document: document)
|
168
|
+
@result
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require_relative "lib/nested_lookup/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nested_lookup"
|
8
|
+
spec.version = NestedLookup::VERSION
|
9
|
+
spec.authors = ["Ramesh RV"]
|
10
|
+
spec.email = ["rameshrvr@outlook.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby gem which enables key/value lookups on deeply nested documents (Arrays and Hashes)}
|
13
|
+
spec.description = %q{A small ruby library which enables key/value lookups on deeply nested documents (Arrays and Hashes)}
|
14
|
+
spec.homepage = "https://github.com/rameshrvr/nested_lookup"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
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
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_lookup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ramesh RV
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-29 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: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A small ruby library which enables key/value lookups on deeply nested
|
56
|
+
documents (Arrays and Hashes)
|
57
|
+
email:
|
58
|
+
- rameshrvr@outlook.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/nested_lookup.rb
|
74
|
+
- lib/nested_lookup/version.rb
|
75
|
+
- nested_lookup.gemspec
|
76
|
+
homepage: https://github.com/rameshrvr/nested_lookup
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
homepage_uri: https://github.com/rameshrvr/nested_lookup
|
82
|
+
source_code_uri: https://github.com/rameshrvr/nested_lookup
|
83
|
+
changelog_uri: 'TODO: Put your gem''s CHANGELOG.md URL here.'
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.2.3
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Ruby gem which enables key/value lookups on deeply nested documents (Arrays
|
104
|
+
and Hashes)
|
105
|
+
test_files: []
|