has_hierarchy 0.4.1 → 0.4.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/has_hierarchy.rb +15 -5
- data/lib/has_hierarchy/version.rb +1 -1
- data/spec/tree.rb +33 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7fa480c2dfa727db155946f7a32a374012d502a3
|
4
|
+
data.tar.gz: c10d67ed70b1dd55e2dff7b01e4037bbdf2d74f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77c9d5e8305249b2e249499d3898b18cfcd797a4d6bd0e71fce811e5e9ccbf27c3ba66e03c2f6ce0e7dabf36b119e4460a4fd9911c4223064008eae3cb027c0f
|
7
|
+
data.tar.gz: 473491eca491d329ce3cd379377c0c10bc01d34b50fc0f0da78b83ed344c196828c9fc9e243f75939a20aec6b0185ee5b4adfccae4b8e25b559ac73c0bcdcdbe
|
data/lib/has_hierarchy.rb
CHANGED
@@ -49,15 +49,25 @@ module HasHierarchy
|
|
49
49
|
|
50
50
|
def tree
|
51
51
|
nodes = all
|
52
|
+
tree_hash = {}
|
53
|
+
|
52
54
|
index = {}
|
53
|
-
|
55
|
+
nodes.each{ |n| index[n.id] = {} }
|
56
|
+
nodes.each{ |n| (index[n.parent_id] || tree_hash)[n] = index[n.id] }
|
57
|
+
|
58
|
+
tree_hash
|
59
|
+
end
|
60
|
+
|
61
|
+
def flat_tree(tree_hash = nil)
|
62
|
+
tree_hash ||= tree
|
63
|
+
list = []
|
54
64
|
|
55
|
-
|
56
|
-
|
57
|
-
|
65
|
+
tree_hash.each do |node, children|
|
66
|
+
list << node
|
67
|
+
list += flat_tree(children) unless children.empty?
|
58
68
|
end
|
59
69
|
|
60
|
-
|
70
|
+
list
|
61
71
|
end
|
62
72
|
|
63
73
|
protected
|
data/spec/tree.rb
CHANGED
@@ -18,7 +18,7 @@ shared_examples 'adjacency list' do
|
|
18
18
|
include_context 'example tree'
|
19
19
|
|
20
20
|
describe '.tree' do
|
21
|
-
it 'arranges tree' do
|
21
|
+
it 'arranges tree to hash' do
|
22
22
|
expect(described_class.tree).to be_arranged_like({
|
23
23
|
foo => {},
|
24
24
|
bar => {
|
@@ -43,6 +43,28 @@ shared_examples 'adjacency list' do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
describe '.flat_tree' do
|
47
|
+
it 'arranges tree to array' do
|
48
|
+
expect(described_class.flat_tree).to eq([
|
49
|
+
foo,
|
50
|
+
bar,
|
51
|
+
qux,
|
52
|
+
quux,
|
53
|
+
baz
|
54
|
+
])
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'allows custom order' do
|
58
|
+
expect(described_class.alphabetic.flat_tree).to eq([
|
59
|
+
bar,
|
60
|
+
baz,
|
61
|
+
qux,
|
62
|
+
quux,
|
63
|
+
foo
|
64
|
+
])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
46
68
|
describe '.roots' do
|
47
69
|
it 'returns roots' do
|
48
70
|
expect(described_class.roots).to match_array([ foo, bar ])
|
@@ -156,12 +178,9 @@ shared_examples 'materialized path' do
|
|
156
178
|
|
157
179
|
describe '#subtree' do
|
158
180
|
it 'returns node with descendants' do
|
159
|
-
expect(
|
160
|
-
|
161
|
-
|
162
|
-
quux => {}
|
163
|
-
},
|
164
|
-
baz => {}
|
181
|
+
expect(qux.subtree.tree).to be_arranged_like({
|
182
|
+
qux => {
|
183
|
+
quux => {}
|
165
184
|
}
|
166
185
|
})
|
167
186
|
end
|
@@ -258,8 +277,13 @@ shared_examples 'tree with cached depth' do
|
|
258
277
|
|
259
278
|
it 'stores node level' do
|
260
279
|
expect(described_class.where(depth: 0)).to match_array([ foo, bar ])
|
261
|
-
|
262
|
-
expect(described_class.where(depth:
|
280
|
+
|
281
|
+
expect(described_class.where(depth: 1...3).tree).to be_arranged_like({
|
282
|
+
qux => {
|
283
|
+
quux => {}
|
284
|
+
},
|
285
|
+
baz => {}
|
286
|
+
})
|
263
287
|
end
|
264
288
|
end
|
265
289
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_hierarchy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kolesnikov Danil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|