ruby_dig2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +8 -0
- data/lib/ruby_dig2.rb +26 -0
- data/lib/ruby_dig2/version.rb +3 -0
- data/ruby_dig2.gemspec +23 -0
- data/test/ruby_dig2_test.rb +88 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e6979c486db18c2597b6d41471e387bf482a6d1
|
4
|
+
data.tar.gz: da672e7a1374ff3978f82f857b813dce51865e72
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61f7272f33bc0689ddba786fdea5f360915444f33e013d3979a578887b3a2787c28aece2ae2ca4aa888fa6e0d0e775075d96b101cee72703647329df8f535a77
|
7
|
+
data.tar.gz: fbf7277e84cbc700dbc42779ff343a748c5bb9ca2334803c9bbbf0c6a499a14298647c0ad7917ce1f44692260c98a31e647971e07c441452eb10e276d465529d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
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,59 @@
|
|
1
|
+
# RubyDig2
|
2
|
+
|
3
|
+
`Hash#dig2` and `Array#dig2` enhances Rubys `Hash#dig` and `Array#dig` methods functionality.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ruby_dig2'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ruby_dig2
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'ruby_dig2'
|
25
|
+
|
26
|
+
response = {mom: {first: "Marge", last: "Bouvier"},
|
27
|
+
dad: {first: "Homer", last: "Simpson"},
|
28
|
+
kids: [
|
29
|
+
{first: "Bart", last: "Simpson"},
|
30
|
+
{first: "Lisa", last: "Simpson"}
|
31
|
+
]}
|
32
|
+
|
33
|
+
response.dig2(:kids, 1, :first)
|
34
|
+
# => "Lisa"
|
35
|
+
```
|
36
|
+
|
37
|
+
Or finding the first detected element in array of hashes.
|
38
|
+
|
39
|
+
```
|
40
|
+
require 'ruby_dig2'
|
41
|
+
|
42
|
+
response = {mom: {first: "Marge", last: "Bouvier"},
|
43
|
+
dad: {first: "Homer", last: "Simpson"},
|
44
|
+
kids: [
|
45
|
+
{first: "Bart", last: "Simpson"},
|
46
|
+
{first: "Lisa", last: "Simpson"}
|
47
|
+
]}
|
48
|
+
|
49
|
+
response.dig2(:kids, {first: 'Lisa'}, :last)
|
50
|
+
# => "Simpson"
|
51
|
+
```
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it ( https://github.com/leoc/ruby_dig2/fork )
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/ruby_dig2.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module RubyDig2
|
2
|
+
def dig2(key, *rest)
|
3
|
+
value =
|
4
|
+
if key.is_a?(Hash)
|
5
|
+
detect do |e|
|
6
|
+
key.all? { |k, v| e[k] == v }
|
7
|
+
end
|
8
|
+
else
|
9
|
+
self[key]
|
10
|
+
end
|
11
|
+
if value.nil? || rest.empty?
|
12
|
+
value
|
13
|
+
elsif value.respond_to?(:dig2)
|
14
|
+
value.dig2(*rest)
|
15
|
+
else
|
16
|
+
fail TypeError, "#{value.class} does not have #dig2 method"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Array
|
22
|
+
include RubyDig2
|
23
|
+
end
|
24
|
+
class Hash
|
25
|
+
include RubyDig2
|
26
|
+
end
|
data/ruby_dig2.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruby_dig2/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ruby_dig2'
|
8
|
+
spec.version = RubyDig2::VERSION
|
9
|
+
spec.authors = ['Colin Kelley', 'Arthur Andersen']
|
10
|
+
spec.email = ['colin@invoca.com', 'aandersen@velalu.qa']
|
11
|
+
spec.summary = 'Enhanced Array#dig and Hash#dig implementation.'
|
12
|
+
spec.homepage = 'https://github.com/leoc/ruby_dig2'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'minitest'
|
23
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
|
2
|
+
|
3
|
+
require 'minitest'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
require 'ruby_dig2'
|
7
|
+
|
8
|
+
class RubyDig2Test
|
9
|
+
class Diggable
|
10
|
+
def dig2(*keys)
|
11
|
+
keys
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe RubyDig2 do
|
16
|
+
describe "Array" do
|
17
|
+
it "digs an array by index" do
|
18
|
+
assert_equal 'one', ['zero', 'one', 'two'].dig2(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "digs a nested array by index" do
|
22
|
+
assert_equal 'twelve', ['zero', ['ten', 'eleven', 'twelve'], 'two'].dig2(1, 2)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "raises TypeError when nested array doesn't support dig2" do
|
26
|
+
assert_raises(TypeError) { ['zero', 'one', 'two'].dig2(1, 2) }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns nil when dig not found" do
|
30
|
+
assert_equal nil, ['zero', 'one', 'two'].dig2(4)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "raises TypeError when dig index not an integer" do
|
34
|
+
assert_raises(TypeError) { ['zero', 'one', 'two'].dig2(:four) }
|
35
|
+
end
|
36
|
+
|
37
|
+
it "digs into any object that implements dig2" do
|
38
|
+
assert_equal [:a, :b], [0, Diggable.new].dig2(1, :a, :b)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns the value false" do
|
42
|
+
assert_equal false, [:a, [true, false]].dig2(1, 1)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "Hash" do
|
47
|
+
it "digs a hash by key" do
|
48
|
+
assert_equal 'Homer', {first: "Homer", last: "Simpson"}.dig2(:first)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "digs a nested hash by keys" do
|
52
|
+
assert_equal 'Homer', {mom: {first: "Marge", last: "Bouvier"}, dad: {first: "Homer", last: "Simpson"}}.dig2(:dad, :first)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises TypeError when nested hash doesn't support dig2" do
|
56
|
+
assert_raises(TypeError) { {mom: {first: "Marge", last: "Bouvier"}, dad: "Homer Simpson"}.dig2(:dad, :first) }
|
57
|
+
end
|
58
|
+
|
59
|
+
it "returns nil when dig not found" do
|
60
|
+
assert_equal nil, {first: "Homer", last: "Simpson"}.dig2(:middle)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "digs into any object that implements dig2" do
|
64
|
+
assert_equal [:a, :b], {diggable: Diggable.new}.dig2(:diggable, :a, :b)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns the value false" do
|
68
|
+
assert_equal false, {first: "Homer", last: "Simpson", sobber: false}.dig2(:sobber)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "Nested Hash and Array" do
|
73
|
+
it "navigates both" do
|
74
|
+
assert_equal 'Lisa', {mom: {first: "Marge", last: "Bouvier"},
|
75
|
+
dad: {first: "Homer", last: "Simpson"},
|
76
|
+
kids: [{first: "Bart"}, {first: "Lisa"}]}.dig2(:kids, 1, :first)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "detects key-value path element" do
|
80
|
+
assert_equal 'Bouvier', { people: [
|
81
|
+
{first: "Marge", last: "Bouvier"},
|
82
|
+
{first: "Homer", last: "Simpson"},
|
83
|
+
]
|
84
|
+
}.dig2(:people, {first: "Marge"}, :last)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_dig2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Kelley
|
8
|
+
- Arthur Andersen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-09-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.7'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.7'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description:
|
57
|
+
email:
|
58
|
+
- colin@invoca.com
|
59
|
+
- aandersen@velalu.qa
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/ruby_dig2.rb
|
70
|
+
- lib/ruby_dig2/version.rb
|
71
|
+
- ruby_dig2.gemspec
|
72
|
+
- test/ruby_dig2_test.rb
|
73
|
+
homepage: https://github.com/leoc/ruby_dig2
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Enhanced Array#dig and Hash#dig implementation.
|
97
|
+
test_files:
|
98
|
+
- test/ruby_dig2_test.rb
|