hash_path 0.4.0 → 0.5.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/hash_path.rb +21 -0
- data/lib/hash_path/version.rb +1 -1
- data/spec/hash_path_spec.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41873ea7e48e65009bc87fd7a332086b168ba612
|
|
4
|
+
data.tar.gz: aee0abeeb18db90fe34ffb138d45fdd92ed12844
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1b87e891baa0ef21473bbcadf5cb1d49b76a5e6ca5d0c1e9bc280510e8c66423cd5bbc7d661f0f427abe8caff25cde9bb0d3f8c2d982a39809777d6d818c33ef
|
|
7
|
+
data.tar.gz: 12801e82c63f51e9b5a9614393784f71c026cd2589a2de49e561abdb252137868f478a090add7592b7caa62f4eba9bff52febba518c7bc21add8e4e528e20900
|
data/CHANGELOG.md
CHANGED
data/lib/hash_path.rb
CHANGED
|
@@ -2,6 +2,22 @@ require "hash_path/version"
|
|
|
2
2
|
module HashPath
|
|
3
3
|
class PathNotFound < StandardError; end
|
|
4
4
|
|
|
5
|
+
module NilPath
|
|
6
|
+
def at_path(path)
|
|
7
|
+
nil
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def at_path!(path)
|
|
11
|
+
raise PathNotFound, path
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def flatten_key_paths(*)
|
|
15
|
+
{}
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
5
21
|
DELIMITER = '.'
|
|
6
22
|
|
|
7
23
|
# Looks up the path provided
|
|
@@ -70,6 +86,11 @@ module HashPath
|
|
|
70
86
|
end
|
|
71
87
|
end
|
|
72
88
|
|
|
89
|
+
|
|
73
90
|
class Hash
|
|
74
91
|
include HashPath
|
|
75
92
|
end
|
|
93
|
+
|
|
94
|
+
class NilClass
|
|
95
|
+
include HashPath::NilPath
|
|
96
|
+
end
|
data/lib/hash_path/version.rb
CHANGED
data/spec/hash_path_spec.rb
CHANGED
|
@@ -9,9 +9,19 @@ RSpec.describe HashPath do
|
|
|
9
9
|
it 'returns a value if it is found' do
|
|
10
10
|
expect({foo: {bar: 5}}.at_path('foo.bar')).to eq(5)
|
|
11
11
|
end
|
|
12
|
+
|
|
13
|
+
it 'returns nil if the starting object is nil' do
|
|
14
|
+
expect(nil.at_path('foo')).to eq(nil)
|
|
15
|
+
end
|
|
12
16
|
end
|
|
13
17
|
|
|
14
18
|
describe '#at_path!' do
|
|
19
|
+
it 'raises when nil' do
|
|
20
|
+
expect {
|
|
21
|
+
nil.at_path!('foo')
|
|
22
|
+
}.to raise_error(HashPath::PathNotFound)
|
|
23
|
+
end
|
|
24
|
+
|
|
15
25
|
it 'looks up a key given by a string' do
|
|
16
26
|
expect({'foo' => 5}.at_path!('foo')).to eq(5)
|
|
17
27
|
end
|
|
@@ -77,6 +87,10 @@ RSpec.describe HashPath do
|
|
|
77
87
|
it 'un-nests hashes' do
|
|
78
88
|
expect({a: 5, b: {c: 7}}.flatten_key_paths).to eq({'a' => 5, 'b.c' => 7})
|
|
79
89
|
end
|
|
90
|
+
|
|
91
|
+
it 'returns an empty hash for nil' do
|
|
92
|
+
expect(nil.flatten_key_paths).to eq({})
|
|
93
|
+
end
|
|
80
94
|
end
|
|
81
95
|
|
|
82
96
|
describe 'HashPath::VERSION' do
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hash_path
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel Neighman
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-12-
|
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|