tree_graph 0.1.1 → 0.2.0
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/README.md +3 -16
- data/lib/tree_graph/version.rb +1 -1
- data/lib/tree_graph.rb +46 -23
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae297d2b5265ef94e73403efb35c9fd1a7348737
|
4
|
+
data.tar.gz: 4fa3803abbc5a6d6509313dc5f2e3ff631c1db89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d89095d952e52da07dbae183425cb73ae3223597a9a67800e24d97a60ea7aec3b68af1233eb0d7aaa2bc1785f0bedcd48c76966c027e019c471ca59b974b2d43
|
7
|
+
data.tar.gz: f868060e3bc62449d4e3bff34631c0a20df41de532678f4e2a93a5830822bb4fb856c032586682b65a2362b92197e3c94be8dd23a28a9d1dccc652d0d8ade22d
|
data/README.md
CHANGED
@@ -20,22 +20,9 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
In Node class, `include TreeGraph`, then implement
|
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.
|
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
|
|
27
|
-
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/ken/tree_graph.
|
36
|
-
|
37
|
-
|
38
|
-
## License
|
39
|
-
|
40
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
27
|
+
Gems depending on tree_graph: [constree](https://github.com/turnon/constree), [trace_tree](https://github.com/turnon/trace_tree).
|
41
28
|
|
data/lib/tree_graph/version.rb
CHANGED
data/lib/tree_graph.rb
CHANGED
@@ -3,34 +3,57 @@ require "tree_graph/version"
|
|
3
3
|
module TreeGraph
|
4
4
|
|
5
5
|
def tree_graph
|
6
|
-
(
|
7
|
-
children_for_tree_graph.map(&:tree_graph)
|
8
|
-
).join("\n")
|
6
|
+
Node.new(self).tree_graph
|
9
7
|
end
|
10
8
|
|
11
|
-
|
12
|
-
tree_graph_indent +
|
13
|
-
tree_graph_branch +
|
14
|
-
label_for_tree_graph
|
15
|
-
end
|
9
|
+
class Node
|
16
10
|
|
17
|
-
|
18
|
-
|
19
|
-
parent_for_tree_graph.tree_graph_ancestors + [parent_for_tree_graph]
|
20
|
-
end
|
11
|
+
attr_accessor :is_last
|
12
|
+
attr_reader :raw_node, :parent
|
21
13
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
14
|
+
def initialize raw_node, parent=nil
|
15
|
+
@raw_node, @parent, @is_last = raw_node, parent, false
|
16
|
+
end
|
26
17
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
18
|
+
def tree_graph
|
19
|
+
([level] +
|
20
|
+
children_nodes.map(&:tree_graph)
|
21
|
+
).join("\n")
|
22
|
+
end
|
23
|
+
|
24
|
+
def children_nodes
|
25
|
+
children = []
|
26
|
+
raw_node.children_for_tree_graph.each do |c|
|
27
|
+
children << Node.new(c, self)
|
33
28
|
end
|
34
|
-
|
29
|
+
return children if children.empty?
|
30
|
+
children.last.is_last = true
|
31
|
+
children
|
32
|
+
end
|
33
|
+
|
34
|
+
def level
|
35
|
+
[indent, branch, raw_node.label_for_tree_graph].join
|
36
|
+
end
|
37
|
+
|
38
|
+
def ancestors
|
39
|
+
return [] unless parent
|
40
|
+
parent.ancestors + [parent]
|
41
|
+
end
|
42
|
+
|
43
|
+
def branch
|
44
|
+
return '' unless parent
|
45
|
+
is_last ? '└─' : '├─'
|
46
|
+
end
|
47
|
+
|
48
|
+
def indent
|
49
|
+
ancestors.map do |a|
|
50
|
+
unless a.parent
|
51
|
+
''
|
52
|
+
else
|
53
|
+
a.is_last ? ' ' : '│ '
|
54
|
+
end
|
55
|
+
end.join
|
56
|
+
end
|
57
|
+
|
35
58
|
end
|
36
59
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2017-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
version: '0'
|
91
91
|
requirements: []
|
92
92
|
rubyforge_project:
|
93
|
-
rubygems_version: 2.
|
93
|
+
rubygems_version: 2.6.8
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
96
|
summary: A mixin to help you generate tree graph
|