hierarchy-tree 0.3.3 → 0.3.5
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/hierarchy_tree.rb +43 -5
- 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: f05844c843379d26d0aa9bb076c0e0e12212992b39e289a3ec838478f365b273
|
4
|
+
data.tar.gz: be4a10778e5799065e9114422d74099b02d5ecd96fdde176376121a65555a7d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1802c4508846ee55aed45c163cf8ecb77fc1f1f0776094e91dd27794f6c366b97cf258f9351aec71c6e35f7647dcaaf6f1687f9e2c2853dcf753c16c0e036fd0
|
7
|
+
data.tar.gz: 6c7446063bf94384205133d78cc0a1fd9e3b24ced8b93572d552513b8868833f1d89953ae33718ebce1683b15eaddb957fdd8e4055319199b55fac863af6cb26
|
data/lib/hierarchy_tree.rb
CHANGED
@@ -3,9 +3,9 @@ require 'active_support/core_ext/object/inclusion.rb'
|
|
3
3
|
|
4
4
|
################ Debug ################
|
5
5
|
# gem cleanup hierarchy-tree
|
6
|
-
# rm hierarchy-tree-0.3.
|
6
|
+
# rm hierarchy-tree-0.3.5.gem
|
7
7
|
# gem build hierarchy_tree
|
8
|
-
# gem install hierarchy-tree-0.3.
|
8
|
+
# gem install hierarchy-tree-0.3.5.gem
|
9
9
|
# ruby -Itest test/test_hierarchy_tree.rb
|
10
10
|
|
11
11
|
class Hierarchy
|
@@ -26,6 +26,14 @@ class Hierarchy
|
|
26
26
|
@classes_list
|
27
27
|
end
|
28
28
|
|
29
|
+
# Return the array of children classes in a bottom up manner
|
30
|
+
# From leaf classes to upper classes
|
31
|
+
def self.bottom_up_classes(klass)
|
32
|
+
@classes_list = []
|
33
|
+
build_descendants(klass)
|
34
|
+
topological_sort([klass.to_s] + @classes_list)
|
35
|
+
end
|
36
|
+
|
29
37
|
# Return all the possible ancestors associations by navigating through :belongs_to
|
30
38
|
# Starting from the "from" class towards the "to" class
|
31
39
|
def self.ancestors(from:, to:)
|
@@ -41,6 +49,8 @@ class Hierarchy
|
|
41
49
|
current_path = current[:path]
|
42
50
|
|
43
51
|
current_class.constantize.reflect_on_all_associations(:belongs_to).each do |relation|
|
52
|
+
next if relation.options[:polymorphic]
|
53
|
+
|
44
54
|
next_class = relation.klass.to_s
|
45
55
|
next_path = current_path + [relation.name]
|
46
56
|
|
@@ -59,7 +69,7 @@ class Hierarchy
|
|
59
69
|
# Return the ancestors associations by navigating through :belongs_to
|
60
70
|
# Starting from the "from" class towards the "to" class
|
61
71
|
# Using BFS - Breadth First Search, thus finding the Shortest Path
|
62
|
-
def self.ancestors_bfs(from:, to:)
|
72
|
+
def self.ancestors_bfs(from:, to:, classify: false)
|
63
73
|
return if from == to
|
64
74
|
|
65
75
|
queue = [{ class: from, path: [] }]
|
@@ -71,10 +81,17 @@ class Hierarchy
|
|
71
81
|
current_path = current[:path]
|
72
82
|
|
73
83
|
current_class.reflect_on_all_associations(:belongs_to).each do |relation|
|
84
|
+
next if relation.options[:polymorphic]
|
85
|
+
|
74
86
|
next_class = relation.klass
|
75
|
-
next_path = current_path + [relation.name]
|
76
87
|
|
77
|
-
|
88
|
+
if classify # An array of classes
|
89
|
+
next_path = current_path + [relation.klass.to_s]
|
90
|
+
return next_path if next_class.to_s == to.to_s
|
91
|
+
else # A hash of associations
|
92
|
+
next_path = current_path + [relation.name]
|
93
|
+
return hashify(next_path) if next_class.to_s == to.to_s
|
94
|
+
end
|
78
95
|
|
79
96
|
if visited.exclude?(next_class)
|
80
97
|
visited << next_class
|
@@ -181,6 +198,27 @@ class Hierarchy
|
|
181
198
|
true
|
182
199
|
end
|
183
200
|
|
201
|
+
def self.topological_sort(classes)
|
202
|
+
dependencies = classes.to_h do |c|
|
203
|
+
[c, Hierarchy.ancestors_bfs(from: c.constantize, to: classes[0].constantize, classify: true)]
|
204
|
+
end
|
205
|
+
|
206
|
+
sorted_items = []
|
207
|
+
visited = {}
|
208
|
+
|
209
|
+
visit = lambda do |item|
|
210
|
+
unless visited[item]
|
211
|
+
visited[item] = true
|
212
|
+
dependencies[item]&.each { |dependency| visit.call(dependency) }
|
213
|
+
sorted_items.unshift(item)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
classes.each { |item| visit.call(item) unless visited[item] }
|
218
|
+
|
219
|
+
sorted_items
|
220
|
+
end
|
221
|
+
|
184
222
|
def self.hashify(array)
|
185
223
|
if array.length == 1
|
186
224
|
array.first
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hierarchy-tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Cordeiro Costa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|