aligned_table 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7dbee126d20129c2ec3d2d12d95e50f925efa69d
4
+ data.tar.gz: 7f2997860de015315179c9bb0441645a2c34814f
5
+ SHA512:
6
+ metadata.gz: 3821b6a25b7df02f93e87fa90d97350e9d79ec68c92b91916bd4ed2c6a5cec0ab4345cc7306d7628422631889ec29a76b098fc2071cfc80f70bb62c7d06c2f34
7
+ data.tar.gz: 2ff5468a03bad691a44681201612d6495e73718b3f886091a4589e61deb2574bf2a36552e405eab1cabcc07246522ceccb123ea433aa262cf8675fb8534ec09b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aligned_table.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Zak Kristjanson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # AlignedTable
2
+
3
+ An easy way to create simple lightweight text tables with a
4
+ right-aligned first column, total row, and title.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'aligned_table'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install aligned_table
19
+
20
+ ## Usage
21
+
22
+ ### Basic Usage
23
+ ```ruby
24
+ at = AlignedTable.new
25
+ at.title = "My Title"
26
+ at.separator = " | "
27
+ at.rows = [
28
+ ["My Cool Column", "Some data"],
29
+ ["More data", "Info"]
30
+ ]
31
+
32
+ puts at.render
33
+ ```
34
+
35
+ gives you
36
+
37
+ ```
38
+ ======= My Title =======
39
+ My Cool Column Some data
40
+ More data Info
41
+ ```
42
+
43
+ ### Custom Separators
44
+ ```ruby
45
+ at = AlignedTable.new
46
+ at.title = "My Title"
47
+
48
+ at.rows = [
49
+ ["My Cool Column", "Some data"],
50
+ ["More data", "Info"]
51
+ ]
52
+
53
+ puts at.render
54
+ ```
55
+
56
+ gives you
57
+
58
+ ```
59
+ ======== My Title ========
60
+ My Cool Column | Some data
61
+ More data | Info
62
+ ```
63
+
64
+
65
+ ### Column Lines
66
+ Pass a symbol instead of a string, and the output will be repeated
67
+ for the length of the line
68
+ ```ruby
69
+ at = AlignedTable.new
70
+ at.title = "My Title"
71
+
72
+ at.rows = [
73
+ ["My Cool Column", "Some data"],
74
+ ["More data", "Info"],
75
+ [nil, :-],
76
+ [nil, "Total: 15 bananas"]
77
+ ]
78
+
79
+ puts at.render
80
+ ```
81
+
82
+ gives you
83
+
84
+ ```
85
+ =========== My Title ===========
86
+ My Cool Column Some data
87
+ More data Info
88
+ -----------------
89
+ Total: 15 bananas
90
+ ```
91
+
92
+
93
+ ## Contributing
94
+
95
+ 1. Fork it ( http://github.com/ubercow/aligned_table/fork )
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aligned_table'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aligned_table"
8
+ spec.version = AlignedTable::VERSION
9
+ spec.authors = ["Zak Kristjanson"]
10
+ spec.email = ["zak.kristjanson@gmail.com"]
11
+ spec.summary = "An easy way to create simple lightweight text tables."
12
+ spec.homepage = "https://github.com/ubercow/aligned_table"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", "~> 2.14"
23
+ end
@@ -0,0 +1,56 @@
1
+ class AlignedTable
2
+ VERSION = "0.1.0"
3
+
4
+ attr_accessor :rows, :title, :separator
5
+
6
+ def initialize
7
+ @separator = " "
8
+ end
9
+
10
+ def render
11
+ col_len = column_lengths
12
+ rows = @rows.map do |row|
13
+ render_row(row, col_len)
14
+ end
15
+
16
+ max_row_length = rows.max_by(&:length).length
17
+
18
+ if @title
19
+ rows.unshift(" #@title ".center(max_row_length, "="))
20
+ end
21
+
22
+ rows.join("\n")
23
+ end
24
+
25
+ def column_lengths
26
+ columns = []
27
+
28
+ @rows.each do |row|
29
+ row.each_with_index do |col, index|
30
+ columns[index] ||= []
31
+ columns[index] << col
32
+ end
33
+ end
34
+
35
+ columns.map! do |col|
36
+ col.map { |x| x.to_s }.max_by(&:length).length
37
+ end
38
+
39
+ columns
40
+ end
41
+
42
+ def render_row(row, col_len)
43
+ columns = row.each_with_index.map do |col, index|
44
+ if col.is_a?(Symbol)
45
+ col.to_s * col_len[index]
46
+ else
47
+ if index == 0
48
+ col.to_s.rjust(col_len[index])
49
+ else
50
+ col.to_s.ljust(col_len[index])
51
+ end
52
+ end
53
+ end
54
+ columns.join(@separator)
55
+ end
56
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlignedTable do
4
+ describe ".column_lengths" do
5
+ context "with a column of nils" do
6
+ it "returns 0 for the column count" do
7
+ at = AlignedTable.new
8
+ at.rows = [[nil],[nil]]
9
+ expect(at.column_lengths).to eq([0])
10
+ end
11
+ end
12
+
13
+ context "with a column of strings" do
14
+ it "returns the maximum string length" do
15
+ at = AlignedTable.new
16
+ at.rows = [["some"],["random"],["content"]]
17
+ expect(at.column_lengths).to eq([7])
18
+ end
19
+ end
20
+
21
+ context "with multiple columns of strings" do
22
+ it "returns the maximum string length" do
23
+ at = AlignedTable.new
24
+ at.rows = [
25
+ ["some", "more"],
26
+ ["random", "things"],
27
+ ["content", "here"]
28
+ ]
29
+ expect(at.column_lengths).to eq([7, 6])
30
+ end
31
+ end
32
+ end
33
+
34
+ describe ".render_row" do
35
+ context "with a symbol" do
36
+ it "renders symbols as a repeated line" do
37
+ at = AlignedTable.new
38
+ rendered = at.render_row([:-], [5])
39
+ expect(rendered).to eq("-----")
40
+ end
41
+ end
42
+
43
+ context "with rows with text" do
44
+ it "renders the first column left padded" do
45
+ at = AlignedTable.new
46
+ rendered = at.render_row(["col1", "col2"], [5, 4])
47
+ expect(rendered).to eq(" col1 col2")
48
+ end
49
+
50
+ it "renders additional columns right padded" do
51
+ at = AlignedTable.new
52
+ rendered = at.render_row(["col1", "col2"], [5, 7])
53
+ expect(rendered).to eq(" col1 col2 ")
54
+ end
55
+ end
56
+
57
+ describe "with a nonstandard separator" do
58
+ it "renders rows with separator" do
59
+ at = AlignedTable.new
60
+ at.separator = " | "
61
+ rendered = at.render_row(["col1", "col2"], [3, 3])
62
+ expect(rendered).to eq("col1 | col2")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe ".rows" do
68
+ context "with a title" do
69
+ it "renders the title before rows" do
70
+ at = AlignedTable.new
71
+ at.title = "Table"
72
+ at.rows = [
73
+ ["hey", "what's"],
74
+ ["going", "on"]
75
+ ]
76
+ output = at.render
77
+ expect(output).to eq("== Table ===\n hey what's\ngoing on ")
78
+ end
79
+ end
80
+
81
+ context "without a title" do
82
+ it "renders the rows" do
83
+ at = AlignedTable.new
84
+ at.rows = [
85
+ ["hey", "what's"],
86
+ ["going", "on"]
87
+ ]
88
+ output = at.render
89
+ expect(output).to eq(" hey what's\ngoing on ")
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+
5
+ require 'aligned_table'
6
+
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :rspec do |c|
10
+ c.syntax = :expect
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aligned_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zak Kristjanson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-14 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description:
56
+ email:
57
+ - zak.kristjanson@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - aligned_table.gemspec
69
+ - lib/aligned_table.rb
70
+ - spec/lib/aligned_table_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/ubercow/aligned_table
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: An easy way to create simple lightweight text tables.
96
+ test_files:
97
+ - spec/lib/aligned_table_spec.rb
98
+ - spec/spec_helper.rb
99
+ has_rdoc: