dig_rb 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 +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/dig_rb.gemspec +23 -0
- data/lib/dig_rb.rb +13 -0
- data/lib/dig_rb/array.rb +10 -0
- data/lib/dig_rb/hash.rb +15 -0
- data/lib/dig_rb/ostruct.rb +26 -0
- data/lib/dig_rb/struct.rb +27 -0
- data/lib/dig_rb/version.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8119335fa948de9dfdfccbf17dd0bb995fa92d67
|
4
|
+
data.tar.gz: a5fa30724dde8b5798924b74f46ab7b49ef6b885
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1575d9074b230b488030453743e3344577ed1656c8e1c9867d437e3c011d4669ead57e0c4905454893a694119b4000eda29dcb21605e70241d1bbf8aad499a33
|
7
|
+
data.tar.gz: 6f685adff1c006cfcfef66c2a013ee289787b62b2c1be9706e5d82eb379af6c2e44580502b2ef10cf4586e058c93d23842349f5e3696c4d08b50df88e70f9f4c
|
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) 2016 Jonathan Rochkind
|
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,57 @@
|
|
1
|
+
# Dig_rb
|
2
|
+
|
3
|
+
[Ruby 2.3.0 introduced #dig on Hash, Array, and Struct](https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-released/). With this gem, you can have dig on ruby pre 2.3.0, or any ruby lacking dig.
|
4
|
+
|
5
|
+
If you are writing an app and want to use dig in it you should probably just upgrade to ruby 2.3.0. But if you are writing a gem and want it to work with both MRI 2.3.0 and others, this gem is for you. This gem only adds #dig methods if they aren't already defined, so it's safe to use in code that is for all rubies, if run on MRI 2.3.0 you'll still be using native #dig, otherwise dig_rb's implementation.
|
6
|
+
|
7
|
+
## Will it work identically to MRI 2.3.0 dig?
|
8
|
+
|
9
|
+
Dig_rb is tested with:
|
10
|
+
|
11
|
+
* Specs found in MRI repo for #dig in 2.3.0
|
12
|
+
* [Ruby Spec Suite](https://github.com/ruby/spec/) specs found in repo for Array and Hash#dig
|
13
|
+
* All examples in MRI 2.3.0 generated method API docs. (One example in MRI 2.3.0 is _wrong_ about exception class/method returned, dig_rb matches actual 2.3.0 behavior there, not documented example)
|
14
|
+
|
15
|
+
If you find any weird edge cases that work differenty in MRI 2.3.0 than in ruby_dig, let me know in a GitHub Issue please.
|
16
|
+
|
17
|
+
The performance of dig_rb will probably be less than native MRI 2.3.0 implementation, this code is not written for performance. But it should
|
18
|
+
be fine, really.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'dig_rb'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install dig_rb
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
Just go ahead and use #dig as doc'd in MRI 2.3.0, now it'll work on any ruby.
|
39
|
+
|
40
|
+
* [Hash](http://ruby-doc.org/core-2.3.0/Hash.html#method-i-dig)
|
41
|
+
* [Array](http://ruby-doc.org/core-2.3.0/Array.html#method-i-dig)
|
42
|
+
* [Struct](http://ruby-doc.org/core-2.3.0/Struct.html#method-i-dig)
|
43
|
+
* [OpenStruct](http://ruby-doc.org/stdlib-2.3.0/libdoc/ostruct/rdoc/OpenStruct.html#method-i-dig)
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it ( https://github.com/[my-github-username]/dig_rb/fork )
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dig_rb"
|
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
data/dig_rb.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 'dig_rb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dig_rb"
|
8
|
+
spec.version = DigRb::VERSION
|
9
|
+
spec.authors = ["Jonathan Rochkind"]
|
10
|
+
spec.email = ["jonathan@dnil.net"]
|
11
|
+
|
12
|
+
spec.summary = %q{Array/Hash/Struct#dig backfill for ruby}
|
13
|
+
spec.homepage = "https://github.com/jrochkind/dig_rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/dig_rb.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'dig_rb/version'
|
2
|
+
require 'dig_rb/hash'
|
3
|
+
require 'dig_rb/array'
|
4
|
+
require 'dig_rb/struct'
|
5
|
+
require 'dig_rb/ostruct'
|
6
|
+
|
7
|
+
module DigRb
|
8
|
+
def self.guard_dig(obj)
|
9
|
+
unless obj.respond_to?(:dig)
|
10
|
+
raise TypeError, "#{obj.class.name} does not have #dig method"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/dig_rb/array.rb
ADDED
data/lib/dig_rb/hash.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
unless Hash.respond_to?(:dig)
|
2
|
+
Hash.class_eval do
|
3
|
+
# Retrieves the value object corresponding to the each key objects repeatedly.
|
4
|
+
#
|
5
|
+
# h = { foo: {bar: {baz: 1}}}
|
6
|
+
# h.dig(:foo, :bar, :baz) #=> 1
|
7
|
+
# h.dig(:foo, :zot) #=> nil
|
8
|
+
def dig(key, *args)
|
9
|
+
value = self[key]
|
10
|
+
return value if args.length == 0 || value.nil?
|
11
|
+
DigRb.guard_dig(value)
|
12
|
+
value.dig(*args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
unless OpenStruct.respond_to?(:dig)
|
3
|
+
OpenStruct.class_eval do
|
4
|
+
#
|
5
|
+
# Retrieves the value object corresponding to the each +name+
|
6
|
+
# objects repeatedly.
|
7
|
+
#
|
8
|
+
# address = OpenStruct.new('city' => "Anytown NC", 'zip' => 12345)
|
9
|
+
# person = OpenStruct.new('name' => 'John Smith', 'address' => address)
|
10
|
+
# person.dig(:address, 'zip') # => 12345
|
11
|
+
# person.dig(:business_address, 'zip') # => nil
|
12
|
+
#
|
13
|
+
def dig(name, *args)
|
14
|
+
begin
|
15
|
+
name = name.to_sym
|
16
|
+
rescue NoMethodError
|
17
|
+
raise TypeError, "#{name} is not a symbol nor a string"
|
18
|
+
end
|
19
|
+
return nil unless self.respond_to?(name)
|
20
|
+
value = self.send(name)
|
21
|
+
return value if args.length == 0 || value.nil?
|
22
|
+
DigRb.guard_dig(value)
|
23
|
+
value.dig(*args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
unless Struct.respond_to?(:dig)
|
3
|
+
Struct.class_eval do
|
4
|
+
|
5
|
+
# Extracts the nested value specified by the sequence of <i>idx</i>
|
6
|
+
# objects by calling +dig+ at each step, returning +nil+ if any
|
7
|
+
# intermediate step is +nil+.
|
8
|
+
|
9
|
+
# klass = Struct.new(:a)
|
10
|
+
# o = klass.new(klass.new({b: [1, 2, 3]}))
|
11
|
+
|
12
|
+
# o.dig(:a, :a, :b, 0) #=> 1
|
13
|
+
# o.dig(:b, 0) #=> nil
|
14
|
+
def dig(name, *args)
|
15
|
+
begin
|
16
|
+
name = name.to_sym
|
17
|
+
rescue NoMethodError
|
18
|
+
raise TypeError, "#{name} is not a symbol nor a string"
|
19
|
+
end
|
20
|
+
return nil unless self.respond_to?(name)
|
21
|
+
value = self.send(name)
|
22
|
+
return value if args.length == 0 || value.nil?
|
23
|
+
DigRb.guard_dig(value)
|
24
|
+
value.dig(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dig_rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Rochkind
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-18 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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
|
+
description:
|
42
|
+
email:
|
43
|
+
- jonathan@dnil.net
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- dig_rb.gemspec
|
57
|
+
- lib/dig_rb.rb
|
58
|
+
- lib/dig_rb/array.rb
|
59
|
+
- lib/dig_rb/hash.rb
|
60
|
+
- lib/dig_rb/ostruct.rb
|
61
|
+
- lib/dig_rb/struct.rb
|
62
|
+
- lib/dig_rb/version.rb
|
63
|
+
homepage: https://github.com/jrochkind/dig_rb
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Array/Hash/Struct#dig backfill for ruby
|
87
|
+
test_files: []
|