hash_pick 0.1.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: 0b75650026df7c097b17e4a1fa531839b0433ac1
4
+ data.tar.gz: da6ed3ea00514b66eca4d2224683144ebcc2f144
5
+ SHA512:
6
+ metadata.gz: e8d0f5ead75097e5d2ade4e0b30d44ef2fa374763d6ccfd95d5827fd68d06a561b23e073b5ecda1dc3dfd9b3cbc970873ceb25fadf7ee6ee866171d028153933
7
+ data.tar.gz: 724582a7f839502b46603823ccc1aae482810eb856d00937e4d80a7d440c1063b82c3e1143efebe2fe637555f6605e7172d1207ae2023f66474bb792425a42cf
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --no-private
2
+ --exclude README.md
3
+ --readme README.rdoc
4
+ -
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_path.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ [![Gem Version](https://badge.fury.io/rb/hash_path.svg)](http://badge.fury.io/rb/hash_path) [![Build Status](https://travis-ci.org/hetznerZA/hash_path.svg?branch=master)](https://travis-ci.org/hetznerZA/hash_path) [![Dependency Status](https://gemnasium.com/hetznerZA/hash_path.svg)](https://gemnasium.com/hetznerZA/hash_path)
2
+
3
+ # HashPath
4
+
5
+ A module of utility methods for hash path queries.
6
+
7
+ ## Documentation
8
+
9
+ For documentation of the released gem, see [rubydoc.info](http://www.rubydoc.info/gems/hash_path).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'hash_path'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install hash_path
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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`, 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hetznerZA/hash_path.
36
+
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ = HashPath
2
+
3
+ Provides utility methods for hash path queries.
4
+
5
+ Hash paths can be expressed as lists of keys of these types:
6
+
7
+ * Object.
8
+ * String.
9
+ * Symbol.
10
+ * Eithe String or Symbol.
11
+
12
+ The methods are provided by the module {HashPath}.
13
+
14
+ The implementation is covered by an rspec test suite in the {https://github.com/hetznerZA/hash_path HashPath repo}.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :default => :spec
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hash_path"
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
@@ -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
data/hash_pick.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hash_pick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hash_pick"
8
+ spec.version = HashPick::VERSION
9
+ spec.authors = ["Sheldon Hearn"]
10
+ spec.email = ["sheldonh@starjuice.net"]
11
+
12
+ spec.summary = %q{Hash path query}
13
+ spec.description = %q{A hash path query support library}
14
+ spec.homepage = "https://github.com/hetznerZA/hash_pick"
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.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.5"
24
+ end
@@ -0,0 +1,3 @@
1
+ module HashPick
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hash_pick.rb ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ##
4
+ # Fetch a value from a nested dictionary
5
+ #
6
+ # Provides methods for fetching a value from a nested dictionary (an object that implements +include?+ and +fetch+),
7
+ # using a key path expressed as a list (+Enumerable+).
8
+ #
9
+ # The key path is iterated. In each iteration, the key is looked up in the dictionary, and the value found is used
10
+ # as the dictionary for the next iteration. Lookup failure immediately returns nil.
11
+ #
12
+ # @example Indifferent hash path
13
+ #
14
+ # require "hash_pick"
15
+ #
16
+ # dict = {
17
+ # people: {
18
+ # "sheldon": {
19
+ # first_name: "Sheldon",
20
+ # last_name: "Hearn",
21
+ # contacts: {
22
+ # "email": "sheldonh@starjuice",
23
+ # }
24
+ # },
25
+ # "charles": {
26
+ # first_name: "Charles",
27
+ # last_name: "Mulder",
28
+ # }
29
+ # }
30
+ # }
31
+ #
32
+ # HashPick[dict, %w{people sheldon contacts email}] # -> "sheldonh@starjuice"
33
+ # HashPick[dict, %w{people charles contacts email}] # -> nil
34
+ #
35
+ module HashPick
36
+
37
+ ##
38
+ # Fetch value from dictionary using string or symbol key path
39
+ #
40
+ # Each value in the path is used as a symbol or, if symbol lookup fails, a string.
41
+ #
42
+ # @param [Hash] hash
43
+ # the dictionary to apply the +path+ to.
44
+ # @param [Array] path
45
+ # an ordered list of keys that implement +to_s+ and +to_sym+.
46
+ # @return [Object] the value found at the path in the dictionary.
47
+ # @return [nil] if any key lookup failed.
48
+ #
49
+ def self.indifferent(hash, path)
50
+ path.inject(hash) do |acc, p|
51
+ break unless acc.respond_to?(:include?) && acc.respond_to?(:fetch)
52
+ if acc.include?(p.to_sym)
53
+ acc.fetch(p.to_sym)
54
+ elsif acc.include?(p.to_s)
55
+ acc[p.to_s]
56
+ else
57
+ break
58
+ end
59
+ end
60
+ end
61
+
62
+ class << self
63
+ alias_method :[], :indifferent
64
+ end
65
+
66
+ ##
67
+ # Fetch value from dictionary using object key path
68
+ #
69
+ # Each value in the path is used as is.
70
+ #
71
+ # @param [Hash] hash
72
+ # the dictionary to apply the +path+ to.
73
+ # @param [Array] path
74
+ # an ordered list of object keys.
75
+ # @return [Object] the value found at the path in the dictionary.
76
+ # @return [nil] if any key lookup failed.
77
+ #
78
+ def self.object(hash, path)
79
+ path.inject(hash) do |acc, p|
80
+ break unless acc.respond_to?(:include?) && acc.respond_to?(:fetch) && acc.include?(p)
81
+ acc[p]
82
+ end
83
+ end
84
+
85
+ ##
86
+ # Fetch value from dictionary using symbol key path
87
+ #
88
+ # Each value in the path is used as a symbol.
89
+ #
90
+ # @param [Hash] hash
91
+ # the dictionary to apply the +path+ to.
92
+ # @param [Array] path
93
+ # an ordered list of keys that implement +to_sym+.
94
+ # @return [Object] the value found at the path in the dictionary.
95
+ # @return [nil] if any key lookup failed.
96
+ #
97
+ def self.symbol(hash, path)
98
+ object(hash, path.map(&:to_sym))
99
+ end
100
+
101
+ ##
102
+ # Fetch value from dictionary using string key path
103
+ #
104
+ # Each value in the path is used as a string.
105
+ #
106
+ # @param [Hash] hash
107
+ # the dictionary to apply the +path+ to.
108
+ # @param [Array] path
109
+ # an ordered list of keys that implement +to_s+.
110
+ # @return [Object] the value found at the path in the dictionary.
111
+ # @return [nil] if any key lookup failed.
112
+ #
113
+ def self.string(hash, path)
114
+ object(hash, path.map(&:to_s))
115
+ end
116
+
117
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_pick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sheldon Hearn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-13 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ description: A hash path query support library
56
+ email:
57
+ - sheldonh@starjuice.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - ".yardopts"
66
+ - Gemfile
67
+ - README.md
68
+ - README.rdoc
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - hash_pick.gemspec
73
+ - lib/hash_pick.rb
74
+ - lib/hash_pick/version.rb
75
+ homepage: https://github.com/hetznerZA/hash_pick
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Hash path query
98
+ test_files: []
99
+ has_rdoc: