hierarchy-tree 0.3.4 → 0.3.5

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/hierarchy_tree.rb +39 -5
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddb0de6ef3ff8b267337c4b212fb79cc61882daeabdeed87f24ed8c6e2cf57a2
4
- data.tar.gz: 2f9edaf7700263b9cb0258c3b78254d2860d5483379898ddab7a05d9eefa0d09
3
+ metadata.gz: f05844c843379d26d0aa9bb076c0e0e12212992b39e289a3ec838478f365b273
4
+ data.tar.gz: be4a10778e5799065e9114422d74099b02d5ecd96fdde176376121a65555a7d9
5
5
  SHA512:
6
- metadata.gz: a737550c58c67149a23afd9252bc5f357953c715b9a76274da6c6c6b599bdd5fa3a22bda025cbec8913ba48c7350d44a2fa384222533b7816fb8f385469fbd40
7
- data.tar.gz: e2b5a61beed7165bb58e36edc90c86e27e0214fe85a27af2a2ea99cb3825c0c4916c8700b8b333d7fabf1431f0ba9eff4621f22c8afa3ec4fef4e6b4d6f9aeac
6
+ metadata.gz: 1802c4508846ee55aed45c163cf8ecb77fc1f1f0776094e91dd27794f6c366b97cf258f9351aec71c6e35f7647dcaaf6f1687f9e2c2853dcf753c16c0e036fd0
7
+ data.tar.gz: 6c7446063bf94384205133d78cc0a1fd9e3b24ced8b93572d552513b8868833f1d89953ae33718ebce1683b15eaddb957fdd8e4055319199b55fac863af6cb26
@@ -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-X.Y.Z.gem
6
+ # rm hierarchy-tree-0.3.5.gem
7
7
  # gem build hierarchy_tree
8
- # gem install hierarchy-tree-X.Y.Z.gem
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:)
@@ -61,7 +69,7 @@ class Hierarchy
61
69
  # Return the ancestors associations by navigating through :belongs_to
62
70
  # Starting from the "from" class towards the "to" class
63
71
  # Using BFS - Breadth First Search, thus finding the Shortest Path
64
- def self.ancestors_bfs(from:, to:)
72
+ def self.ancestors_bfs(from:, to:, classify: false)
65
73
  return if from == to
66
74
 
67
75
  queue = [{ class: from, path: [] }]
@@ -76,9 +84,14 @@ class Hierarchy
76
84
  next if relation.options[:polymorphic]
77
85
 
78
86
  next_class = relation.klass
79
- next_path = current_path + [relation.name]
80
87
 
81
- return hashify(next_path) if next_class.to_s == to.to_s
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
82
95
 
83
96
  if visited.exclude?(next_class)
84
97
  visited << next_class
@@ -185,6 +198,27 @@ class Hierarchy
185
198
  true
186
199
  end
187
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
+
188
222
  def self.hashify(array)
189
223
  if array.length == 1
190
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
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-10-26 00:00:00.000000000 Z
11
+ date: 2023-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest