hashlib 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hashlib.rb +59 -35
- metadata +2 -2
data/lib/hashlib.rb
CHANGED
@@ -13,34 +13,54 @@ class Hash
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def get(path, default=nil)
|
16
|
-
|
17
|
-
|
18
|
-
begin
|
19
|
-
path = path.to_s.split('.') unless path.is_a?(Array)
|
16
|
+
rv = self
|
20
17
|
|
21
|
-
|
22
|
-
|
18
|
+
# make path an array if not already
|
19
|
+
if not path.is_a?(Array)
|
20
|
+
path = path.split('.')
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
23
|
+
# step through path components...
|
24
|
+
path.each_index do |i|
|
25
|
+
# hashes: if the current path component is in the hash, set the pointer
|
26
|
+
# and move on to the next path component
|
27
|
+
# otherwise return default value
|
28
|
+
#
|
29
|
+
if rv.is_a?(Hash)
|
30
|
+
if rv.has_key?(path[i])
|
31
|
+
rv = rv[path[i]]
|
32
|
+
next
|
33
|
+
else
|
34
|
+
return default
|
35
|
+
end
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
# arrays: flatten into one array, iterate over it. for each element:
|
38
|
+
# if it is a hash, recurse into it from the next path component
|
39
|
+
# otherwise return it
|
40
|
+
# the resulant pointer should be fully populated from this point down
|
41
|
+
#
|
42
|
+
elsif rv.is_a?(Array)
|
43
|
+
rv = (rv.flatten.collect do |j|
|
44
|
+
if j.is_a?(Hash)
|
45
|
+
j.get(path[(i)..-1], default)
|
46
|
+
else
|
47
|
+
j
|
32
48
|
end
|
49
|
+
end).compact
|
33
50
|
|
34
|
-
|
35
|
-
else
|
36
|
-
root = (root[key] rescue nil)
|
37
|
-
end
|
38
|
-
end
|
51
|
+
next
|
39
52
|
|
40
|
-
|
41
|
-
|
42
|
-
|
53
|
+
# scalars: if we're still in this loop and trying to look for a path component
|
54
|
+
# inside of a scalar value, then that won't exist and we just return default
|
55
|
+
else
|
56
|
+
return default
|
57
|
+
end
|
43
58
|
end
|
59
|
+
|
60
|
+
return default if rv.nil? or (rv.is_a?(Array) and rv.flatten.compact.empty?)
|
61
|
+
|
62
|
+
# good job! you made it here!
|
63
|
+
return rv
|
44
64
|
end
|
45
65
|
|
46
66
|
def set(path, value)
|
@@ -148,23 +168,27 @@ class Array
|
|
148
168
|
if i.is_a?(Hash)
|
149
169
|
components = group_by
|
150
170
|
components = components.collect{|j| i.get(j) }
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
171
|
+
components = [*components.first].product([*components.last])
|
172
|
+
|
173
|
+
components.each do |component|
|
174
|
+
path = []
|
175
|
+
stack = []
|
176
|
+
|
177
|
+
component.compact.each do |c|
|
178
|
+
if c.is_a?(Array)
|
179
|
+
path += [stack].product(c.flatten).collect{|i| i.flatten }
|
180
|
+
stack = []
|
181
|
+
else
|
182
|
+
stack << c
|
183
|
+
end
|
160
184
|
end
|
161
|
-
end
|
162
185
|
|
163
|
-
|
186
|
+
path = [component] if path.empty?
|
164
187
|
|
165
|
-
|
166
|
-
|
167
|
-
|
188
|
+
path.each do |parts|
|
189
|
+
parts[-1] = :null if parts[-1].nil?
|
190
|
+
rv.set(parts, rv.get(parts, 0) + 1)
|
191
|
+
end
|
168
192
|
end
|
169
193
|
end
|
170
194
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashlib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
40
|
version: '0'
|
41
41
|
requirements: []
|
42
42
|
rubyforge_project:
|
43
|
-
rubygems_version: 1.8.
|
43
|
+
rubygems_version: 1.8.23
|
44
44
|
signing_key:
|
45
45
|
specification_version: 3
|
46
46
|
summary: Useful utility methods for working with Ruby hashes
|