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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hash/as/tree.rb +21 -16
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fc1b4473793caf0d2f652d6bdcb4402a5e14b7c
4
- data.tar.gz: b26ef30216c838d0bed808fd8b7c50f61c099067
3
+ metadata.gz: dd7dc34f2a588ef21cc27402c1a2f1b1f26a03f0
4
+ data.tar.gz: 0e99b251cc97535c93ad43fff06beffd45dfcd19
5
5
  SHA512:
6
- metadata.gz: fde51f1225de3a4f7ec1569f09241d8ea227fc15b79816edd2802bdbc6d5499f7127d8f12f711b82240184c6912eeeb0f5a0bfdb57475082a73494aca2d14389
7
- data.tar.gz: a9d5fe6598febfce9c9a3bf7b0bd854365c6b2384b5c9baa5ec72ea30ce6ec7bea49d76f47f2903771614eccaeb6aafbe63254175ef339e1fe4d127750e85f20
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
- nodes.push root_node
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
- if parent = passed_node.parent
32
- parent.value.delete passed_node.key
33
- parent.value[returned_node.key] = returned_node.value
34
- end
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
- root_node.to_h
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 to_h
50
- if key
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.3
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-02-05 00:00:00.000000000 Z
11
+ date: 2017-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: to_proc