respond_to_dig 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 684e7adf041fc4664d3d08ed6e68690858d1ff18
4
- data.tar.gz: 7da073c97ebb9d9227b353f9efdc6c3b4ed744bf
3
+ metadata.gz: 38f3df9283b99bb984df4903b5cbf3c9918eafd3
4
+ data.tar.gz: 4fda9b0c7ad5aa7a33e0f75c872e01714a2e4f64
5
5
  SHA512:
6
- metadata.gz: 0d3310f6d20d9e25c3b79ada3a8a3c18c1a579343e7c218678ca7933556a262a82d1b66982d61309997378d6e48e849749a5edb591b5bff6da715e716627c3fc
7
- data.tar.gz: f97cd4a6aec84f0700bf935ec8cad0e200d5c9587c50ecf108b6b90ce92f959e2a5078af356e964cc6d6ef8c6e337b69a04543207d0624f4c0af794a7b8e6bbe
6
+ metadata.gz: 70ed1d747efffcf96b38ba3e70c1fb3730b09e5466fc72f591f964675ab098c925d8ce8b63c55ad7b8112f451cae5190f2c2ec632288865445c245baecfa2da7
7
+ data.tar.gz: c5ef4ceee329deaf2317936992287be1aced7310256a18c61f1ecca5113ee3cc61558cae4222e05fcf709c09f98713d542d4a98ca89d09fdf46107882f2e0487
data/.editorconfig ADDED
@@ -0,0 +1,10 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ indent_style = space
7
+ indent_size = 2
8
+ insert_final_newline = true
9
+ trim_trailing_whitespace = true
10
+ max_line_length = 120
@@ -0,0 +1,11 @@
1
+ | Q | A
2
+ | ------------- | ---
3
+ | Branch? | "master" for new features / 1.0 for fixes
4
+ | Bug fix? | yes/no
5
+ | New feature? | yes/no
6
+ | BC breaks? | yes/no
7
+ | Deprecations? | yes/no
8
+ | Tests pass? | yes/no
9
+ | Fixed tickets | comma-separated list of tickets fixed by the PR, if any
10
+ | License | MIT
11
+ | Doc PR | reference to the documentation PR, if any
data/.gitignore CHANGED
@@ -12,3 +12,7 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+
16
+ !/.github/
17
+ !/.editorconfig
18
+ !/.travis.yml
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0
6
+ - 1.9
data/README.md CHANGED
@@ -5,8 +5,9 @@ RespondToDig
5
5
 
6
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
7
 
8
- ## vs RubyDig
9
- * [RubyDig](https://github.com/Invoca/ruby_dig) has side-effects, but RespondToDig doesn't.
8
+ ## vs [RubyDig](https://github.com/Invoca/ruby_dig)
9
+ * RubyDig has side-effects, but RespondToDig doesn't.
10
+ * RespondToDig only supports `Array` and `Hash`, but RespondToDig supports any `Enumerable` classes which have `[]` method such as `Struct`.
10
11
 
11
12
  ## Installation
12
13
 
@@ -1,3 +1,3 @@
1
1
  module RespondToDig
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.freeze
3
3
  end
@@ -1,15 +1,18 @@
1
1
  module RespondToDig
2
- RESPONDERS = [Array, Hash]
3
2
 
4
3
  class << self
5
4
  def respond_to_dig(receiver)
6
5
  receiver.tap do |r|
7
- r.singleton_class.send(:include, RespondToDig) if RespondToDig.target? r
6
+ r.singleton_class.send(:include, RespondToDig) if RespondToDig.diggable? r
8
7
  end
9
8
  end
10
9
 
11
- def target?(target)
12
- not respond_to? :dig and RESPONDERS.any? {|klass| target.is_a? klass}
10
+ # The reason why not purely duck typed by `[]` method is to avoid unexpected behavior,
11
+ # for example we won't get `'o'` by `'foo'.dig(1)` by String#[].
12
+ def diggable?(target)
13
+ target.is_a? Enumerable and
14
+ target.respond_to? :[] and
15
+ not target.respond_to? :dig
13
16
  end
14
17
  end
15
18
 
@@ -6,9 +6,9 @@ require 'respond_to_dig/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "respond_to_dig"
8
8
  spec.version = RespondToDig::VERSION
9
- spec.authors = ["@ymkjp"]
9
+ spec.authors = ["ymkjp"]
10
10
  spec.email = ["ymkjp@jaist.ac.jp"]
11
- spec.summary = %q{Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.}
11
+ spec.summary = %q{Explicit backporting of Array#dig and Hash#dig for Ruby < 2.3.}
12
12
  spec.homepage = "https://github.com/announce/respond_to_dig"
13
13
  spec.license = "MIT"
14
14
 
@@ -25,6 +25,10 @@ class RespondToDigTest
25
25
  let (:a_nested_hash) {
26
26
  RespondToDig::respond_to_dig({mom: {first: "Marge", last: "Bouvier"}, dad: {first: "Homer", last: "Simpson"}})
27
27
  }
28
+ let (:containing_struct) {
29
+ Dog = Struct.new(:name, :age)
30
+ RespondToDig::respond_to_dig({dogs: [Dog.new("Fred", 5), Dog.new("Harry", 2)]})
31
+ }
28
32
 
29
33
  describe "Array" do
30
34
  it "digs an array by index" do
@@ -47,10 +51,6 @@ class RespondToDigTest
47
51
  assert_raises(TypeError) { an_array.dig(:four) }
48
52
  end
49
53
 
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
54
  it "returns the value false" do
55
55
  assert_equal false, a_nested_array.dig(1, 3)
56
56
  end
@@ -82,11 +82,20 @@ class RespondToDigTest
82
82
  end
83
83
  end
84
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)
85
+ describe "Various classes" do
86
+ it "navigates both nested Hash and Array" do
87
+ assert_equal 'Lisa', RespondToDig::respond_to_dig(
88
+ {mom: {first: "Marge", last: "Bouvier"},
89
+ dad: {first: "Homer", last: "Simpson"},
90
+ kids: [{first: "Bart"}, {first: "Lisa"}]}).dig(:kids, 1, :first)
91
+ end
92
+
93
+ it "digs into any object that implements dig" do
94
+ assert_equal [:a, :b], RespondToDig::respond_to_dig([0, Diggable.new]).dig(1, :a, :b)
95
+ end
96
+
97
+ it "digs into Enumerable object that implements #[]" do
98
+ assert_equal 'Harry', RespondToDig::respond_to_dig(containing_struct).dig(:dogs, 1, :name)
90
99
  end
91
100
  end
92
101
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: respond_to_dig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - '@ymkjp'
7
+ - ymkjp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
@@ -14,42 +14,42 @@ dependencies:
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -59,7 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".editorconfig"
63
+ - ".github/PULL_REQUEST_TEMPLATE.md"
64
+ - ".gitignore"
65
+ - ".travis.yml"
63
66
  - CONTRIBUTING.md
64
67
  - Gemfile
65
68
  - LICENSE.txt
@@ -79,19 +82,19 @@ require_paths:
79
82
  - lib
80
83
  required_ruby_version: !ruby/object:Gem::Requirement
81
84
  requirements:
82
- - - '>='
85
+ - - ">="
83
86
  - !ruby/object:Gem::Version
84
87
  version: '0'
85
88
  required_rubygems_version: !ruby/object:Gem::Requirement
86
89
  requirements:
87
- - - '>='
90
+ - - ">="
88
91
  - !ruby/object:Gem::Version
89
92
  version: '0'
90
93
  requirements: []
91
94
  rubyforge_project:
92
- rubygems_version: 2.6.4
95
+ rubygems_version: 2.2.2
93
96
  signing_key:
94
97
  specification_version: 4
95
- summary: Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.
98
+ summary: Explicit backporting of Array#dig and Hash#dig for Ruby < 2.3.
96
99
  test_files:
97
100
  - test/respond_to_dig_test.rb