tree_outline 0.1.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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDBhZGU3NDMwMWY1NTM0YzA1OWI2NjhmNTM3ZDM0YzRhMjFkY2Y4ZA==
5
+ data.tar.gz: !binary |-
6
+ MjI4YjBmYTAyZmM5NjBjNGFjYzhiYzM0ZWVlZjhiNTMyNWRhODJmZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmZlMjhhMzA5MzllMDUzN2I4NDQyNTljNjI2YmYzYTBkYzBhMjdlNmU3ODU2
10
+ ZTIyYjBhNTRiNjc5MDFhODM5M2VjMDczYzVkZjMxOTljMGUzNjViMzIxZjk4
11
+ OWVlNTYzZDEzYTY1NDMzMGUxMmZhY2RkYzEyOTAzMTlmNGYwMDI=
12
+ data.tar.gz: !binary |-
13
+ OGViODc4ZTRlY2ZkZGVjZjU3YzE1N2RjYmQ0MDNkZmJmYmIyMmM5MDIwMGEw
14
+ OWVlYmYyZDU1MzUxM2Y3MmEzMTAwN2ViZGI5NzFlZjZmZWQwYTlkYWYzOTcw
15
+ OTc1MGYzMDM0ODRiODRmM2Y5NTFkYWVlNGRjYjdmOTAzZTM1MmU=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ addons:
5
+ code_climate:
6
+ repo_token: 58e8a92216235bcf7808e03c37610985a6524d25ce7ac20aeedd2f8a5c32c797
7
+ deploy:
8
+ provider: rubygems
9
+ api_key:
10
+ secure: ypr3m3B8Tr4IQNa7hqydWdB0Iu1lgjAxU8rtpze9yd0u6+q48GF0jdlwnIQ/po2pQDOc/O7uourXgTjp8g8zkZCgdDsvMzV1pAiRt5TlXeSAkBWL81033Sjo9CkMzeGxspekCx8JrjxjH7EhtUqe/OiX8PfYS1lUYCCQTryahcpTUdYJ0BK14X0Z9j4Z/qmvRe35oeoMVZ0K06O1GCdGfZmeKp9IoKxkZKj5df6oU3K5EQkauNVgar8MYzxvxfoJtSBnPs4LO1ruYaGdMaHfTI7xpPpzUlLCZdeEBjmnNmLG8tmSurl8NKQJuSY7hQCWtNMlGG/JZaMmdAgwJKZkK9wzh7ZlR/ItsVBFzNapMRTLPTNghwtNW29ceE1CPNdJJ93xZC02jf8GCux1+OOVhBtlLlgeDYVNO0i0p++11Q6gtgg6ToYm5xJnpq3+4Pib/s7Hidpka5ej0XOA+OxMxH+Llu++JDNOP41Hg38bgZ/st7G23G4R7rXC74ACsTa8yMPiJ6Cm0BN/vzuYoaiJxUFYPhZDdS6Yi0r+w1HpL2eeBH9gmRfpX6zMOo6+k0Uf4luGFqKoQpdH5+HHH5Vl7e0HjX75EPMhcWXFgoxinLUnp6DSC71MwADc2WQILytsMjFFok4bEnpxlxtoT73V00aMoDFKLnFNC4Ov5+0MHrs=
11
+ gem: tree_outline
12
+ on:
13
+ tags: true
14
+ repo: flower-pot/tree_outline
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tree_outline.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Frederic Branczyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ [![Build Status](https://travis-ci.org/flower-pot/tree_outline.svg?branch=master)](https://travis-ci.org/flower-pot/tree_outline)
2
+ [![Test Coverage](https://codeclimate.com/github/flower-pot/tree_outline/badges/coverage.svg)](https://codeclimate.com/github/flower-pot/tree_outline/coverage)
3
+ [![Code Climate](https://codeclimate.com/github/flower-pot/tree_outline/badges/gpa.svg)](https://codeclimate.com/github/flower-pot/tree_outline)
4
+
5
+ # TreeOutline
6
+
7
+ Use TreeOutline to stringify trees. It uses characters from the extended ASCII
8
+ character encoding to make them the most beautiful they can be.
9
+
10
+ The style the tree is formatted in is similar to the way the unix tool `tree`
11
+ outputs the the tree of a directory.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'tree_outline'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install tree_outline
28
+
29
+ ## Usage
30
+
31
+ All you need is a recursive datastructure such as the below `Node` class. It
32
+ contains the attribute `children`, which is an enumeration of `Node`s as well.
33
+ The `to_s` method is called to stringify a node itself.
34
+
35
+ ```ruby
36
+ require 'tree_outline'
37
+
38
+ class Node
39
+ attr_reader :name, :children
40
+
41
+ def initialize(name, children = [])
42
+ @name = name
43
+ @children = children
44
+ end
45
+
46
+ alias_method :to_s, :name
47
+ end
48
+
49
+ my_tree = Node.new('root', [
50
+ Node.new('first'),
51
+ Node.new('second'),
52
+ Node.new('third')
53
+ ])
54
+
55
+ puts TreeOutline.new(my_tree).to_s
56
+ # root
57
+ # ├── first
58
+ # ├── second
59
+ # └── third
60
+ ```
61
+
62
+ ## Side-Note
63
+
64
+ When implementing we noticed that this also makes a interesting, good (and
65
+ hard) kata. Try implementing it and see for yourself.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
70
+ `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`. To
73
+ release a new version, update the version number in `version.rb`, git tag for
74
+ the version, push git commits and tags. Travis CI then runs the tests and when
75
+ passed automatically pushes the new gem to
76
+ [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/[my-github-username]/tree_outline/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ # remove release task
3
+ Rake::Task["release"].clear
4
+
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task default: :spec
9
+ rescue LoadError
10
+ puts 'rspec tasks could not be loaded'
11
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tree_outline"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class TreeOutline
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ require 'tree_outline/version'
2
+
3
+ class TreeOutline
4
+ DEFAULT = '├── '
5
+ LINE = '│ '
6
+ SINGLE = '└── '
7
+ EMPTY = ' '
8
+
9
+ def initialize(tree)
10
+ @tree = tree
11
+ @label_method = :to_s
12
+ @children_method = :children
13
+ end
14
+
15
+ def label_method(method_symbol)
16
+ @label_method = method_symbol
17
+ self
18
+ end
19
+
20
+ def children_method(method_symbol)
21
+ @children_method = method_symbol
22
+ self
23
+ end
24
+
25
+ def to_s
26
+ build_lines(@tree).join("\n")
27
+ end
28
+
29
+ private
30
+
31
+ def build_lines(node)
32
+ return [node_label(node)] if node_children(node).empty?
33
+ result = node_children(node).map do |c|
34
+ build_lines(c)
35
+ end
36
+ result = [node_label(node)] + last_map(result,
37
+ ->(block){
38
+ first_map(block, ->(line){ DEFAULT+line }, ->(line){ LINE+line })
39
+ },
40
+ ->(block){
41
+ first_map(block, ->(line){ SINGLE+line }, ->(line){ EMPTY+line })
42
+ }
43
+ ).flatten(1)
44
+ end
45
+
46
+ def first_map(enumerable, first, other)
47
+ result = [first.call(enumerable.first)]
48
+ result += enumerable.drop(1).map do |e|
49
+ other.call(e)
50
+ end
51
+ result
52
+ end
53
+
54
+ def last_map(enumerable, other, last)
55
+ result = enumerable[0..-2].map do |e|
56
+ other.call(e)
57
+ end
58
+ result = result + [last.call(enumerable.last)]
59
+ result
60
+ end
61
+
62
+ def node_label(node)
63
+ node.public_send @label_method
64
+ end
65
+
66
+ def node_children(node)
67
+ node.public_send @children_method
68
+ end
69
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tree_outline/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tree_outline"
8
+ spec.version = TreeOutline::VERSION
9
+ spec.authors = ["flower-pot"]
10
+ spec.email = ["fbranczyk@gmail.com"]
11
+
12
+ spec.summary = "Stringifying trees"
13
+ spec.description = "Stringify trees in a fashion similar the output known from the unix tool 'tree'"
14
+ spec.homepage = "https://github.com/flower-pot/tree_outline"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'codeclimate-test-reporter'
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_outline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - flower-pot
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codeclimate-test-reporter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Stringify trees in a fashion similar the output known from the unix tool
84
+ 'tree'
85
+ email:
86
+ - fbranczyk@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .rspec
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/tree_outline.rb
101
+ - lib/tree_outline/version.rb
102
+ - tree_outline.gemspec
103
+ homepage: https://github.com/flower-pot/tree_outline
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Stringifying trees
127
+ test_files: []