tree_graph 0.1.1 → 0.2.0

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: 457d1b759ef73e437da805f9fea94aef374bf8b0
4
- data.tar.gz: e38b29a657054c54d9026bff943b93c2b0fb079a
3
+ metadata.gz: ae297d2b5265ef94e73403efb35c9fd1a7348737
4
+ data.tar.gz: 4fa3803abbc5a6d6509313dc5f2e3ff631c1db89
5
5
  SHA512:
6
- metadata.gz: 5595b438ce21764d26bb2ea65471c1beb87a6f277403959231535d307d4c92a93d6e465c5458b68273b3e92f04da6990f60f0304ce8bd73d6ad5ba0992aa897f
7
- data.tar.gz: 600015270d0f10ba8016daf6d22f48d250c1fbe4e0498379b28c719dbaee350894d08309f98908bdae1ed5f409df31902d2c07402d4f744c570994a8939b80bf
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 `parent_for_tree_graph`, `label_for_tree_graph`, `is_last_for_tree_graph`, you get `tree_graph_level`. To have `tree_graph`, also implement `children_for_tree_graph`.
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
- Gems depending on this: [constree](https://github.com/turnon/constree), [trace_tree](https://github.com/turnon/trace_tree).
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
- ## Development
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
 
@@ -1,3 +1,3 @@
1
1
  module TreeGraph
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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
- ([tree_graph_level] +
7
- children_for_tree_graph.map(&:tree_graph)
8
- ).join("\n")
6
+ Node.new(self).tree_graph
9
7
  end
10
8
 
11
- def tree_graph_level
12
- tree_graph_indent +
13
- tree_graph_branch +
14
- label_for_tree_graph
15
- end
9
+ class Node
16
10
 
17
- def tree_graph_ancestors
18
- return [] unless parent_for_tree_graph
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
- def tree_graph_branch
23
- return '' unless parent_for_tree_graph
24
- is_last_for_tree_graph ? '└─' : '├─'
25
- end
14
+ def initialize raw_node, parent=nil
15
+ @raw_node, @parent, @is_last = raw_node, parent, false
16
+ end
26
17
 
27
- def tree_graph_indent
28
- tree_graph_ancestors.map do |a|
29
- unless a.parent_for_tree_graph
30
- ''
31
- else
32
- a.is_last_for_tree_graph ? ' ' : '│ '
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
- end.join
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.1.1
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-04 00:00:00.000000000 Z
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.5.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