better_dig 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 20b8ebd00c373f4c14eb90b8e61122c442757a26475bb13d236b40f45ce733bf
4
+ data.tar.gz: 8634cf08b1e593957c5d55612dd5b92c3ac3b7a3e114fead05b8662d12392935
5
+ SHA512:
6
+ metadata.gz: 1d15de67cd3132eb39da5be6446218bbee0a574701360f91422fe7fc3e42057caf11375b1798cbd6d97c18ffc75449f990829072498a421f585088ab30100d28
7
+ data.tar.gz: eb8bae3f347ae3ea91f18a28f8cf127efa9778d9a721c2f1067772b1487c3a5ac731250fdc1a608be95d0c48533275695f583aacca975f9d922707426cd26502
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ sudo: false
2
+ language: ruby
3
+ env:
4
+ global:
5
+ - CC_TEST_REPORTER_ID=18aab811b8decd95390672fc6eec558036fe67eddc166878ae660e356219f3f8
6
+ rvm:
7
+ - 2.1
8
+ - 2.2
9
+ - 2.3
10
+ - 2.4
11
+ - 2.5
12
+ before_install:
13
+ - gem install bundler
14
+ - gem update --system
15
+ - gem --version
16
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
17
+ - chmod +x ./cc-test-reporter
18
+ - ./cc-test-reporter before-build
19
+ script:
20
+ - bundle exec rake test
21
+ after_script:
22
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in better_dig.gemspec
6
+ gemspec
7
+
8
+ group :test do
9
+ gem "simplecov", "~> 0.15.1"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ better_dig (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ docile (1.1.5)
10
+ json (2.1.0)
11
+ minitest (5.11.3)
12
+ rake (10.5.0)
13
+ simplecov (0.15.1)
14
+ docile (~> 1.1.0)
15
+ json (>= 1.8, < 3)
16
+ simplecov-html (~> 0.10.0)
17
+ simplecov-html (0.10.2)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ better_dig!
24
+ bundler (~> 1.16)
25
+ minitest (~> 5.0)
26
+ rake (~> 10.0)
27
+ simplecov (~> 0.15.1)
28
+
29
+ BUNDLED WITH
30
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Bernie Chiu
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,58 @@
1
+ [![Maintainability](https://api.codeclimate.com/v1/badges/6bc69601493caa67e52e/maintainability)](https://codeclimate.com/github/berniechiu/better_dig/maintainability)
2
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/6bc69601493caa67e52e/test_coverage)](https://codeclimate.com/github/berniechiu/better_dig/test_coverage)
3
+ [![Build Status](https://travis-ci.org/berniechiu/better_dig.svg?branch=master)](https://travis-ci.org/berniechiu/better_dig)
4
+
5
+ # BetterDig
6
+
7
+ Another kind of hash/array lookup you'll like
8
+
9
+ ## Table of Context
10
+
11
+ * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [Contributing](#contributing)
14
+ * [License](#license)
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'better_dig'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install better_dig
31
+
32
+ ## Usage
33
+
34
+ ```ruby
35
+ hash = { hello: 'world', nested: { hello: 'world' }, nested_array: ['hello', 'world'] }
36
+
37
+ hash.digg(:hello) # => 'world'
38
+ hash.digg('hello') # => 'world'
39
+ hash.digg('nested', 'hello') # => 'world'
40
+ hash.digg('nested_array', 0) # => 'hello'
41
+
42
+ # Since `nil` will be returned for not found value instead of exception,
43
+ # we can use conditional block now without breaking the application
44
+
45
+ if hash.digg(:nested, :hello, :world) # => nil
46
+ # DO SOMETHING
47
+ else
48
+ # DO SOMETHING
49
+ end
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/berniechiu/better_dig.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "better_dig/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "better_dig"
8
+ spec.version = BetterDig::VERSION
9
+ spec.authors = ["Bernie Chiu"]
10
+ spec.email = ["bernie_chiu@hotmail.com"]
11
+
12
+ spec.summary = %q{Dig-like method for indifferent access of nested data lookup.}
13
+ spec.description = %q{Provide simple to use dig library for nested hash or array lookup}
14
+ spec.homepage = "https://github.com/berniechiu/better_dig"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "better_dig"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module BetterDig
2
+ VERSION = "0.5.0"
3
+ end
data/lib/better_dig.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "better_dig/version"
2
+
3
+ module BetterDig
4
+ DIGGABLE_CLASSES = [Hash, Array].freeze
5
+ INDIFFERENT_FIND = { Symbol => :to_s, String => :to_sym }.freeze
6
+
7
+ def digg(key, *rest)
8
+ value = self[key.to_i] if match_array?(key)
9
+ value = find_indifferent_value_by(key) if self.is_a?(Hash)
10
+ return value if value.nil? || rest.empty?
11
+ return value.digg(*rest) if value.respond_to?(:digg)
12
+ nil
13
+ end
14
+
15
+ private
16
+
17
+ def match_array?(key)
18
+ if self.is_a?(Array)
19
+ return key.match(/\A[0-9]+\z/) if key.is_a?(String)
20
+ return true if key.is_a?(Numeric)
21
+ end
22
+ false
23
+ end
24
+
25
+ def find_indifferent_value_by(key)
26
+ self[key] || self[key.send(INDIFFERENT_FIND[key.class])
27
+ end
28
+ end
29
+
30
+ BetterDig::DIGGABLE_CLASSES.each { |klass| klass.send(:include, BetterDig) unless klass.method_defined?(:digg) }
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_dig
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Bernie Chiu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Provide simple to use dig library for nested hash or array lookup
56
+ email:
57
+ - bernie_chiu@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - better_dig.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/better_dig.rb
73
+ - lib/better_dig/version.rb
74
+ homepage: https://github.com/berniechiu/better_dig
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Dig-like method for indifferent access of nested data lookup.
99
+ test_files: []