ruby_dig 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d693ef850fe9ed76af4081bfcd41c29fde29085
4
+ data.tar.gz: 840a4f8139a67ec93021624600dec541e26b7211
5
+ SHA512:
6
+ metadata.gz: 481f89b52a9e9b63f0bb600d76c5219c231c6f1391a9525d153c30ffceded3f70976a501d5842f4b5702c49c47555827c7637329474e8a0b408accd80e265bfd
7
+ data.tar.gz: 2aa00078822254d15251aced71edc1a0d9fd35107a4f575ac8b24d8e4c6bf9c0416921695db23a82f69fd45dd87c8f0c01a68bfcff3412520c1d25b5e02d99d2
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
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
@@ -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.
@@ -0,0 +1,31 @@
1
+ # RubyDig
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby_dig'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby_dig
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/ruby_dig/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ desc "Run unit tests."
6
+ task :test do
7
+ ruby "./test/ruby_dig_test.rb"
8
+ end
@@ -0,0 +1,21 @@
1
+ module RubyDig
2
+ def dig(key, *rest)
3
+ if value = (self[key] rescue nil)
4
+ if rest.empty?
5
+ value
6
+ elsif value.is_a?(RubyDig)
7
+ value.dig(*rest)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ if RUBY_VERSION < '2.3'
14
+ class Array
15
+ include RubyDig
16
+ end
17
+
18
+ class Hash
19
+ include RubyDig
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module RubyDig
2
+ VERSION = "0.0.1"
3
+ 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 'ruby_dig/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_dig"
8
+ spec.version = RubyDig::VERSION
9
+ spec.authors = ["Colin Kelley"]
10
+ spec.email = ["colin@invoca.com"]
11
+ spec.summary = %q{Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.}
12
+ spec.homepage = ""
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,58 @@
1
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
2
+
3
+ require 'minitest'
4
+ require 'minitest/autorun'
5
+
6
+ require 'ruby_dig'
7
+
8
+ class RubyDigTest
9
+ describe RubyDig do
10
+ describe "Array" do
11
+ it "digs an array by index" do
12
+ assert_equal 'one', ['zero', 'one', 'two'].dig(1)
13
+ end
14
+
15
+ it "digs a nested array by index" do
16
+ assert_equal 'twelve', ['zero', ['ten', 'eleven', 'twelve'], 'two'].dig(1, 2)
17
+ end
18
+
19
+ it "returns nil when nested array doesn't support dig" do
20
+ assert_equal nil, ['zero', 'one', 'two'].dig(1, 2)
21
+ end
22
+
23
+ it "returns nil when dig not found" do
24
+ assert_equal nil, ['zero', 'one', 'two'].dig(4)
25
+ end
26
+
27
+ it "returns nil when dig index not an integer" do
28
+ assert_equal nil, ['zero', 'one', 'two'].dig(:four)
29
+ end
30
+ end
31
+
32
+ describe "Hash" do
33
+ it "digs a hash by key" do
34
+ assert_equal 'Homer', {first: "Homer", last: "Simpson"}.dig(:first)
35
+ end
36
+
37
+ it "digs a nested hash by keys" do
38
+ assert_equal 'Homer', {mom: {first: "Marge", last: "Bouvier"}, dad: {first: "Homer", last: "Simpson"}}.dig(:dad, :first)
39
+ end
40
+
41
+ it "returns nil when nested hash doesn't support dig" do
42
+ assert_equal nil, {mom: {first: "Marge", last: "Bouvier"}, dad: "Homer Simpson"}.dig(:dad, :first)
43
+ end
44
+
45
+ it "returns nil when dig not found" do
46
+ assert_equal nil, {first: "Homer", last: "Simpson"}.dig(:middle)
47
+ end
48
+ end
49
+
50
+ describe "Nested Hash and Array" do
51
+ it "navigates both" do
52
+ assert_equal 'Lisa', {mom: {first: "Marge", last: "Bouvier"},
53
+ dad: {first: "Homer", last: "Simpson"},
54
+ kids: [{first: "Bart"}, {first: "Lisa"}]}.dig(:kids, 1, :first)
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_dig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Colin Kelley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-29 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
+ - colin@invoca.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/ruby_dig.rb
68
+ - lib/ruby_dig/version.rb
69
+ - ruby_dig.gemspec
70
+ - test/ruby_dig_test.rb
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Pure Ruby implementation of Array#dig and Hash#dig for Ruby < 2.3.
95
+ test_files:
96
+ - test/ruby_dig_test.rb