hash-as-tree 0.0.0

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 +7 -0
  2. data/lib/hash/as/tree.rb +78 -0
  3. metadata +57 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48181687858a679f1ca8ac394e8df9b85d73a8e4
4
+ data.tar.gz: 2462500e3cebc6a79e3b858ec546ca7fb87a6197
5
+ SHA512:
6
+ metadata.gz: e662810002cf4c3eaf8a47c048d681ba0b01a8794a9454679f78e4975c41d6b91421042897159b707ccf0239c00a9dc20d70c29b7b3ca8f4583406cc328b9e95
7
+ data.tar.gz: ece390faebd260ff64aafdad24f414397b17f7e51e7290fe260b730c65d09f5341fb17a3ed09cba94d8d5a529525242f3ee44977c33f8ee09872ab2500a9d693
@@ -0,0 +1,78 @@
1
+ require 'to_proc/all'
2
+
3
+ class Hash
4
+ module As
5
+ module Tree
6
+ refine Hash do
7
+ def traverse
8
+ return to_enum(__method__) unless block_given?
9
+
10
+ nodes = Queue.new
11
+ nodes.push Node.new value: self
12
+
13
+ until nodes.empty?
14
+ current_node = nodes.pop
15
+ yield current_node
16
+ current_node.children.each &[nodes, :push]
17
+ end
18
+ end
19
+
20
+ def rewrite
21
+ nodes, root_node = Queue.new, (Node.new value: self)
22
+ nodes.push root_node
23
+
24
+ until nodes.empty?
25
+ passed_node = nodes.pop
26
+ returned_node = yield passed_node
27
+
28
+ if passed_node == returned_node
29
+ passed_node.children.each &[nodes, :push]
30
+ else
31
+ passed_node.parent.value.delete passed_node.key
32
+ passed_node.parent.value[returned_node.key] = returned_node.value
33
+ returned_node.children.each &[nodes, :push]
34
+ end
35
+ end
36
+
37
+ root_node.to_h
38
+ end
39
+ end
40
+
41
+ class Node
42
+ def initialize value: nil, key: nil, parent: nil
43
+ @value, @key, @parent = value, key, parent
44
+ end
45
+ attr_accessor :value, :key, :parent
46
+
47
+ def to_h
48
+ if key
49
+ if value.is_a? Hash
50
+ { key => children.map(&:to_h).reduce(&:merge) }
51
+ else
52
+ { key => value }
53
+ end
54
+ else
55
+ children.map(&:to_h).reduce(&:merge)
56
+ end
57
+ end
58
+
59
+ def with **kwargs
60
+ Node.new \
61
+ value: (kwargs[:value] or value),
62
+ key: (kwargs[:key] or key),
63
+ parent: (kwargs[:parent] or parent)
64
+ end
65
+
66
+ def children
67
+ if value.is_a? Hash
68
+ value.map do |key, value|
69
+ Node.new value: value, key: key, parent: self
70
+ end
71
+ else
72
+ []
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-as-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Chernow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: to_proc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.7
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/hash/as/tree.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.6.8
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: A refinements for Hash to use it as a tree.
57
+ test_files: []