hierarchical_graph 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a3224dabda32c9f8a4b7ba2daff315a9c36282362acc8b509f703039e34d0cb
4
+ data.tar.gz: 8a290679d5683ce0566d6433c829684961fa440581c49ccedec02c27c22839f8
5
+ SHA512:
6
+ metadata.gz: 0b5540cc426ca14fbd4ca929442ee493fcb6396665e801c6755dafaa01bc17ff03010d10d80cfe75354e0345b60d01b57f68575a17a08a3904463825aad54947
7
+ data.tar.gz: 52d18fa51a604a5b8f3724e604d430ef286c93bec32a1e7248d40b4808d2a50d6a7abb23bc5f4c6ba4bf81ef7c86ef73e3264a34c02f2dd0961e7e3888faabee
data/.coveralls.yml ADDED
@@ -0,0 +1,2 @@
1
+ service_name: travis-ci
2
+ repo_token: 4teGg1i7u1MnrCFFDhwfZvH9erpoQYq7T
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ hierarchical_graph
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.7.0
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
9
+ - 2.6
10
+ - 2.7
11
+ - jruby-9.2.9.0
12
+ - ruby-head
13
+ - jruby-head
14
+
15
+ matrix:
16
+ fast_finish: true
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+ - rvm: jruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rasti-db.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Gabriel Naiman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # HierarchicalGraph
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/hierarchical_graph.svg)](https://rubygems.org/gems/hierarchical_graph)
4
+ [![Build Status](https://travis-ci.org/gabynaiman/hierarchical_graph.svg?branch=master)](https://travis-ci.org/gabynaiman/hierarchical_graph)
5
+ [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/hierarchical_graph/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/hierarchical_graph?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/gabynaiman/hierarchical_graph.svg)](https://codeclimate.com/github/gabynaiman/hierarchical_graph)
7
+
8
+ Hierarchical graph representation
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'hierarchical_graph'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install hierarchical_graph
25
+
26
+ ## Usage
27
+
28
+ ### Build graph
29
+ ```ruby
30
+ graph = HierarchicalGraph.new
31
+
32
+ graph.add_node 1, name: 'Node 1', other: 1000
33
+ graph.add_node 2
34
+ graph.add_node 3
35
+
36
+ graph.add_relation parent_id: 1, child_id: 2
37
+ graph.add_relation parent_id: 2, child_id: 3
38
+ ```
39
+
40
+ ### Navigate graph
41
+ ```ruby
42
+ graph[1] # <Node 1>
43
+ graph.roots # [<Node 1>, <Node 2>]
44
+ graph.parents_of(3) # [<Node 2>]
45
+ graph.ancestors_of(3) # [<Node 1>, <Node 2>]
46
+ graph.children_of(1) # [<Node 2>]
47
+ graph.descendants_of(1) # [<Node 2>, <Node 3>]
48
+ ```
49
+
50
+ ### Node
51
+ ```ruby
52
+ node = graph[node_id]
53
+
54
+ node.id # node_id
55
+ node.root? # true/false
56
+
57
+ node.data # {key_1: val_1, key_2: val_2}
58
+ node.data[:key_1] # val_1
59
+ node.data[:key_3] = val_3
60
+ node[:key_1] # val_1
61
+ node[:key_3] = val_3
62
+
63
+ node.parents # [<Node>, ...]
64
+ node.ancestors # [<Node>, ...]
65
+ node.children # [<Node>, ...]
66
+ node.descendants # [<Node>, ...]
67
+ ```
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/hierarchical_graph.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << 'spec'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
8
+ t.verbose = false
9
+ t.warning = false
10
+ t.loader = nil if ENV['TEST']
11
+ ENV['TEST'], ENV['LINE'] = ENV['TEST'].split(':') if ENV['TEST'] && !ENV['LINE']
12
+ t.options = ''
13
+ t.options << "--name=/#{ENV['NAME']}/ " if ENV['NAME']
14
+ t.options << "-l #{ENV['LINE']} " if ENV['LINE'] && ENV['TEST']
15
+ end
16
+
17
+ task default: :spec
18
+
19
+ desc 'Pry console'
20
+ task :console do
21
+ require 'hierarchical_graph'
22
+ require 'pry'
23
+ ARGV.clear
24
+ Pry.start
25
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hierarchical_graph/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hierarchical_graph'
8
+ spec.version = HierarchicalGraph::VERSION
9
+ spec.authors = ['Gabriel Naiman']
10
+ spec.email = ['gabynaiman@gmail.com']
11
+ spec.summary = 'Hierarchical graph representation'
12
+ spec.description = 'Hierarchical graph representation'
13
+ spec.homepage = 'https://github.com/gabynaiman/hierarchical_graph'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'rake', '~> 12.3'
22
+ spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
23
+ spec.add_development_dependency 'minitest-colorin', '~> 0.1'
24
+ spec.add_development_dependency 'minitest-line', '~> 0.6'
25
+ spec.add_development_dependency 'simplecov', '~> 0.12'
26
+ spec.add_development_dependency 'coveralls', '~> 0.8'
27
+ spec.add_development_dependency 'pry-nav', '~> 0.2'
28
+ end
@@ -0,0 +1,120 @@
1
+ require_relative 'hierarchical_graph/version'
2
+ require_relative 'hierarchical_graph/node'
3
+
4
+ class HierarchicalGraph
5
+
6
+ include Enumerable
7
+ include TSort
8
+
9
+ def initialize
10
+ @nodes = {}
11
+ @parent_to_children = Hash.new { |h,k| h[k] = Set.new }
12
+ @child_to_parents = Hash.new { |h,k| h[k] = Set.new }
13
+ @ancestors_cache = {}
14
+ @descendants_cache = {}
15
+ end
16
+
17
+ def [](id)
18
+ nodes[id]
19
+ end
20
+
21
+ def each(&block)
22
+ nodes.each_value(&block)
23
+ end
24
+
25
+ def empty?
26
+ nodes.empty?
27
+ end
28
+
29
+ def roots
30
+ select(&:root?)
31
+ end
32
+
33
+ def add_node(id, attributes={})
34
+ clear_cache
35
+ nodes[id] = Node.new self, id, attributes
36
+ end
37
+
38
+ def remove_node(id)
39
+ validate! id
40
+
41
+ parent_to_children[id].each { |child_id| child_to_parents[child_id].delete id }
42
+ child_to_parents[id].each { |parent_id| parent_to_children[parent_id].delete id }
43
+
44
+ parent_to_children.delete id
45
+ child_to_parents.delete id
46
+
47
+ nodes.delete id
48
+ clear_cache
49
+
50
+ nil
51
+ end
52
+
53
+ def add_relation(parent_id:, child_id:)
54
+ validate! parent_id, child_id
55
+
56
+ clear_cache
57
+ parent_to_children[parent_id] << child_id
58
+ child_to_parents[child_id] << parent_id
59
+
60
+ nil
61
+ end
62
+
63
+ def remove_relation(parent_id:, child_id:)
64
+ validate! parent_id, child_id
65
+
66
+ clear_cache
67
+ parent_to_children[parent_id].delete child_id
68
+ child_to_parents[child_id].delete parent_id
69
+
70
+ nil
71
+ end
72
+
73
+ def parents_of(id)
74
+ child_to_parents[id].map { |node_id| nodes[node_id] }
75
+ end
76
+
77
+ def children_of(id)
78
+ parent_to_children[id].map { |node_id| nodes[node_id] }
79
+ end
80
+
81
+ def ancestors_of(id)
82
+ ancestors_cache[id] ||= parents_of(id).flat_map do |parent|
83
+ ancestors_of(parent.id) + [parent]
84
+ end.uniq(&:id)
85
+ end
86
+
87
+ def descendants_of(id)
88
+ children_of(id).flat_map do |child|
89
+ [child] + descendants_of(child.id)
90
+ end.uniq(&:id)
91
+ end
92
+
93
+ def to_s
94
+ "<#{self.class.name} nodes:[#{map(&:to_s).join(', ')}]>"
95
+ end
96
+ alias_method :inspect, :to_s
97
+
98
+ private
99
+
100
+ attr_reader :nodes, :parent_to_children, :child_to_parents, :ancestors_cache, :descendants_cache
101
+
102
+ def validate!(*ids)
103
+ invalid_ids = ids.reject { |id| nodes.key? id }
104
+ raise "Invalid nodes: #{invalid_ids.join(', ')}" if invalid_ids.any?
105
+ end
106
+
107
+ def clear_cache
108
+ descendants_cache.clear
109
+ ancestors_cache.clear
110
+ end
111
+
112
+ def tsort_each_child(node, &block)
113
+ children_of(node.id).each(&block)
114
+ end
115
+
116
+ def tsort_each_node(&block)
117
+ each(&block)
118
+ end
119
+
120
+ end
@@ -0,0 +1,50 @@
1
+ class HierarchicalGraph
2
+ class Node
3
+
4
+ attr_reader :id, :data
5
+
6
+ def initialize(graph, id, data={})
7
+ @graph = graph
8
+ @id = id
9
+ @data = data
10
+ end
11
+
12
+ def [](key)
13
+ data[key]
14
+ end
15
+
16
+ def []=(key, value)
17
+ data[key] = value
18
+ end
19
+
20
+ def parents
21
+ graph.parents_of id
22
+ end
23
+
24
+ def children
25
+ graph.children_of id
26
+ end
27
+
28
+ def ancestors
29
+ graph.ancestors_of id
30
+ end
31
+
32
+ def descendants
33
+ graph.descendants_of id
34
+ end
35
+
36
+ def root?
37
+ parents.empty?
38
+ end
39
+
40
+ def to_s
41
+ "<#{self.class.name} #{id} parents:[#{parents.map(&:id).join(', ')}] children:[#{children.map(&:id).join(', ')}]>"
42
+ end
43
+ alias_method :inspect, :to_s
44
+
45
+ private
46
+
47
+ attr_reader :graph
48
+
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ class HierarchicalGraph
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
5
+ SimpleCov.start
@@ -0,0 +1,296 @@
1
+ require 'minitest_helper'
2
+
3
+ describe HierarchicalGraph do
4
+
5
+ it 'Empty graph' do
6
+ graph = HierarchicalGraph.new
7
+
8
+ graph.must_be_empty
9
+ end
10
+
11
+ it 'Add node' do
12
+ graph = HierarchicalGraph.new
13
+
14
+ node = graph.add_node 1
15
+
16
+ graph[1].must_equal node
17
+ graph.count.must_equal 1
18
+ end
19
+
20
+ it 'Remove node' do
21
+ graph = HierarchicalGraph.new
22
+ graph.add_node 1
23
+
24
+ graph.remove_node 1
25
+
26
+ graph[1].must_be_nil
27
+ graph.must_be_empty
28
+ end
29
+
30
+ it 'Remove invalid node' do
31
+ graph = HierarchicalGraph.new
32
+
33
+ error = proc { graph.remove_node 1 }.must_raise RuntimeError
34
+ error.message.must_equal "Invalid nodes: 1"
35
+ end
36
+
37
+ it 'Add relation' do
38
+ graph = HierarchicalGraph.new
39
+ graph.add_node 1
40
+ graph.add_node 2
41
+
42
+ graph.add_relation parent_id: 1, child_id: 2
43
+
44
+ graph.parents_of(1).must_be_empty
45
+ graph.children_of(1).map(&:id).must_equal [2]
46
+
47
+ graph.parents_of(2).map(&:id).must_equal [1]
48
+ graph.children_of(2).must_be_empty
49
+ end
50
+
51
+ it 'Add relation with invalid nodes' do
52
+ graph = HierarchicalGraph.new
53
+
54
+ error = proc { graph.add_relation parent_id: 1, child_id: 2 }.must_raise RuntimeError
55
+ error.message.must_equal "Invalid nodes: 1, 2"
56
+ end
57
+
58
+ it 'Remove relation' do
59
+ graph = HierarchicalGraph.new
60
+ graph.add_node 1
61
+ graph.add_node 2
62
+
63
+ graph.add_relation parent_id: 1, child_id: 2
64
+ graph.children_of(1).count.must_equal 1
65
+
66
+ graph.remove_relation parent_id: 1, child_id: 2
67
+ graph.children_of(1).must_be_empty
68
+ graph.count 2
69
+ end
70
+
71
+ it 'Remove relation in complex graph' do
72
+ graph = HierarchicalGraph.new
73
+
74
+ graph.add_node 0
75
+ graph.add_node 1
76
+ graph.add_node 2
77
+ graph.add_node 3
78
+ graph.add_node 4
79
+ graph.add_node 5
80
+ graph.add_node 6
81
+ graph.add_node 7
82
+
83
+ graph.add_relation parent_id: 0, child_id: 3
84
+ graph.add_relation parent_id: 1, child_id: 3
85
+ graph.add_relation parent_id: 1, child_id: 2
86
+ graph.add_relation parent_id: 3, child_id: 4
87
+ graph.add_relation parent_id: 3, child_id: 5
88
+ graph.add_relation parent_id: 2, child_id: 4
89
+ graph.add_relation parent_id: 4, child_id: 5
90
+ graph.add_relation parent_id: 4, child_id: 6
91
+ graph.add_relation parent_id: 6, child_id: 7
92
+
93
+ graph.remove_node 3
94
+
95
+ graph.ancestors_of(4).map(&:id).must_equal [1, 2]
96
+ graph.parents_of(4).map(&:id).must_equal [2]
97
+ graph.children_of(4).map(&:id).must_equal [5, 6]
98
+ graph.descendants_of(4).map(&:id).must_equal [5, 6, 7]
99
+ end
100
+
101
+ it 'Remove relation of invalid nodes' do
102
+ graph = HierarchicalGraph.new
103
+
104
+ error = proc { graph.remove_relation parent_id: 1, child_id: 2 }.must_raise RuntimeError
105
+ error.message.must_equal "Invalid nodes: 1, 2"
106
+ end
107
+
108
+ it 'Roots' do
109
+ graph = HierarchicalGraph.new
110
+ graph.add_node 1
111
+ graph.add_node 2
112
+ graph.add_node 3
113
+ graph.add_node 4
114
+
115
+ graph.roots.map(&:id).must_equal [1, 2, 3, 4]
116
+
117
+ graph.add_relation parent_id: 1, child_id: 3
118
+ graph.add_relation parent_id: 2, child_id: 4
119
+
120
+ graph.roots.map(&:id).must_equal [1, 2]
121
+ end
122
+
123
+ it 'Children' do
124
+ graph = HierarchicalGraph.new
125
+ graph.add_node 1
126
+ graph.add_node 2
127
+ graph.add_node 3
128
+ graph.add_node 4
129
+ graph.add_relation parent_id: 1, child_id: 2
130
+ graph.add_relation parent_id: 1, child_id: 3
131
+ graph.add_relation parent_id: 3, child_id: 4
132
+
133
+ graph.children_of(1).map(&:id).must_equal [2, 3]
134
+ graph.children_of(3).map(&:id).must_equal [4]
135
+ end
136
+
137
+ it 'Parents' do
138
+ graph = HierarchicalGraph.new
139
+ graph.add_node 1
140
+ graph.add_node 2
141
+ graph.add_node 3
142
+ graph.add_node 4
143
+ graph.add_relation parent_id: 1, child_id: 2
144
+ graph.add_relation parent_id: 1, child_id: 3
145
+ graph.add_relation parent_id: 3, child_id: 4
146
+
147
+ graph.parents_of(2).map(&:id).must_equal [1]
148
+ graph.parents_of(4).map(&:id).must_equal [3]
149
+ end
150
+
151
+ it 'Ancestors' do
152
+ graph = HierarchicalGraph.new
153
+ graph.add_node 1
154
+ graph.add_node 2
155
+ graph.add_node 3
156
+ graph.add_node 4
157
+ graph.add_relation parent_id: 1, child_id: 2
158
+ graph.add_relation parent_id: 1, child_id: 3
159
+ graph.add_relation parent_id: 3, child_id: 4
160
+
161
+ graph.ancestors_of(4).map(&:id).must_equal [1, 3]
162
+ end
163
+
164
+ it 'Clear ancestors Cache' do
165
+ graph = HierarchicalGraph.new
166
+ graph.add_node 0
167
+ graph.add_node 1
168
+ graph.add_node 2
169
+ graph.add_node 3
170
+ graph.add_node 4
171
+ graph.add_relation parent_id: 1, child_id: 2
172
+ graph.add_relation parent_id: 1, child_id: 3
173
+ graph.add_relation parent_id: 3, child_id: 4
174
+
175
+ graph.ancestors_of(4).map(&:id).must_equal [1, 3]
176
+
177
+ graph.add_relation parent_id: 0, child_id: 1
178
+
179
+ graph.ancestors_of(4).map(&:id).must_equal [0, 1, 3]
180
+ end
181
+
182
+ it 'Descendants' do
183
+ graph = HierarchicalGraph.new
184
+ graph.add_node 1
185
+ graph.add_node 2
186
+ graph.add_node 3
187
+ graph.add_node 4
188
+ graph.add_relation parent_id: 1, child_id: 2
189
+ graph.add_relation parent_id: 1, child_id: 3
190
+ graph.add_relation parent_id: 3, child_id: 4
191
+
192
+ graph.descendants_of(1).map(&:id).must_equal [2, 3, 4]
193
+ end
194
+
195
+ it 'Clear descendants Cache' do
196
+ graph = HierarchicalGraph.new
197
+ graph.add_node 1
198
+ graph.add_node 2
199
+ graph.add_node 3
200
+ graph.add_node 4
201
+ graph.add_node 5
202
+ graph.add_relation parent_id: 1, child_id: 2
203
+ graph.add_relation parent_id: 1, child_id: 3
204
+ graph.add_relation parent_id: 3, child_id: 4
205
+
206
+ graph.descendants_of(1).map(&:id).must_equal [2, 3, 4]
207
+
208
+ graph.add_relation parent_id: 4, child_id: 5
209
+
210
+ graph.descendants_of(1).map(&:id).must_equal [2, 3, 4, 5]
211
+ end
212
+
213
+ it 'Topological sort' do
214
+ graph = HierarchicalGraph.new
215
+ graph.add_node 1
216
+ graph.add_node 2
217
+ graph.add_node 3
218
+ graph.add_node 4
219
+ graph.add_relation parent_id: 1, child_id: 2
220
+ graph.add_relation parent_id: 1, child_id: 3
221
+ graph.add_relation parent_id: 3, child_id: 4
222
+
223
+ graph.tsort.map(&:id).must_equal [2, 4, 3, 1]
224
+ end
225
+
226
+ it 'To string' do
227
+ graph = HierarchicalGraph.new
228
+ graph.add_node 1
229
+ graph.add_node 2
230
+ graph.add_relation parent_id: 1, child_id: 2
231
+
232
+ graph.to_s.must_equal '<HierarchicalGraph nodes:[<HierarchicalGraph::Node 1 parents:[] children:[2]>, <HierarchicalGraph::Node 2 parents:[1] children:[]>]>'
233
+ end
234
+
235
+ describe 'Node' do
236
+
237
+ let :graph do
238
+ HierarchicalGraph.new.tap do |graph|
239
+ graph.add_node 1, name: 'Node 1', code: 'node_1'
240
+ graph.add_node 2
241
+ graph.add_node 3
242
+ graph.add_node 4
243
+ graph.add_node 5
244
+ graph.add_node 6
245
+ graph.add_relation parent_id: 1, child_id: 2
246
+ graph.add_relation parent_id: 1, child_id: 3
247
+ graph.add_relation parent_id: 3, child_id: 4
248
+ graph.add_relation parent_id: 4, child_id: 5
249
+ graph.add_relation parent_id: 6, child_id: 4
250
+ end
251
+ end
252
+
253
+ it 'Root' do
254
+ graph[1].must_be :root?
255
+ graph[2].wont_be :root?
256
+ end
257
+
258
+ it 'Parents' do
259
+ graph[1].parents.must_be_empty
260
+ graph[4].parents.map(&:id).must_equal [3, 6]
261
+ end
262
+
263
+ it 'Children' do
264
+ graph[5].children.must_be_empty
265
+ graph[1].children.map(&:id).must_equal [2, 3]
266
+ end
267
+
268
+ it 'Ancestors' do
269
+ graph[6].ancestors.must_be_empty
270
+ graph[4].ancestors.map(&:id).must_equal [1, 3, 6]
271
+ end
272
+
273
+ it 'Descendants' do
274
+ graph[5].descendants.must_be_empty
275
+ graph[1].descendants.map(&:id).must_equal [2, 3, 4, 5]
276
+ end
277
+
278
+ it 'Get data' do
279
+ graph[1].data.must_equal name: 'Node 1', code: 'node_1'
280
+ graph[1][:name].must_equal 'Node 1'
281
+ graph[1][:code].must_equal 'node_1'
282
+ end
283
+
284
+ it 'Set data' do
285
+ node = graph[2]
286
+ node.data[:name] = 'Node 2'
287
+ node[:code] = 'node_2'
288
+
289
+ graph[2].data.must_equal name: 'Node 2', code: 'node_2'
290
+ graph[2][:name].must_equal 'Node 2'
291
+ graph[2][:code].must_equal 'node_2'
292
+ end
293
+
294
+ end
295
+
296
+ end
@@ -0,0 +1,5 @@
1
+ require 'coverage_helper'
2
+ require 'hierarchical_graph'
3
+ require 'minitest/autorun'
4
+ require 'minitest/colorin'
5
+ require 'pry-nav'
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hierarchical_graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.11'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '5.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.11'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest-colorin
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.1'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.1'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-line
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.6'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.6'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.12'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.12'
89
+ - !ruby/object:Gem::Dependency
90
+ name: coveralls
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.8'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.8'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry-nav
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.2'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.2'
117
+ description: Hierarchical graph representation
118
+ email:
119
+ - gabynaiman@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".coveralls.yml"
125
+ - ".gitignore"
126
+ - ".ruby-gemset"
127
+ - ".ruby-version"
128
+ - ".travis.yml"
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - hierarchical_graph.gemspec
134
+ - lib/hierarchical_graph.rb
135
+ - lib/hierarchical_graph/node.rb
136
+ - lib/hierarchical_graph/version.rb
137
+ - spec/coverage_helper.rb
138
+ - spec/hierarchical_graph_spec.rb
139
+ - spec/minitest_helper.rb
140
+ homepage: https://github.com/gabynaiman/hierarchical_graph
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubygems_version: 3.1.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Hierarchical graph representation
163
+ test_files:
164
+ - spec/coverage_helper.rb
165
+ - spec/hierarchical_graph_spec.rb
166
+ - spec/minitest_helper.rb