hash_dig_and_collect 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: fad901b18c1025db61c628891834e39f3364bdd9
4
+ data.tar.gz: 48948fabfe8ee7a3488a276787d87a6e7848a55c
5
+ SHA512:
6
+ metadata.gz: 537012f52a0dfcda47ed6e30fd34c57b98b438a4b65019ccad1418f4f358a5ab4b899da6623748f70094936884159e9c6db2b5384ae6aaf7fe4a732bec7d5dff
7
+ data.tar.gz: cf99df1ab898d37dbd826e03f9fdfa8c6bd35555622c17479ad4f38ad4f387c1874176ca96183de7be3b77c8ddbea5dbbe6484ae17a0e5651ef407aef41cc9dc
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_dig.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hash_dig_and_collect (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.1)
10
+ diff-lcs (1.2.5)
11
+ method_source (0.8.2)
12
+ pry (0.10.4)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.8.1)
15
+ slop (~> 3.4)
16
+ pry-nav (0.2.4)
17
+ pry (>= 0.9.10, < 0.11.0)
18
+ rspec (3.4.0)
19
+ rspec-core (~> 3.4.0)
20
+ rspec-expectations (~> 3.4.0)
21
+ rspec-mocks (~> 3.4.0)
22
+ rspec-core (3.4.4)
23
+ rspec-support (~> 3.4.0)
24
+ rspec-expectations (3.4.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.4.0)
27
+ rspec-mocks (3.4.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.4.0)
30
+ rspec-support (3.4.1)
31
+ slop (3.6.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 1.7)
38
+ hash_dig_and_collect!
39
+ pry
40
+ pry-nav
41
+ rspec
42
+
43
+ BUNDLED WITH
44
+ 1.13.6
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Colin Kelley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'hash_dig_and_collect'
7
+ ```
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install hash_dig_and_collect
16
+
17
+ ## Usage
18
+
19
+ Hash#dig in Ruby 2.3 completely changed the way I navigate deeply nested hashes. This repo contains an implementation of
20
+ Hash#dig_and_collect, a new utility method to navigate even more complex nested hashes without messing up your code.
21
+
22
+ See the details of the why in this [Blog post](http://www.alfredo.motta.name/making-ruby-hashdig-even-more-awesome-introducing-hashdig_and_collect).
23
+
24
+ ```
25
+ client_with_many_addresses = {
26
+ details: {
27
+ first_name: "Florentino",
28
+ last_name: "Perez"
29
+ },
30
+ addresses: [
31
+ {
32
+ type: "home",
33
+ postcode: "SE1 9SG",
34
+ street: "London Bridge St",
35
+ number: 32,
36
+ city: "London",
37
+ location: {
38
+ latitude: 51.504382,
39
+ longitude: -0.086279
40
+ }
41
+ },
42
+ {
43
+ type: "office",
44
+ postcode: "SW1A 1AA",
45
+ street: "Buckingham Palace Road",
46
+ number: nil,
47
+ city: "London",
48
+ location: {
49
+ latitude: 51.5013673,
50
+ longitude: -0.1440787
51
+ }
52
+ }
53
+ ]
54
+ }
55
+
56
+ client_with_many_addresses.dig_and_collect(:addresses, :location, :latitude)
57
+ # 51.5013673
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it ( https://github.com/mottalrd/hash_dig_and_collect/fork )
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create a new Pull Request
@@ -0,0 +1,81 @@
1
+ # This example demonstrates how complicated it could
2
+ # get without dig_and_collect and why it is a good idea
3
+ # to have it
4
+
5
+ require_relative 'lib/hash'
6
+
7
+ client_with_no_addresses = {
8
+ details: {
9
+ first_name: "Florentino",
10
+ last_name: "Perez"
11
+ },
12
+ addresses: nil
13
+ }
14
+
15
+ client_with_one_address = {
16
+ details: {
17
+ first_name: "Florentino",
18
+ last_name: "Perez"
19
+ },
20
+ addresses: {
21
+ type: "home",
22
+ postcode: "SE1 9SG",
23
+ street: "London Bridge St",
24
+ number: 32,
25
+ city: "London",
26
+ location: {
27
+ latitude: 51.504382,
28
+ longitude: -0.086279
29
+ }
30
+ }
31
+ }
32
+
33
+ client_with_many_addresses = {
34
+ details: {
35
+ first_name: "Florentino",
36
+ last_name: "Perez"
37
+ },
38
+ addresses: [
39
+ {
40
+ type: "home",
41
+ postcode: "SE1 9SG",
42
+ street: "London Bridge St",
43
+ number: 32,
44
+ city: "London",
45
+ location: {
46
+ latitude: 51.504382,
47
+ longitude: -0.086279
48
+ }
49
+ },
50
+ {
51
+ type: "office",
52
+ postcode: "SW1A 1AA",
53
+ street: "Buckingham Palace Road",
54
+ number: nil,
55
+ city: "London",
56
+ location: {
57
+ latitude: 51.5013673,
58
+ longitude: -0.1440787
59
+ }
60
+ }
61
+ ]
62
+ }
63
+
64
+ def get_offices_latitudes_for(client)
65
+ addresses = client[:addresses]
66
+ return [] if addresses.nil?
67
+
68
+ if addresses.is_a? Hash
69
+ [ addresses.dig(:location, :latitude) ]
70
+ else
71
+ addresses.map { |address| address.dig(:location, :latitude) }
72
+ end
73
+ end
74
+
75
+ puts get_offices_latitudes_for(client_with_no_addresses).join(", ")
76
+ puts get_offices_latitudes_for(client_with_one_address).join(", ")
77
+ puts get_offices_latitudes_for(client_with_many_addresses).join(", ")
78
+
79
+ puts client_with_no_addresses.dig_and_collect(:addresses, :location, :latitude)
80
+ puts client_with_one_address.dig_and_collect(:addresses, :location, :latitude)
81
+ puts client_with_many_addresses.dig_and_collect(:addresses, :location, :latitude)
@@ -0,0 +1,35 @@
1
+ # This example demonstrates the limitations of dig when
2
+ # you have Arrays in your nested Hash
3
+
4
+ client_with_many_addresses = {
5
+ details: {
6
+ first_name: "Florentino",
7
+ last_name: "Perez"
8
+ },
9
+ addresses: [
10
+ {
11
+ type: "home",
12
+ postcode: "SE1 9SG",
13
+ street: "London Bridge St",
14
+ number: 32,
15
+ city: "London",
16
+ location: {
17
+ latitude: 51.504382,
18
+ longitude: -0.086279
19
+ }
20
+ },
21
+ {
22
+ type: "office",
23
+ postcode: "SW1A 1AA",
24
+ street: "Buckingham Palace Road",
25
+ number: nil,
26
+ city: "London",
27
+ location: {
28
+ latitude: 51.5013673,
29
+ longitude: -0.1440787
30
+ }
31
+ }
32
+ ]
33
+ }
34
+
35
+ puts client_with_many_addresses.dig(:addresses, :location, :latitude)
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'hash_dig_and_collect/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "hash_dig_and_collect"
7
+ spec.version = HashDigAndCollect::VERSION
8
+ spec.authors = ["Alfredo Motta"]
9
+ spec.summary = %q{Utility method build on top of Ruby dig to navigate hashes mixed up with arrays.}
10
+ spec.homepage = "https://github.com/mottalrd/hash_dig_and_collect"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.7"
19
+ spec.add_development_dependency "rspec"
20
+ spec.add_development_dependency "pry"
21
+ spec.add_development_dependency "pry-nav"
22
+ end
@@ -0,0 +1,23 @@
1
+ module HashDigAndCollect
2
+ def dig_and_collect *keys
3
+ keys = keys.dup
4
+
5
+ next_key = keys.shift
6
+ return [] unless self.has_key? next_key
7
+
8
+ next_val = self[next_key]
9
+ return [next_val] if keys.empty?
10
+
11
+ return next_val.dig_and_collect(*keys) if next_val.is_a? Hash
12
+
13
+ return [] unless next_val.is_a? Array
14
+ next_val.each_with_object([]) do |v, result|
15
+ inner = v.dig_and_collect(*keys)
16
+ result.concat inner
17
+ end
18
+ end
19
+ end
20
+
21
+ class Hash
22
+ include HashDigAndCollect
23
+ end
@@ -0,0 +1,3 @@
1
+ module HashDigAndCollect
2
+ VERSION = "0.0.1"
3
+ end
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+ require_relative '../lib/hash_dig_and_collect'
3
+
4
+ RSpec.describe HashDigAndCollect do
5
+ describe '#dig_and_collect' do
6
+ context 'when key is present' do
7
+ let(:values) { [nil, '', []] }
8
+
9
+ context 'when nil' do
10
+ subject do { a: nil } end
11
+ it 'returns the value' do
12
+ expect(subject.dig_and_collect(:a)).to eq([ nil ])
13
+ end
14
+ end
15
+
16
+ context 'when empty string' do
17
+ subject do { a: '' } end
18
+ it 'returns the value' do
19
+ expect(subject.dig_and_collect(:a)).to eq([ '' ])
20
+ end
21
+ end
22
+
23
+ context 'when empty array' do
24
+ subject do { a: [] } end
25
+ it 'returns the value' do
26
+ expect(subject.dig_and_collect(:a)).to eq([ [] ])
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'when no arrays are present' do
32
+ subject do
33
+ {
34
+ a: { b: { c: 'ok' } }
35
+ }
36
+ end
37
+
38
+ it 'collects the values' do
39
+ expect(subject.dig_and_collect(:a, :b, :c)).to eq(['ok'])
40
+ end
41
+
42
+ context 'and some key is missing' do
43
+ it 'returns an empty array' do
44
+ expect(subject.dig_and_collect(:a, :zzz, :c)).to eq([])
45
+ end
46
+ end
47
+ end
48
+
49
+ context 'when arrays are present' do
50
+ subject do
51
+ {
52
+ a: [
53
+ {
54
+ b: {
55
+ c: [
56
+ { d: { e: 'ok' } },
57
+ { d: { e: 'ok' } }
58
+ ]
59
+ }
60
+ },
61
+ {
62
+ b: {
63
+ c: [
64
+ { d: { e: 'ok' } },
65
+ { d: { e: 'ok' } }
66
+ ]
67
+ }
68
+ }
69
+ ]
70
+ }
71
+ end
72
+
73
+ it 'returns an array with the collected values' do
74
+ expect(subject.dig_and_collect(:a, :b, :c, :d, :e)).to eq(['ok', 'ok', 'ok', 'ok'])
75
+ end
76
+
77
+ context 'some key in the path is missing' do
78
+ it 'default to empty set' do
79
+ expect(subject.dig_and_collect(:a, :zz, :c, :d, :e)).to eq([])
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'when objects are present and we miss the path' do
85
+ subject do
86
+ {
87
+ a: { b: Object.new }
88
+ }
89
+ end
90
+
91
+ it 'returns an empty array' do
92
+ expect(subject.dig_and_collect(:a, :b, :c)).to eq([])
93
+ end
94
+ end
95
+
96
+ context 'when objects are present and there is a path' do
97
+ subject do
98
+ {
99
+ a: [
100
+ { b: Object.new },
101
+ { b: { c: 'ok' } }
102
+ ]
103
+ }
104
+ end
105
+
106
+ it 'returns an empty array' do
107
+ expect(subject.dig_and_collect(:a, :b, :c)).to eq(['ok'])
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,12 @@
1
+ require 'pry'
2
+ require 'pry-nav'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |expectations|
6
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7
+ end
8
+
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_dig_and_collect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alfredo Motta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-04 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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
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: pry-nav
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
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - examples/dig_and_collect_demonstration.rb
82
+ - examples/dig_limitations.rb
83
+ - hash_dig_and_collect.gemspec
84
+ - lib/hash_dig_and_collect.rb
85
+ - lib/hash_dig_and_collect/version.rb
86
+ - spec/hash_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/mottalrd/hash_dig_and_collect
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Utility method build on top of Ruby dig to navigate hashes mixed up with
112
+ arrays.
113
+ test_files:
114
+ - spec/hash_spec.rb
115
+ - spec/spec_helper.rb