hierarchy-tree 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hierarchy_tree.rb +52 -22
- 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: 6644c370fbd994c645135ec56cd7aa19c6586cf7da38136e929b1bac2edd0ddf
|
4
|
+
data.tar.gz: 938f28a4313baa9ba0dabfdbfed06dd32022fbf79177b2bbdf537233593d15ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e3f81dad78bfb864c73153cdebfb2ad3978f1caa70ad2996c2125ce7d88fd2ecf4a362e6c2920877f7e23cfe129f51055f0f945e647392c8f66d69ac3b5c67e
|
7
|
+
data.tar.gz: 790d3534a9798da8fea30a5d44252e57f6d5f00f3fbdac7700502a3c2860a2427e9b97594a926c32cab800eaa3b2d86df8107ab5d675fcb48bd3274ed330bf4c
|
data/lib/hierarchy_tree.rb
CHANGED
@@ -2,10 +2,10 @@ require 'active_record'
|
|
2
2
|
require 'active_support/core_ext/object/inclusion.rb'
|
3
3
|
|
4
4
|
################ Debug ################
|
5
|
-
# gem
|
6
|
-
# rm hierarchy-tree-0.3.
|
5
|
+
# gem cleanup hierarchy-tree
|
6
|
+
# rm hierarchy-tree-0.3.1.gem
|
7
7
|
# gem build hierarchy_tree
|
8
|
-
# gem install hierarchy-tree-0.3.
|
8
|
+
# gem install hierarchy-tree-0.3.1.gem
|
9
9
|
# ruby -Itest test/test_hierarchy_tree.rb
|
10
10
|
|
11
11
|
class Hierarchy
|
@@ -26,20 +26,34 @@ class Hierarchy
|
|
26
26
|
@classes_list
|
27
27
|
end
|
28
28
|
|
29
|
-
# Return the ancestors associations by navigating through :belongs_to
|
29
|
+
# Return all the possible ancestors associations by navigating through :belongs_to
|
30
30
|
# Starting from the "from" class towards the "to" class
|
31
|
-
|
32
|
-
|
33
|
-
return if from.to_s == to.to_s and descendants == [] # Base case
|
34
|
-
return 'loop' if from.in? descendants # Avoids cycle
|
31
|
+
def self.ancestors(from:, to:)
|
32
|
+
return [] if from.to_s == to.to_s
|
35
33
|
|
36
|
-
|
34
|
+
queue = [{ class: from.to_s, path: [] }]
|
35
|
+
visited = { from.to_s => [] }
|
36
|
+
paths = []
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
while queue.any?
|
39
|
+
current = queue.shift
|
40
|
+
current_class = current[:class]
|
41
|
+
current_path = current[:path]
|
42
|
+
|
43
|
+
current_class.constantize.reflect_on_all_associations(:belongs_to).each do |relation|
|
44
|
+
next_class = relation.klass.to_s
|
45
|
+
next_path = current_path + [relation.name]
|
46
|
+
|
47
|
+
paths << hashify(next_path) if next_class == to.to_s
|
48
|
+
|
49
|
+
if next_path == next_path.uniq # Non-looped path
|
50
|
+
visited[next_class] = next_path
|
51
|
+
queue.push({ class: next_class, path: next_path })
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
paths
|
43
57
|
end
|
44
58
|
|
45
59
|
# Return the ancestors associations by navigating through :belongs_to
|
@@ -70,6 +84,22 @@ class Hierarchy
|
|
70
84
|
end
|
71
85
|
end
|
72
86
|
|
87
|
+
# Return the ancestors associations by navigating through :belongs_to
|
88
|
+
# Starting from the "from" class towards the "to" class
|
89
|
+
# Using DFS - Depth First Search, thus finding the Deepest Path (more likely)
|
90
|
+
def self.ancestors_dfs(from:, to:, descendants: [])
|
91
|
+
return if from.to_s == to.to_s and descendants == [] # Base case
|
92
|
+
return 'loop' if from.in? descendants # Avoids cycle
|
93
|
+
|
94
|
+
descendants.push(from)
|
95
|
+
|
96
|
+
from.reflect_on_all_associations(:belongs_to).map do |relation|
|
97
|
+
return relation.name if relation.klass.to_s == to.to_s # Path is found
|
98
|
+
path = ancestors_dfs(from: relation.klass, to: to, descendants: descendants)
|
99
|
+
return { relation.name => path } if valid_path?(path, to.model_name.param_key.to_sym)
|
100
|
+
end.compact.first
|
101
|
+
end
|
102
|
+
|
73
103
|
def self.loop?(klass)
|
74
104
|
@cache = {}
|
75
105
|
false if dfs_hierarchy(class: klass, classes?: false)
|
@@ -151,6 +181,14 @@ class Hierarchy
|
|
151
181
|
true
|
152
182
|
end
|
153
183
|
|
184
|
+
def self.hashify(array)
|
185
|
+
if array.length == 1
|
186
|
+
array.first
|
187
|
+
else
|
188
|
+
{ array.first => hashify(array.drop(1)) }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
154
192
|
def self.valid_path?(path, target)
|
155
193
|
return true if path == target
|
156
194
|
|
@@ -163,12 +201,4 @@ class Hierarchy
|
|
163
201
|
false
|
164
202
|
end
|
165
203
|
end
|
166
|
-
|
167
|
-
def self.hashify(array)
|
168
|
-
if array.length == 1
|
169
|
-
array.first
|
170
|
-
else
|
171
|
-
{ array.first => hashify(array.drop(1)) }
|
172
|
-
end
|
173
|
-
end
|
174
204
|
end
|
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.1
|
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-
|
11
|
+
date: 2023-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|