values_at_nested 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 82a8e37c7655ba5e29ecde1250afe43483335f07
4
+ data.tar.gz: ae2f1fb9e2a4aea8b21999ffe6f6745ea8fb73a2
5
+ SHA512:
6
+ metadata.gz: 0a10da734b01971358b1edcb1f28f43c66b51572710bb5a20d270cc294b1ebfdd1a4d1e62b532929a4cc28013e4b777c8d763ff43cd0208d9f77dc8ce74a01dd
7
+ data.tar.gz: '0594d6d75e3edf6262debb9478b0253fc72882fbb37641cea32877c2926b14b7e38324bc31c2bbf670b51cf47a062e36c6d0f163da1e913dead9c92ca1d10051'
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ # Created by https://www.gitignore.io/api/ruby
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /spec/examples.txt
12
+ /test/tmp/
13
+ /test/version_tmp/
14
+ /tmp/
15
+
16
+ # Used by dotenv library to load environment variables.
17
+ # .env
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalization:
26
+ /.bundle/
27
+ /vendor/bundle
28
+ /lib/bundler/man/
29
+
30
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
31
+ .rvmrc
32
+
33
+ # End of https://www.gitignore.io/api/ruby
34
+
35
+ # Ignore publish script due to credentials
36
+ .publish
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ values_at_nested
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.3.0
6
+ - 2.4.0
7
+ - 2.4.1
8
+ before_install: gem install bundler -v 1.16.2
9
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,6 @@
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 values_at_nested.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ values_at_nested (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ codeclimate-test-reporter (1.0.8)
10
+ simplecov (<= 0.13)
11
+ diff-lcs (1.3)
12
+ docile (1.1.5)
13
+ json (2.1.0)
14
+ rake (10.5.0)
15
+ rspec (3.6.0)
16
+ rspec-core (~> 3.6.0)
17
+ rspec-expectations (~> 3.6.0)
18
+ rspec-mocks (~> 3.6.0)
19
+ rspec-core (3.6.0)
20
+ rspec-support (~> 3.6.0)
21
+ rspec-expectations (3.6.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.6.0)
24
+ rspec-mocks (3.6.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.6.0)
27
+ rspec-support (3.6.0)
28
+ simplecov (0.13.0)
29
+ docile (~> 1.1.0)
30
+ json (>= 1.8, < 3)
31
+ simplecov-html (~> 0.10.0)
32
+ simplecov-html (0.10.2)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.15)
39
+ codeclimate-test-reporter (~> 1.0)
40
+ rake (~> 10.0)
41
+ rspec (~> 3.0)
42
+ simplecov (~> 0.13.0)
43
+ values_at_nested!
44
+
45
+ BUNDLED WITH
46
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Abdullah Barrak
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,85 @@
1
+ # Values At Nested
2
+ [![Gem Version](https://badge.fury.io/rb/values_at_nested.svg)](https://rubygems.org/gems/values_at_nested)
3
+ [![Build Status](https://travis-ci.org/abarrak/values_at_nested.svg?branch=master)](https://travis-ci.org/abarrak/values_at_nested)
4
+ [![Test Coverage](https://codeclimate.com/github/abarrak/values_at_nested/badges/coverage.svg)](https://codeclimate.com/github/abarrak/values_at_nested/coverage)
5
+ [![Code Climate](https://lima.codeclimate.com/github/abarrak/values_at_nested/badges/gpa.svg)](https://lima.codeclimate.com/github/abarrak/values_at_nested)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ `values_at_nested` allows extraction of hash values at various depth of the hash.
9
+
10
+ It's an extended version of ruby's `#values_at` hash method.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'values_at_nested'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ ```shell
23
+ $ bundle
24
+ ```
25
+
26
+ Or install it yourself as:
27
+
28
+ ```shell
29
+ $ gem install values_at_nested
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Given the following hash:
35
+
36
+ ```ruby
37
+ h = { name: 'Albert', degree: 'Physics', preferences: { color: 'orange', pet: 'cat' } }
38
+ ```
39
+
40
+ You can deep extract values:
41
+
42
+ ```ruby
43
+ h.values_at_nested(preferences: :pet)
44
+ => [['cat']]
45
+
46
+ h.values_at_nested(:degree, preferences: [:color])
47
+ => ['Physics', ['orange']]
48
+ ```
49
+
50
+ Or extrac values in the first level just like normal `#values_at` does:
51
+
52
+ ```ruby
53
+ h.values_at_nested(:name)
54
+ => ['Albert']
55
+
56
+ h.values_at_nested(:name, :degree)
57
+ => ['Albert', 'Physics']
58
+
59
+ h.values_at_nested(:name, :degree, :preferences)
60
+ => ['Albert', 'Physics', ['orange', 'cat']]
61
+ ```
62
+
63
+ It works for any nested levels. Check [test case](https://github.com/abarrak/values_at_nested/blob/master/spec/values_at_nested_spec.rb#64-84) for 3-level, and 4-level depth examples.
64
+
65
+ ## Documentation
66
+
67
+ [RDoc version](http://www.rubydoc.info/gems/values_at_nested) at RubyDoc.
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
72
+
73
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`.
76
+
77
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/abarrak/values_at_nested).
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "values_at_nested"
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,41 @@
1
+ class Hash
2
+ ##
3
+ # Returns an array that holds the values of the given keys list of various depth.
4
+ #
5
+ # === Example:
6
+ #
7
+ # hash = { a: 58, b: { x: true, y: false }, c: nil }
8
+ #
9
+ # hash.values_at_nested(:b, :c) # => [58, nil]
10
+ # hash.values_at_nested(:a, b: [:x]) # => [58, [true]]
11
+ # hash.values_at_nested(:a, b: [:x, :y]) # => [58, [true, false]]
12
+ #
13
+ def values_at_nested(*nested_keys)
14
+ nested_keys.empty? ? [] : iterate_over_nested(nested_keys) { |k| self.dig(*k) }
15
+ end
16
+
17
+ def iterate_over_nested(keys)
18
+ keys.collect do |key|
19
+ value = self[key]
20
+
21
+ if key.is_a?(Hash)
22
+ key.collect do |k, v|
23
+ if Hash === v
24
+ yield [k, v.iterate_over_nested(v.keys) { |kn| kn }].flatten
25
+ elsif Array === v
26
+ v.collect { |vi| yield [k, vi] }
27
+ else
28
+ yield [k, v]
29
+ end
30
+ end.flatten 1
31
+
32
+ elsif key.is_a?(Symbol) && value.is_a?(Hash)
33
+ value.iterate_over_nested(value.keys) { |k| yield [key, k]}
34
+ elsif key.is_a?(Symbol) && value.is_a?(Symbol)
35
+ yield [key, value]
36
+ else
37
+ yield key
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module ValuesAtNested
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "values_at_nested/core_ext/hash/values_at_nested"
2
+ require "values_at_nested/version"
3
+
4
+ module ValuesAtNested
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require "date"
6
+ require "values_at_nested/version"
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "values_at_nested"
10
+ spec.version = ValuesAtNested::VERSION
11
+ spec.date = Date.today.to_s
12
+ spec.authors = ["Abdullah Barrak"]
13
+ spec.email = ["abdullah@abarrak.com"]
14
+
15
+ spec.summary = "An extended version of ruby's `#values_at` hash method."
16
+ spec.description = "values_at_nested values extraction of given hash keys at various depth."
17
+ spec.homepage = "https://github.com/abarrak/values_at_nested"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.15"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "simplecov", "~> 0.13.0"
31
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
32
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: values_at_nested
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Abdullah Barrak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-17 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ description: values_at_nested values extraction of given hash keys at various depth.
84
+ email:
85
+ - abdullah@abarrak.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".ruby-gemset"
93
+ - ".ruby-version"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/values_at_nested.rb
103
+ - lib/values_at_nested/core_ext/hash/values_at_nested.rb
104
+ - lib/values_at_nested/version.rb
105
+ - values_at_nested.gemspec
106
+ homepage: https://github.com/abarrak/values_at_nested
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.6.11
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: An extended version of ruby's `#values_at` hash method.
130
+ test_files: []