nested_hash_helper 1.0.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 +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +118 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/nested_hash_helper/version.rb +3 -0
- data/lib/nested_hash_helper.rb +82 -0
- data/nested_hash_helper.gemspec +28 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e7f67ec121f9910b50c6a7f7b61941bcd6e1784e
|
4
|
+
data.tar.gz: a0b23de228d5f145b977552ffdcc2286e93abca8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83c7ec8e9a4f938ac89a6777d1d819e5eeffaa15b0968a4b2a8226fe2d974662b1d4cc59992e51092df31bd3d968e142eb0a720781454c45de344c639eaae167
|
7
|
+
data.tar.gz: 96ad4403b6123c37880e68f61db2d480792e33e77674ba73e245aa43afcf38115dfa3c8a3e20d2d9815da52229d8360375928e20b420db80abc331ac313babb4
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 vivek n
|
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,118 @@
|
|
1
|
+
# NestedHashHelper
|
2
|
+
|
3
|
+
NestedHashHelper gem's aim is to make your life easier in dealing with nested hashes . If you have been working with Rails , You must have noted that You are dealing with nested hashes ( like params from the front end) a lot .
|
4
|
+
|
5
|
+
There are various methods added in the official Ruby code to handle nested hashes , but still it lacks lots of common and useful methods . NestedHashHelper aims to help you with those , so that you can deal with hashes easily in your application .
|
6
|
+
|
7
|
+
Development has just begun and is active and we will fix bugs , enhance functionality of existing methods and develop new methods in the upcoming version.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
1) deep_except(*excluded_keys)
|
12
|
+
|
13
|
+
This method extends the functionality of except method of ruby hash to nested hash. pass any number of keys to delete them from the hash . Just pass the immediate key and not the entire key path in the hash .
|
14
|
+
|
15
|
+
Syntax
|
16
|
+
|
17
|
+
a = {:name => "vivek" , :age => 22}
|
18
|
+
|
19
|
+
a.deep_except(:name)
|
20
|
+
|
21
|
+
output ==> {:age => 22}
|
22
|
+
|
23
|
+
a = {:user => {:name => "vivek" , :age => 22 , :phone => 3333}}
|
24
|
+
|
25
|
+
a.deep_except(:phone , :age)
|
26
|
+
|
27
|
+
output ==> {:user => {:name => "vivek"}}
|
28
|
+
|
29
|
+
2) deep_delete_empty
|
30
|
+
|
31
|
+
This method deletes all the keys whose value is either empty or nil .
|
32
|
+
|
33
|
+
Syntax
|
34
|
+
|
35
|
+
a = {:user => {:name => "" , :age => 22 , :phone => 3333}}
|
36
|
+
|
37
|
+
a.deep_delete_empty
|
38
|
+
|
39
|
+
output ==> {:user => {:age => 22 , :phone => 3333}}
|
40
|
+
|
41
|
+
3) find_depth
|
42
|
+
|
43
|
+
This method finds the depth of your nested hash.
|
44
|
+
|
45
|
+
Syntax
|
46
|
+
|
47
|
+
a = {:user => {:name => {:last_name => "Vivek"}}}
|
48
|
+
|
49
|
+
a.find_depth
|
50
|
+
|
51
|
+
output ==> 3
|
52
|
+
|
53
|
+
4) find_deep_intersection
|
54
|
+
|
55
|
+
This method returns intersection of two nested Hashes.
|
56
|
+
|
57
|
+
Syntax
|
58
|
+
|
59
|
+
a = {:user => {:name => "vivek" , :age => 22 , :phone => 3333}}
|
60
|
+
|
61
|
+
b = {:user => {:name => "rakesh" , :age => 22 , :phone => 3333}}
|
62
|
+
|
63
|
+
a.find_deep_intersection(b)
|
64
|
+
|
65
|
+
output ==> {:user => {:age => 22 , :phone => 3333}}
|
66
|
+
|
67
|
+
5) find_deep_keys
|
68
|
+
|
69
|
+
This method return all the parent keys of the corresponding value.
|
70
|
+
|
71
|
+
Syntax
|
72
|
+
|
73
|
+
a = {:gh => {:jj => {:pop => "bbb" , :olo => "ooooo"}} , :pp => "ooooo"}
|
74
|
+
|
75
|
+
a.find_deep_keys("ooooo")
|
76
|
+
|
77
|
+
output ==> [:gh , :jj , :olo]
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
## Installation
|
83
|
+
|
84
|
+
Add this line to your application's Gemfile:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
gem 'nested_hash_helper'
|
88
|
+
```
|
89
|
+
|
90
|
+
And then execute:
|
91
|
+
|
92
|
+
$ bundle
|
93
|
+
|
94
|
+
Or install it yourself as:
|
95
|
+
|
96
|
+
$ gem install nested_hash_helper
|
97
|
+
|
98
|
+
## Usage
|
99
|
+
|
100
|
+
TODO: Write usage instructions here
|
101
|
+
|
102
|
+
## Development
|
103
|
+
|
104
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
105
|
+
|
106
|
+
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).
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
Do Contribute if you come across any bugs / any important feature you feel is essential.
|
111
|
+
|
112
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nested_hash_helper.
|
113
|
+
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
118
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nested_hash_helper"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "nested_hash_helper/version"
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Your code goes here...
|
5
|
+
def deep_except(*excluded_keys)
|
6
|
+
current_class = self.class
|
7
|
+
self.each do |current_keys , current_value|
|
8
|
+
if excluded_keys.include?(current_keys)
|
9
|
+
self.delete(current_keys)
|
10
|
+
next
|
11
|
+
end
|
12
|
+
if current_value.is_a?(current_class)
|
13
|
+
current_value.deep_except(*excluded_keys)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def deep_delete_empty
|
19
|
+
current_class = self.class
|
20
|
+
self.each do |current_keys , current_value|
|
21
|
+
if current_value.nil? || current_value.empty?
|
22
|
+
self.delete(current_keys)
|
23
|
+
next
|
24
|
+
end
|
25
|
+
if current_value.is_a?(current_class)
|
26
|
+
current_value.deep_delete_empty
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_depth
|
32
|
+
maxx_depth = 1
|
33
|
+
current_depth = 1
|
34
|
+
self._find_depth(maxx_depth , current_depth)
|
35
|
+
end
|
36
|
+
|
37
|
+
def _find_depth(maxx_depth , current_depth)
|
38
|
+
current_class = self.class
|
39
|
+
self.each do |current_keys , current_value |
|
40
|
+
if current_value.is_a?(current_class)
|
41
|
+
maxx_depth = current_value._find_depth(maxx_depth , current_depth + 1)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
maxx_depth = (maxx_depth > current_depth)? maxx_depth : current_depth
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_deep_intersection(compare_hash)
|
48
|
+
current_class = self.class
|
49
|
+
final_hash = current_class.new
|
50
|
+
self.each do |current_keys , current_value |
|
51
|
+
if compare_hash.has_key?(current_keys)
|
52
|
+
if current_value.is_a?(current_class) && compare_hash.fetch(current_keys).is_a?(current_class)
|
53
|
+
final_hash[current_keys] = current_value.find_deep_intersection(compare_hash.fetch(current_keys))
|
54
|
+
elsif !current_value.is_a?(current_class) && !compare_hash.fetch(current_keys).is_a?(current_class) && (current_value == compare_hash.fetch(current_keys) )
|
55
|
+
final_hash[current_keys] = current_value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
final_hash
|
60
|
+
end
|
61
|
+
|
62
|
+
def find_deep_keys(value)
|
63
|
+
current_class = self.class
|
64
|
+
deep_keys = []
|
65
|
+
self.each do |current_keys , current_value|
|
66
|
+
if !current_value.is_a?(current_class) && current_value == value
|
67
|
+
deep_keys = deep_keys.push(current_keys)
|
68
|
+
elsif current_value.is_a?(current_class)
|
69
|
+
future_deep_keys = current_value.find_deep_keys(value)
|
70
|
+
if future_deep_keys.size >= 1
|
71
|
+
deep_keys.push(current_keys)
|
72
|
+
deep_keys += future_deep_keys
|
73
|
+
return deep_keys
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
deep_keys
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nested_hash_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nested_hash_helper"
|
8
|
+
spec.version = NestedHashHelper::VERSION
|
9
|
+
spec.authors = ["vivek n"]
|
10
|
+
spec.email = ["vivekn@freshdesk.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{NestedHashHelper to deal with nested Hashes.}
|
13
|
+
spec.description = %q{This gem eases your life in dealing with nested Hashes While building Rails apps.}
|
14
|
+
spec.homepage = "https://github.com/vivek3894/nested_hash_helper"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "minitest"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_hash_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vivek n
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-06 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: minitest
|
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
|
+
description: This gem eases your life in dealing with nested Hashes While building
|
56
|
+
Rails apps.
|
57
|
+
email:
|
58
|
+
- vivekn@freshdesk.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/nested_hash_helper.rb
|
72
|
+
- lib/nested_hash_helper/version.rb
|
73
|
+
- nested_hash_helper.gemspec
|
74
|
+
homepage: https://github.com/vivek3894/nested_hash_helper
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: NestedHashHelper to deal with nested Hashes.
|
98
|
+
test_files: []
|