tree_graph 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae297d2b5265ef94e73403efb35c9fd1a7348737
4
- data.tar.gz: 4fa3803abbc5a6d6509313dc5f2e3ff631c1db89
3
+ metadata.gz: ab015d3d94eff12b6296b6e1525c1834aaa93993
4
+ data.tar.gz: 2aef11ef5ecc266b6f222b1ac2174d1cf109e1b1
5
5
  SHA512:
6
- metadata.gz: d89095d952e52da07dbae183425cb73ae3223597a9a67800e24d97a60ea7aec3b68af1233eb0d7aaa2bc1785f0bedcd48c76966c027e019c471ca59b974b2d43
7
- data.tar.gz: f868060e3bc62449d4e3bff34631c0a20df41de532678f4e2a93a5830822bb4fb856c032586682b65a2362b92197e3c94be8dd23a28a9d1dccc652d0d8ade22d
6
+ metadata.gz: 7aa9c7f03dc01db31e7bf558183b3127e222367cee62cadd47107ba43deb6285d9e398f61905c34792a331a3e10c5b97c8ac739530b19b9b5e36911be6344628
7
+ data.tar.gz: 94fa4cdc237b6a8ef0f765e158d09c1784c643506f9abb07c4fd2ad7de63da96fdb28e31b957d3e1a47124f46f485e66f65b148cc197c325b594419bf3f230f0
data/README.md CHANGED
@@ -20,7 +20,7 @@ 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` and `tree_graph_bottom_up` on that Node object.
24
24
 
25
25
  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
26
 
data/lib/tree_graph.rb CHANGED
@@ -3,10 +3,14 @@ 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
+ module Node
10
14
 
11
15
  attr_accessor :is_last
12
16
  attr_reader :raw_node, :parent
@@ -15,16 +19,10 @@ module TreeGraph
15
19
  @raw_node, @parent, @is_last = raw_node, parent, false
16
20
  end
17
21
 
18
- def tree_graph
19
- ([level] +
20
- children_nodes.map(&:tree_graph)
21
- ).join("\n")
22
- end
23
-
24
22
  def children_nodes
25
23
  children = []
26
24
  raw_node.children_for_tree_graph.each do |c|
27
- children << Node.new(c, self)
25
+ children << self.class.new(c, self)
28
26
  end
29
27
  return children if children.empty?
30
28
  children.last.is_last = true
@@ -35,16 +33,15 @@ module TreeGraph
35
33
  [indent, branch, raw_node.label_for_tree_graph].join
36
34
  end
37
35
 
36
+ def levels
37
+ [level] + children_nodes.map(&:tree_graph)
38
+ end
39
+
38
40
  def ancestors
39
41
  return [] unless parent
40
42
  parent.ancestors + [parent]
41
43
  end
42
44
 
43
- def branch
44
- return '' unless parent
45
- is_last ? '└─' : '├─'
46
- end
47
-
48
45
  def indent
49
46
  ancestors.map do |a|
50
47
  unless a.parent
@@ -56,4 +53,30 @@ module TreeGraph
56
53
  end
57
54
 
58
55
  end
56
+
57
+ class TopDown
58
+ include Node
59
+
60
+ def tree_graph
61
+ levels.join("\n")
62
+ end
63
+
64
+ def branch
65
+ return '' unless parent
66
+ is_last ? '└─' : '├─'
67
+ end
68
+ end
69
+
70
+ class BottomUp
71
+ include Node
72
+
73
+ def tree_graph
74
+ levels.reverse.join("\n")
75
+ end
76
+
77
+ def branch
78
+ return '' unless parent
79
+ is_last ? '┌─' : '├─'
80
+ end
81
+ end
59
82
  end
@@ -1,3 +1,3 @@
1
1
  module TreeGraph
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  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.1
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: 2017-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler