hierarchical_graph 1.0.1 → 1.0.2
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/lib/hierarchical_graph.rb +20 -5
- data/lib/hierarchical_graph/version.rb +1 -1
- data/spec/hierarchical_graph_spec.rb +12 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6c1ce470102ca17f36f40fe0f861c221c18237b46fa07d066baed98895501c8
|
4
|
+
data.tar.gz: '08cf95bf13e8217262973a222cb213de9559777170253460acfb6b9856316f1d'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e14241d62a3ffac4fa02fcf4c0ed37f7456658510a0e26ef17c63155dd5f9368fbe21dedd03c52182d86189523ef98df0cd4e1018c770f971e2b2e2077c1258
|
7
|
+
data.tar.gz: 97ed130cabcd32d51266c9fe5bcf7c606c11577746c87a5ba2e169b756b4abd7f9002def04e9bc0218ca8e7df838a0c9e843c634e90fde72ce6cbb771cebbf8b
|
data/lib/hierarchical_graph.rb
CHANGED
@@ -31,6 +31,8 @@ class HierarchicalGraph
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def add_node(id, attributes={})
|
34
|
+
validate_not_present! id
|
35
|
+
|
34
36
|
clear_cache
|
35
37
|
parent_to_children[id] = Set.new
|
36
38
|
child_to_parents[id] = Set.new
|
@@ -38,7 +40,7 @@ class HierarchicalGraph
|
|
38
40
|
end
|
39
41
|
|
40
42
|
def remove_node(id)
|
41
|
-
|
43
|
+
validate_present! id
|
42
44
|
|
43
45
|
parent_to_children[id].each { |child_id| child_to_parents[child_id].delete id }
|
44
46
|
child_to_parents[id].each { |parent_id| parent_to_children[parent_id].delete id }
|
@@ -53,7 +55,7 @@ class HierarchicalGraph
|
|
53
55
|
end
|
54
56
|
|
55
57
|
def add_relation(parent_id:, child_id:)
|
56
|
-
|
58
|
+
validate_present! parent_id, child_id
|
57
59
|
|
58
60
|
clear_cache
|
59
61
|
parent_to_children[parent_id] << child_id
|
@@ -63,7 +65,7 @@ class HierarchicalGraph
|
|
63
65
|
end
|
64
66
|
|
65
67
|
def remove_relation(parent_id:, child_id:)
|
66
|
-
|
68
|
+
validate_present! parent_id, child_id
|
67
69
|
|
68
70
|
clear_cache
|
69
71
|
parent_to_children[parent_id].delete child_id
|
@@ -73,20 +75,28 @@ class HierarchicalGraph
|
|
73
75
|
end
|
74
76
|
|
75
77
|
def parents_of(id)
|
78
|
+
validate_present! id
|
79
|
+
|
76
80
|
child_to_parents[id].map { |node_id| nodes[node_id] }
|
77
81
|
end
|
78
82
|
|
79
83
|
def children_of(id)
|
84
|
+
validate_present! id
|
85
|
+
|
80
86
|
parent_to_children[id].map { |node_id| nodes[node_id] }
|
81
87
|
end
|
82
88
|
|
83
89
|
def ancestors_of(id)
|
90
|
+
validate_present! id
|
91
|
+
|
84
92
|
ancestors_cache[id] ||= parents_of(id).flat_map do |parent|
|
85
93
|
ancestors_of(parent.id) + [parent]
|
86
94
|
end.uniq(&:id)
|
87
95
|
end
|
88
96
|
|
89
97
|
def descendants_of(id)
|
98
|
+
validate_present! id
|
99
|
+
|
90
100
|
children_of(id).flat_map do |child|
|
91
101
|
[child] + descendants_of(child.id)
|
92
102
|
end.uniq(&:id)
|
@@ -101,9 +111,14 @@ class HierarchicalGraph
|
|
101
111
|
|
102
112
|
attr_reader :nodes, :parent_to_children, :child_to_parents, :ancestors_cache, :descendants_cache
|
103
113
|
|
104
|
-
def
|
114
|
+
def validate_present!(*ids)
|
105
115
|
invalid_ids = ids.reject { |id| nodes.key? id }
|
106
|
-
raise "
|
116
|
+
raise "Nodes not found: #{invalid_ids.join(', ')}" if invalid_ids.any?
|
117
|
+
end
|
118
|
+
|
119
|
+
def validate_not_present!(*ids)
|
120
|
+
invalid_ids = ids.select { |id| nodes.key? id }
|
121
|
+
raise "Nodes already exist: #{invalid_ids.join(', ')}" if invalid_ids.any?
|
107
122
|
end
|
108
123
|
|
109
124
|
def clear_cache
|
@@ -17,6 +17,15 @@ describe HierarchicalGraph do
|
|
17
17
|
graph.count.must_equal 1
|
18
18
|
end
|
19
19
|
|
20
|
+
it 'Add duplicated node' do
|
21
|
+
graph = HierarchicalGraph.new
|
22
|
+
|
23
|
+
graph.add_node 1
|
24
|
+
|
25
|
+
error = proc { graph.add_node 1 }.must_raise RuntimeError
|
26
|
+
error.message.must_equal "Nodes already exist: 1"
|
27
|
+
end
|
28
|
+
|
20
29
|
it 'Remove node' do
|
21
30
|
graph = HierarchicalGraph.new
|
22
31
|
graph.add_node 1
|
@@ -31,7 +40,7 @@ describe HierarchicalGraph do
|
|
31
40
|
graph = HierarchicalGraph.new
|
32
41
|
|
33
42
|
error = proc { graph.remove_node 1 }.must_raise RuntimeError
|
34
|
-
error.message.must_equal "
|
43
|
+
error.message.must_equal "Nodes not found: 1"
|
35
44
|
end
|
36
45
|
|
37
46
|
it 'Add relation' do
|
@@ -52,7 +61,7 @@ describe HierarchicalGraph do
|
|
52
61
|
graph = HierarchicalGraph.new
|
53
62
|
|
54
63
|
error = proc { graph.add_relation parent_id: 1, child_id: 2 }.must_raise RuntimeError
|
55
|
-
error.message.must_equal "
|
64
|
+
error.message.must_equal "Nodes not found: 1, 2"
|
56
65
|
end
|
57
66
|
|
58
67
|
it 'Remove relation' do
|
@@ -102,7 +111,7 @@ describe HierarchicalGraph do
|
|
102
111
|
graph = HierarchicalGraph.new
|
103
112
|
|
104
113
|
error = proc { graph.remove_relation parent_id: 1, child_id: 2 }.must_raise RuntimeError
|
105
|
-
error.message.must_equal "
|
114
|
+
error.message.must_equal "Nodes not found: 1, 2"
|
106
115
|
end
|
107
116
|
|
108
117
|
it 'Roots' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hierarchical_graph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|