respond_to_dig 1.0.0
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/CONTRIBUTING.md +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +8 -0
- data/lib/respond_to_dig/version.rb +3 -0
- data/lib/respond_to_dig.rb +27 -0
- data/respond_to_dig.gemspec +23 -0
- data/test/respond_to_dig_test.rb +93 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 684e7adf041fc4664d3d08ed6e68690858d1ff18
|
4
|
+
data.tar.gz: 7da073c97ebb9d9227b353f9efdc6c3b4ed744bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d3310f6d20d9e25c3b79ada3a8a3c18c1a579343e7c218678ca7933556a262a82d1b66982d61309997378d6e48e849749a5edb591b5bff6da715e716627c3fc
|
7
|
+
data.tar.gz: f97cd4a6aec84f0700bf935ec8cad0e200d5c9587c50ecf108b6b90ce92f959e2a5078af356e964cc6d6ef8c6e337b69a04543207d0624f4c0af794a7b8e6bbe
|
data/.gitignore
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
Contributing
|
2
|
+
===
|
3
|
+
|
4
|
+
1. Fork it ( https://github.com/announce/respond_to_dig/fork )
|
5
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
6
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
7
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
8
|
+
5. Create a new Pull Request
|
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,42 @@
|
|
1
|
+
RespondToDig
|
2
|
+
===
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/announce/respond_to_dig.svg?branch=master)](https://travis-ci.org/announce/respond_to_dig)
|
5
|
+
|
6
|
+
This gem backports Array#dig and Hash#dig methods from Ruby 2.3+ to earlier versions of Ruby, only if you explicitly call it.
|
7
|
+
|
8
|
+
## vs RubyDig
|
9
|
+
* [RubyDig](https://github.com/Invoca/ruby_dig) has side-effects, but RespondToDig doesn't.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's `Gemfile`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'respond_to_dig'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install respond_to_dig
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```
|
30
|
+
require 'respond_to_dig'
|
31
|
+
|
32
|
+
response = RespondToDig::respond_to_dig({
|
33
|
+
mom: {first: "Marge", last: "Bouvier"},
|
34
|
+
dad: {first: "Homer", last: "Simpson"},
|
35
|
+
kids: [
|
36
|
+
{first: "Bart", last: "Simpson"},
|
37
|
+
{first: "Lisa", last: "Simpson"}
|
38
|
+
]})
|
39
|
+
|
40
|
+
response.dig(:kids, 1, :first)
|
41
|
+
# => "Lisa"
|
42
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module RespondToDig
|
2
|
+
RESPONDERS = [Array, Hash]
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def respond_to_dig(receiver)
|
6
|
+
receiver.tap do |r|
|
7
|
+
r.singleton_class.send(:include, RespondToDig) if RespondToDig.target? r
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def target?(target)
|
12
|
+
not respond_to? :dig and RESPONDERS.any? {|klass| target.is_a? klass}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def dig(key, *rest)
|
17
|
+
value = RespondToDig::respond_to_dig(self[key])
|
18
|
+
if value.nil? || rest.empty?
|
19
|
+
value
|
20
|
+
elsif value.respond_to?(:dig)
|
21
|
+
value.dig(*rest)
|
22
|
+
else
|
23
|
+
fail TypeError, "#{value.class} does not have #dig method"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -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 'respond_to_dig/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "respond_to_dig"
|
8
|
+
spec.version = RespondToDig::VERSION
|
9
|
+
spec.authors = ["@ymkjp"]
|
10
|
+
spec.email = ["ymkjp@jaist.ac.jp"]
|
11
|
+
spec.summary = %q{Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.}
|
12
|
+
spec.homepage = "https://github.com/announce/respond_to_dig"
|
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,93 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
|
2
|
+
|
3
|
+
require 'minitest'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
|
6
|
+
require 'respond_to_dig'
|
7
|
+
|
8
|
+
class RespondToDigTest
|
9
|
+
class Diggable
|
10
|
+
def dig(*keys)
|
11
|
+
keys
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe RespondToDig do
|
16
|
+
let (:an_array) {
|
17
|
+
RespondToDig::respond_to_dig(['zero', 'one', 'two'])
|
18
|
+
}
|
19
|
+
let (:a_nested_array) {
|
20
|
+
RespondToDig::respond_to_dig(['zero', ['ten', 'eleven', 'twelve', false], 'two'])
|
21
|
+
}
|
22
|
+
let (:a_hash) {
|
23
|
+
RespondToDig::respond_to_dig({first: "Homer", last: "Simpson", sobber: false})
|
24
|
+
}
|
25
|
+
let (:a_nested_hash) {
|
26
|
+
RespondToDig::respond_to_dig({mom: {first: "Marge", last: "Bouvier"}, dad: {first: "Homer", last: "Simpson"}})
|
27
|
+
}
|
28
|
+
|
29
|
+
describe "Array" do
|
30
|
+
it "digs an array by index" do
|
31
|
+
assert_equal 'one', an_array.dig(1)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "digs a nested array by index" do
|
35
|
+
assert_equal 'twelve', a_nested_array.dig(1, 2)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "raises TypeError when nested array doesn't support dig" do
|
39
|
+
assert_raises(TypeError) { an_array.dig(1, 2) }
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns nil when dig not found" do
|
43
|
+
assert_equal nil, an_array.dig(4)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "raises TypeError when dig index not an integer" do
|
47
|
+
assert_raises(TypeError) { an_array.dig(:four) }
|
48
|
+
end
|
49
|
+
|
50
|
+
it "digs into any object that implements dig" do
|
51
|
+
assert_equal [:a, :b], RespondToDig::respond_to_dig([0, Diggable.new]).dig(1, :a, :b)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns the value false" do
|
55
|
+
assert_equal false, a_nested_array.dig(1, 3)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "Hash" do
|
60
|
+
it "digs a hash by key" do
|
61
|
+
assert_equal 'Homer', a_hash.dig(:first)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "digs a nested hash by keys" do
|
65
|
+
assert_equal 'Homer', a_nested_hash.dig(:dad, :first)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "raises TypeError when nested hash doesn't support dig" do
|
69
|
+
assert_raises(TypeError) { a_hash.dig(:first, :foo) }
|
70
|
+
end
|
71
|
+
|
72
|
+
it "returns nil when dig not found" do
|
73
|
+
assert_equal nil, a_hash.dig(:middle)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "digs into any object that implements dig" do
|
77
|
+
assert_equal [:a, :b], RespondToDig::respond_to_dig({diggable: Diggable.new}).dig(:diggable, :a, :b)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "returns the value false" do
|
81
|
+
assert_equal false, a_hash.dig(:sobber)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "Nested Hash and Array" do
|
86
|
+
it "navigates both" do
|
87
|
+
assert_equal 'Lisa', RespondToDig::respond_to_dig({mom: {first: "Marge", last: "Bouvier"},
|
88
|
+
dad: {first: "Homer", last: "Simpson"},
|
89
|
+
kids: [{first: "Bart"}, {first: "Lisa"}]}).dig(:kids, 1, :first)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: respond_to_dig
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- '@ymkjp'
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-03 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: 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:
|
56
|
+
email:
|
57
|
+
- ymkjp@jaist.ac.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- CONTRIBUTING.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/respond_to_dig.rb
|
69
|
+
- lib/respond_to_dig/version.rb
|
70
|
+
- respond_to_dig.gemspec
|
71
|
+
- test/respond_to_dig_test.rb
|
72
|
+
homepage: https://github.com/announce/respond_to_dig
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.4
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.
|
96
|
+
test_files:
|
97
|
+
- test/respond_to_dig_test.rb
|