tree_graph 0.2.0 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ae297d2b5265ef94e73403efb35c9fd1a7348737
4
- data.tar.gz: 4fa3803abbc5a6d6509313dc5f2e3ff631c1db89
2
+ SHA256:
3
+ metadata.gz: b955eb6cbbadd1046da5a1b57d0e309e1409a7383a0f1eb139a4d9a8796c2a1c
4
+ data.tar.gz: e597fd6a7e69411b2da4a59314fca704f3c19ecffe35b58c51b4c73c1db19a9e
5
5
  SHA512:
6
- metadata.gz: d89095d952e52da07dbae183425cb73ae3223597a9a67800e24d97a60ea7aec3b68af1233eb0d7aaa2bc1785f0bedcd48c76966c027e019c471ca59b974b2d43
7
- data.tar.gz: f868060e3bc62449d4e3bff34631c0a20df41de532678f4e2a93a5830822bb4fb856c032586682b65a2362b92197e3c94be8dd23a28a9d1dccc652d0d8ade22d
6
+ metadata.gz: ea5e1e665570c6cb644024ea8b763733c601d844c3afc42ce514ff464cf4222716cfb2e7b065b241885e0b308dfedcb1a29e447be5b057a151dbe18c6e5e2ba5
7
+ data.tar.gz: 3ce2e2b287dc19265f2fcc14358fc0dd5925864a6f0141148a471a435e2641457c39fc4457b8dbc56b9055e11454fa4346b2fdb86495db98a119a0674866a627
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  *.swp
11
11
  *.gem
12
+ .tool-versions
data/README.md CHANGED
@@ -20,7 +20,9 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- In Node class, `include TreeGraph`, then implement two methods, `label_for_tree_graph` and `children_for_tree_graph`(which return thing responds to `each`). Then you can call `tree_graph` on that Node object.
23
+ In Node class, `include TreeGraph`, then implement two methods, `label_for_tree_graph` and `children_for_tree_graph`(which return thing responds to `each`). Then you can call `tree_graph`, `tree_graph_bottom_up` and `tree_graph_bottom_up_in_same_order` on that Node object.
24
+
25
+ By default, `::Object#label_for_tree_graph` call `to_s`, and `::Object#children_for_tree_graph` return empty array.
24
26
 
25
27
  Or checkout [test/tree_graph_test.rb](https://github.com/turnon/tree_graph/blob/master/test/tree_graph_test.rb) to see how to use.
26
28
 
@@ -1,3 +1,3 @@
1
1
  module TreeGraph
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.3"
3
3
  end
data/lib/tree_graph.rb CHANGED
@@ -3,10 +3,30 @@ require "tree_graph/version"
3
3
  module TreeGraph
4
4
 
5
5
  def tree_graph
6
- Node.new(self).tree_graph
6
+ TopDown.new(self).tree_graph
7
7
  end
8
8
 
9
- class Node
9
+ def tree_graph_bottom_up
10
+ BottomUp.new(self).tree_graph
11
+ end
12
+
13
+ def tree_graph_bottom_up_in_same_order
14
+ BottomUpInSameOrder.new(self).tree_graph
15
+ end
16
+
17
+ EMPTY_ARRAY = []
18
+
19
+ class ::Object
20
+ def label_for_tree_graph
21
+ to_s
22
+ end
23
+
24
+ def children_for_tree_graph
25
+ EMPTY_ARRAY
26
+ end
27
+ end
28
+
29
+ module Node
10
30
 
11
31
  attr_accessor :is_last
12
32
  attr_reader :raw_node, :parent
@@ -15,34 +35,29 @@ module TreeGraph
15
35
  @raw_node, @parent, @is_last = raw_node, parent, false
16
36
  end
17
37
 
18
- def tree_graph
19
- ([level] +
20
- children_nodes.map(&:tree_graph)
21
- ).join("\n")
22
- end
23
-
24
38
  def children_nodes
25
- children = []
26
- raw_node.children_for_tree_graph.each do |c|
27
- children << Node.new(c, self)
39
+ children.map do |c|
40
+ self.class.new(c, self)
41
+ end.tap do |nodes|
42
+ nodes.last.is_last = true unless nodes.empty?
28
43
  end
29
- return children if children.empty?
30
- children.last.is_last = true
31
- children
44
+ end
45
+
46
+ def children
47
+ raw_node.children_for_tree_graph
32
48
  end
33
49
 
34
50
  def level
35
51
  [indent, branch, raw_node.label_for_tree_graph].join
36
52
  end
37
53
 
38
- def ancestors
39
- return [] unless parent
40
- parent.ancestors + [parent]
54
+ def levels
55
+ [level] + children_nodes.map(&:tree_graph)
41
56
  end
42
57
 
43
- def branch
44
- return '' unless parent
45
- is_last ? '└─' : '├─'
58
+ def ancestors
59
+ return EMPTY_ARRAY unless parent
60
+ parent.ancestors + [parent]
46
61
  end
47
62
 
48
63
  def indent
@@ -56,4 +71,36 @@ module TreeGraph
56
71
  end
57
72
 
58
73
  end
74
+
75
+ class TopDown
76
+ include Node
77
+
78
+ def tree_graph
79
+ levels.join("\n")
80
+ end
81
+
82
+ def branch
83
+ return '' unless parent
84
+ is_last ? '└─' : '├─'
85
+ end
86
+ end
87
+
88
+ class BottomUp
89
+ include Node
90
+
91
+ def tree_graph
92
+ levels.reverse.join("\n")
93
+ end
94
+
95
+ def branch
96
+ return '' unless parent
97
+ is_last ? '┌─' : '├─'
98
+ end
99
+ end
100
+
101
+ class BottomUpInSameOrder < BottomUp
102
+ def children
103
+ super.reverse
104
+ end
105
+ end
59
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ken
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-20 00:00:00.000000000 Z
11
+ date: 2022-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,8 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.6.8
92
+ rubygems_version: 3.1.6
94
93
  signing_key:
95
94
  specification_version: 4
96
95
  summary: A mixin to help you generate tree graph