hash_pick 0.2.0 → 0.2.1
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/README.md +1 -1
- data/lib/hash_pick/version.rb +1 -1
- data/lib/hash_pick.rb +8 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ab66b9a5a34241d4501d248ac0493ca8254e81c
|
4
|
+
data.tar.gz: 3234a0b99d870f14bb7e24c978132d9aef498fce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '059c479e8ddb54cda612938a2016df22b831b09b987305eb6a8c01afd8c0b9df251d88736b1021b3b5ef3c004052f9dc2f5c65f679690afdbdd79b2f2596bbd1'
|
7
|
+
data.tar.gz: 3b8eaddc6159f30cc3cf3653b62ca2a9d34e7caf3f99613a41b3d612c27d45cb945cf7ea2d8fdd8a5aefc495f42498eca96cdb77b2822024717fb0aa53a9dfbf
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ A module of utility methods for hash path queries.
|
|
6
6
|
|
7
7
|
## Documentation
|
8
8
|
|
9
|
-
For documentation of the released gem
|
9
|
+
For documentation, follow the _Documentation_ link of the released gem on [rubygems.org](https://rubygems.org/gems/hash_pick).
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
data/lib/hash_pick/version.rb
CHANGED
data/lib/hash_pick.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
##
|
4
4
|
# Fetch a value from a nested dictionary
|
5
5
|
#
|
6
|
-
# Provides methods for fetching a value from a nested dictionary (an object
|
7
|
-
# using a key path expressed as a list (an object
|
6
|
+
# Provides methods for fetching a value from a nested dictionary (an object structure whose root and branch nodes implement +[]+),
|
7
|
+
# using a key path expressed as a list (an +Enumerable+ object whose +each+ method yields a path key).
|
8
8
|
#
|
9
9
|
# The key path is iterated. In each iteration, the key is looked up in the dictionary, and the value found is used
|
10
10
|
# as the dictionary for the next iteration. Lookup failure immediately returns nil.
|
@@ -53,13 +53,7 @@ module HashPick
|
|
53
53
|
assert_non_nil_path_keys(path)
|
54
54
|
|
55
55
|
pick(hash, path) do |acc, p|
|
56
|
-
|
57
|
-
acc.fetch(p.to_sym)
|
58
|
-
elsif acc.include?(p.to_s)
|
59
|
-
acc[p.to_s]
|
60
|
-
else
|
61
|
-
throw :break
|
62
|
-
end
|
56
|
+
acc[p.to_sym].nil? ? acc[p.to_s] : acc[p.to_sym]
|
63
57
|
end
|
64
58
|
end
|
65
59
|
|
@@ -87,17 +81,11 @@ module HashPick
|
|
87
81
|
# }
|
88
82
|
# }
|
89
83
|
#
|
90
|
-
# HashPick.pick(dict, [:sheldon, :first_name])
|
91
|
-
# throw :break unless p[:live] and p.include?(k)
|
92
|
-
# p[k]
|
93
|
-
# end
|
84
|
+
# HashPick.pick(dict, [:sheldon, :first_name]) { |p, k| p[k] if p[:live] }
|
94
85
|
# # => "Sheldon"
|
95
86
|
#
|
96
|
-
# HashPick.pick(dict, [:charles, :first_name])
|
97
|
-
#
|
98
|
-
# p[k]
|
99
|
-
# end
|
100
|
-
# # => "Hearn"
|
87
|
+
# HashPick.pick(dict, [:charles, :first_name]) { |p, k| p[k] if p[:live] }
|
88
|
+
# # => nil
|
101
89
|
#
|
102
90
|
# @param [Hash] hash
|
103
91
|
# the dictionary to apply the +path+ to.
|
@@ -129,7 +117,7 @@ module HashPick
|
|
129
117
|
end
|
130
118
|
|
131
119
|
def dictionary?(hash)
|
132
|
-
hash.respond_to?(:
|
120
|
+
hash.respond_to?(:[])
|
133
121
|
end
|
134
122
|
|
135
123
|
def assert_non_nil_path_keys(path)
|
@@ -137,7 +125,7 @@ module HashPick
|
|
137
125
|
end
|
138
126
|
|
139
127
|
def assert_enumerable_path(path)
|
140
|
-
raise ArgumentError.new("path is not enumerable") unless path.
|
128
|
+
raise ArgumentError.new("path is not enumerable") unless path.is_a?(Enumerable)
|
141
129
|
end
|
142
130
|
|
143
131
|
end
|