hash-as-tree 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hash/as/tree.rb +21 -16
- 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: dd7dc34f2a588ef21cc27402c1a2f1b1f26a03f0
|
4
|
+
data.tar.gz: 0e99b251cc97535c93ad43fff06beffd45dfcd19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 035ba398bb143a3983003e8b51d9624c03fb5bcc7606b4fbc2e5e07056f77a1b3bb33d8123f072c3d62b25cf4122f6487c03686ee090c86bed60706a0cfaf628
|
7
|
+
data.tar.gz: bf728a6e41563a8e949a71df93db950c2cf29cc3d9bfa6e14ad5ae4de24ddcb412f04182167fbfb84082c738ccf69ed3150111a50ae0122634388ffb7aea0bfe
|
data/lib/hash/as/tree.rb
CHANGED
@@ -3,6 +3,10 @@ require 'to_proc/all'
|
|
3
3
|
class Hash
|
4
4
|
module As
|
5
5
|
module Tree
|
6
|
+
COMBINATOR = -> hash, key do
|
7
|
+
hash[key] = Hash.new &COMBINATOR
|
8
|
+
end
|
9
|
+
|
6
10
|
refine Hash do
|
7
11
|
def traverse
|
8
12
|
return to_enum(__method__) unless block_given?
|
@@ -18,8 +22,13 @@ class Hash
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def rewrite
|
25
|
+
hash = dup
|
26
|
+
hash.default_proc = COMBINATOR
|
27
|
+
|
21
28
|
nodes, root_node = Queue.new, (Node.new value: self)
|
22
|
-
|
29
|
+
each do |key, value|
|
30
|
+
nodes.push Node.new key: key, value: value, parent: root_node
|
31
|
+
end
|
23
32
|
|
24
33
|
until nodes.empty?
|
25
34
|
passed_node = nodes.pop
|
@@ -28,15 +37,19 @@ class Hash
|
|
28
37
|
if passed_node == returned_node
|
29
38
|
passed_node.children.each &[nodes, :push]
|
30
39
|
else
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
40
|
+
path = passed_node.parent.path
|
41
|
+
cursor = if path.empty?
|
42
|
+
hash
|
43
|
+
else
|
44
|
+
hash.dig *path
|
45
|
+
end
|
46
|
+
cursor[returned_node.key] = returned_node.value
|
35
47
|
returned_node.children.each &[nodes, :push]
|
36
48
|
end
|
37
49
|
end
|
38
50
|
|
39
|
-
|
51
|
+
hash.default_proc = default_proc
|
52
|
+
hash
|
40
53
|
end
|
41
54
|
end
|
42
55
|
|
@@ -46,16 +59,8 @@ class Hash
|
|
46
59
|
end
|
47
60
|
attr_accessor :value, :key, :parent
|
48
61
|
|
49
|
-
def
|
50
|
-
|
51
|
-
if value.is_a?(Hash) && (not value.empty?)
|
52
|
-
{ key => children.map(&:to_h).reduce(&:merge) }
|
53
|
-
else
|
54
|
-
{ key => value }
|
55
|
-
end
|
56
|
-
else
|
57
|
-
children.map(&:to_h).reduce(&:merge)
|
58
|
-
end
|
62
|
+
def path
|
63
|
+
@path ||= [*parent&.path, *key]
|
59
64
|
end
|
60
65
|
|
61
66
|
def with **kwargs
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-as-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: to_proc
|