hash_at_path 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hash_at_path.gemspec +23 -0
- data/lib/hash_at_path.rb +5 -0
- data/lib/hash_at_path/core_ext/hash/at_path.rb +58 -0
- data/lib/hash_at_path/path.rb +34 -0
- data/lib/hash_at_path/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a59bb1a600636c3c0b938a5d688d6d81e61abb2
|
4
|
+
data.tar.gz: fb99650096a690e808209ce73cdb5f4807edcc12
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69a4778dde933d25cc1c032c0d3f912f00dcd2ef0095f509e3f7afa76098491293273811d8e2733cd21ef4cde05c114cf5cc4c62204e6cceac1b18eaefee66e2
|
7
|
+
data.tar.gz: 276d87acac4b4222104f5fd4c953efd906e30891a3b5da2aec5ed19e79b43dd60219155f2df02955a9827c3c39dde92d8325bcf530d86e4b7b23af08c18fae7a
|
data/.gitignore
ADDED
data/.rspec
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) 2015 Chad Fennell
|
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,62 @@
|
|
1
|
+
# HashAtPath
|
2
|
+
|
3
|
+
Provides a simple xpath-like syntax for retrieving values from a hash.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hash_at_path'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hash_at_path
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Paths are delimited with forward slashes and predicates are enclosed by brackets:
|
24
|
+
|
25
|
+
| Predicate | Meaning |
|
26
|
+
| ------------- |:-------------|
|
27
|
+
| [first()] | select the first item from an array|
|
28
|
+
| [last()] | select the last item from an array |
|
29
|
+
| [0] | select an array item at a given index |
|
30
|
+
| [*] | all items (path selections that follow this declaration will be applied to each element) |
|
31
|
+
|
32
|
+
|
33
|
+
```
|
34
|
+
> {'foo' => ['bar', 'baz']}.at_path('/')
|
35
|
+
{'foo' => ['bar', 'baz']}
|
36
|
+
> {'foo' => {'bar' => [{'baz' => 'bam'}, {'baz' => 'jam'}, {'baz' => ['ham']}]}}.at_path('foo/bar[*]/baz')
|
37
|
+
["bam", "jam", ["ham"]]
|
38
|
+
> {'foo' => ['bar', 'baz']}.at_path('foo[1]')
|
39
|
+
'baz'
|
40
|
+
> {'foo' => ['bar', 'baz', 'barn']}.at_path('foo[-1]')
|
41
|
+
'barn'
|
42
|
+
> {'foo' => ['bar', 'baz']}.at_path('foo[last()]')
|
43
|
+
'baz'
|
44
|
+
> {'foo' => ['bar', 'baz']}.at_path('foo[first()]')
|
45
|
+
'bar'
|
46
|
+
> {'foo' => ['bar', 'baz']}.at_path('WAT')
|
47
|
+
nil
|
48
|
+
```
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
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.
|
53
|
+
|
54
|
+
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).
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it ( https://github.com/chadfennell/hash_at_path/fork )
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hash_at_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,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_at_path/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_at_path"
|
8
|
+
spec.version = HashAtPath::VERSION
|
9
|
+
spec.authors = ["chadfennell"]
|
10
|
+
spec.email = ["libsys@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Extends Hash with xpath like syntax for retrieving hash values.}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.1.0"
|
23
|
+
end
|
data/lib/hash_at_path.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module HashAtPath
|
2
|
+
module CoreExt
|
3
|
+
module Hash
|
4
|
+
module AtPath
|
5
|
+
# Returns a value at the end of an xpath-like/lite path
|
6
|
+
def at_path(path)
|
7
|
+
out = {}
|
8
|
+
if path == '/'
|
9
|
+
self
|
10
|
+
else
|
11
|
+
extract(self, path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def extract(val, path_string)
|
18
|
+
predicate = nil
|
19
|
+
Path.parse(path_string).each do |path_parts|
|
20
|
+
return nil if val.nil?
|
21
|
+
predicate = path_parts[:predicate] unless predicate == '*'
|
22
|
+
val, predicate = get_value_at_path(predicate, path_parts[:path], val)
|
23
|
+
val = val[predicate.to_i] unless blank?(predicate) || predicate == '*' || !issa(val, 'Array')
|
24
|
+
end
|
25
|
+
val
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_value_at_path(predicate, path, val)
|
29
|
+
if predicate == '*' && issa(val, 'Array') && !blank?(path)
|
30
|
+
val = val.map {|item| unwind_path(item, path)}
|
31
|
+
predicate = nil
|
32
|
+
elsif !blank?(path) && issa(val, 'Hash')
|
33
|
+
val = unwind_path(val, path)
|
34
|
+
end
|
35
|
+
[val, predicate]
|
36
|
+
end
|
37
|
+
|
38
|
+
def issa(item, classname)
|
39
|
+
item.class.to_s == classname
|
40
|
+
end
|
41
|
+
|
42
|
+
def unwind_path(val, path)
|
43
|
+
path_keys(path).inject(val) { |item, path_key| (issa(item, 'Hash')) ? item.fetch(path_key, nil) : nil}
|
44
|
+
end
|
45
|
+
|
46
|
+
# Be nice: remove empty path items (e.g. when we get a leading slash)
|
47
|
+
def path_keys(path)
|
48
|
+
path.split("/").reject { |c| c.empty? }
|
49
|
+
end
|
50
|
+
|
51
|
+
# Stolen from activesupport/lib/active_support/core_ext/object/blank.rb
|
52
|
+
def blank?(object)
|
53
|
+
object.respond_to?(:empty?) ? !!object.empty? : !object
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module HashAtPath
|
2
|
+
class Path
|
3
|
+
class << self
|
4
|
+
def parse(path_string)
|
5
|
+
path_string = replace_predicates(path_string)
|
6
|
+
vals = extract_path_data(path_string)
|
7
|
+
end
|
8
|
+
|
9
|
+
def replace_predicates(path_string)
|
10
|
+
path_string.gsub!(/first\(\)/, "0")
|
11
|
+
path_string.gsub!(/last\(\)/, "-1")
|
12
|
+
path_string
|
13
|
+
end
|
14
|
+
|
15
|
+
# Extracts paths and predicates from the provide full path
|
16
|
+
def extract_path_data(path_string)
|
17
|
+
results = array_wrap(path_string.scan(/([^\[]*)(\[)*(\d|\-\d|\*)*(\])*/i))
|
18
|
+
results.map {|match| {:path => match[0], :predicate => match[2]}}
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
# Stolen from activesupport/lib/active_support/core_ext/array/wrap.rb
|
23
|
+
def array_wrap(object)
|
24
|
+
if object.nil?
|
25
|
+
[]
|
26
|
+
elsif object.respond_to?(:to_ary)
|
27
|
+
object.to_ary || [object]
|
28
|
+
else
|
29
|
+
[object]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_at_path
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- chadfennell
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-25 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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.1.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.1.0
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- libsys@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- hash_at_path.gemspec
|
72
|
+
- lib/hash_at_path.rb
|
73
|
+
- lib/hash_at_path/core_ext/hash/at_path.rb
|
74
|
+
- lib/hash_at_path/path.rb
|
75
|
+
- lib/hash_at_path/version.rb
|
76
|
+
homepage:
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Extends Hash with xpath like syntax for retrieving hash values.
|
100
|
+
test_files: []
|