tablizer 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Nicolas Oga
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.
@@ -0,0 +1,47 @@
1
+ Tablizer
2
+ ========
3
+
4
+ # DESCRIPTION
5
+
6
+ Tablizer is a gem that provides you a tool for quickly rendering tables, and has ANSI support for printing color strings into your console.
7
+
8
+ This should do the trick:
9
+
10
+ ``` ruby
11
+ table = [
12
+ ['animal', 'familia', 'cantidad de patas', 'pelaje' ],
13
+ ['ornitorrinco', 'mamiferos', '4', 'mediano y marron' , "caca"],
14
+ ['pato', 'aves', '2', 'plumas blancas' ],
15
+ ['gato', 'mamiferos', '4', 'gris y corto' ],
16
+ ['marmota', 'mamiferos', '4', 'color natural y corto' ]
17
+ ]
18
+
19
+ mytable = Table.new(table, header: true, align: 'right')
20
+ puts mytable
21
+
22
+ => +------------+---------+-----------------+---------------------+----+
23
+ | Animal | Familia |Cantidad de patas| Pelaje | |
24
+ +------------+---------+-----------------+---------------------+----+
25
+ |ornitorrinco|mamiferos| 4| mediano y marron|caca|
26
+ | pato| aves| 2| plumas blancas| |
27
+ | gato|mamiferos| 4| gris y corto| |
28
+ | marmota|mamiferos| 4|color natural y corto| |
29
+ +------------+---------+-----------------+---------------------+----+
30
+ ```
31
+
32
+ The table object has the [] and []= methods, that lets you access it's data and manipulate it.
33
+
34
+ ``` ruby
35
+ mytable[0,3] # => 'gato'
36
+ mytable[0,6] # => nil
37
+ mytable[3,2] = 'trololo'
38
+
39
+ => +------------+---------+-----------------+---------------------+----+
40
+ | Animal | Familia |Cantidad de patas| Pelaje | |
41
+ +------------+---------+-----------------+---------------------+----+
42
+ |ornitorrinco|mamiferos| 4| mediano y marron|caca|
43
+ | pato| aves| 2| trololo| |
44
+ | gato|mamiferos| 4| gris y corto| |
45
+ | marmota|mamiferos| 4|color natural y corto| |
46
+ +------------+---------+-----------------+---------------------+----+
47
+ ```
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rdoc/task'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
@@ -0,0 +1,5 @@
1
+ module Tablizer
2
+ VERSION = '1.0.2'
3
+ end
4
+
5
+ require_relative 'tablizer/table'
@@ -0,0 +1,40 @@
1
+ module Tablizer
2
+
3
+ # Some extensions to String to deal with ANSI colors.
4
+ module StringANSI
5
+
6
+ COLORS = {
7
+ :black => 30,
8
+ :red => 31,
9
+ :green => 32,
10
+ :brown => 33,
11
+ :blue => 34,
12
+ :magenta => 35,
13
+ :cyan => 36,
14
+ :gray => 37
15
+ }
16
+
17
+ def color(color)
18
+ code = "\033[#{COLORS[color]}m"
19
+ end_code = "\033[0m"
20
+ return "#{code}#{self}#{end_code}"
21
+ end
22
+
23
+ def ansi_length
24
+ gsub(/\033\[\d+m/, "").length
25
+ end
26
+
27
+ def ansi_rjust(len, char = " ")
28
+ rjust(len + length - ansi_length, char)
29
+ end
30
+
31
+ def ansi_ljust(len, char = " ")
32
+ ljust(len + length - ansi_length, char)
33
+ end
34
+
35
+ def ansi_center(len, char = " ")
36
+ center(len + length - ansi_length, char)
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,12 @@
1
+ module Tablizer
2
+
3
+ module ExistChecker
4
+ def exists?
5
+ return false if self.nil?
6
+ return false if self.is_a?(String) and self.length == 0
7
+ return false if self.is_a?(Numeric) and self == 0
8
+ true
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1,137 @@
1
+ require_relative 'ansi_string'
2
+ require_relative 'conditional'
3
+
4
+ class String
5
+ include Tablizer::StringANSI
6
+ end
7
+
8
+ module Tablizer
9
+ class Table
10
+ include Enumerable
11
+
12
+ attr_accessor :options, :content
13
+
14
+ DEFAULTS = {
15
+ align: 'ansi_ljust',
16
+ header: false,
17
+ footer: false
18
+ }
19
+
20
+ def initialize(content = [], params = {})
21
+ @options = DEFAULTS.merge(params)
22
+
23
+ raise "Cannot set rows and columns at the same time." if @options.has_key?(:cols) && @options.has_key?(:rows)
24
+
25
+ set_content content
26
+ end
27
+
28
+ def set_content(content)
29
+ @content = content.dup
30
+ end
31
+
32
+ # The []= method let's you access the Table object data as if it was a 2 dimension Array.
33
+ # You can call []= in the form 'obj[col,row] = value', or, in mathematical terms, 'obj[x,y] = value'
34
+ def []=(col, row, value)
35
+ @content[row] ||= []
36
+ @content[row][col] = value
37
+ end
38
+
39
+ def [](col, row)
40
+ @content[row][col].to_s
41
+ end
42
+
43
+ def empty?
44
+ each { |elem| return false if elem =~ /\S/ }
45
+ true
46
+ end
47
+
48
+ def each(&block)
49
+ block_given? ? enumerator.each(&block) : enumerator
50
+ end
51
+
52
+ def enumerator
53
+ Enumerator.new do |yielder|
54
+ number_of_columns, number_of_rows = columns, rows
55
+ (0...number_of_rows).each do |row_index|
56
+ (0...number_of_columns).each do |column_index|
57
+ yielder.yield self[column_index, row_index]
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def <=>(other)
64
+ size <=> other
65
+ end
66
+
67
+ def size
68
+ rows * columns
69
+ end
70
+
71
+ def to_s
72
+ tablize
73
+ end
74
+
75
+ def to_s_with_margins
76
+ ("\n" * 4) << to_s << ("\n" * 4)
77
+ end
78
+
79
+ def columns
80
+ @content.collect { |a| a.length }.max || 0
81
+ end
82
+
83
+ def rows
84
+ @content.length
85
+ end
86
+
87
+ private
88
+
89
+ def tablize
90
+ return "" if empty?
91
+
92
+ alignment = @options[:align]
93
+
94
+ number_of_columns, number_of_rows = columns, rows
95
+
96
+ # Getting maximun lengths for each column
97
+ length = (0...number_of_columns).map do |column_index|
98
+ (0...rows).map do |row_index|
99
+ self[column_index, row_index].ansi_length
100
+ end.max
101
+ end
102
+
103
+ # Generating the table itself
104
+ table = border(length)
105
+
106
+ (0...number_of_rows).each do |row_index|
107
+ table << '|'
108
+
109
+ if @options[:footer] && row_index == number_of_rows - 1 then table << border(length) end
110
+
111
+ if @options[:header] && row_index == 0 then
112
+ (0...number_of_columns).each do |column_index|
113
+ val = self[column_index, row_index]
114
+ table << val.capitalize.center(length[column_index]) << '|'
115
+ end
116
+
117
+ table << "\n" + border(length)
118
+ else
119
+ (0...number_of_columns).each do |column_index|
120
+ val = self[column_index, row_index] || EMPTY
121
+ table << val.send(alignment, length[column_index]) << '|'
122
+ end
123
+ table << "\n"
124
+ end
125
+ end
126
+
127
+ table << border(length)
128
+ end
129
+
130
+ def border(length)
131
+ border = '+'
132
+ length.each { |length| border << ('-' * length) << '+' }
133
+ border << "\n"
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,44 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'tablizer'
16
+ s.version = '1.0.2'
17
+ s.date = '2012-01-22'
18
+ s.rubyforge_project = 'tablizer'
19
+
20
+ s.summary = "Table object generator for ruby."
21
+ s.description = "Table object generator for ruby."
22
+
23
+ s.authors = ["Nicolas Oga"]
24
+ s.email = '2112.oga@gmail.com'
25
+ s.homepage = 'http://nicooga.github.com/Tablizer'
26
+
27
+ s.extra_rdoc_files = %w[README.md LICENSE]
28
+
29
+ # = MANIFEST =
30
+ s.files = %w[
31
+ LICENSE
32
+ README.md
33
+ Rakefile
34
+ lib/tablizer.rb
35
+ lib/tablizer/ansi_string.rb
36
+ lib/tablizer/conditional.rb
37
+ lib/tablizer/table.rb
38
+ tablizer.gemspec
39
+ test/test_tablizer.rb
40
+ ]
41
+ # = MANIFEST =
42
+
43
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
44
+ end
@@ -0,0 +1,107 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'tablizer/table'
4
+
5
+ class TestTablizer < MiniTest::Unit::TestCase
6
+ def data
7
+ [
8
+ ['animal' , 'familia' , 'cantidad de patas' , 'pelaje' ],
9
+ ['ornitorrinco' , 'mamiferos' , '4' , 'mediano y marron' , "caca"] ,
10
+ ['pato' , 'aves' , '2' , 'plumas blancas' ],
11
+ ['gato' , 'mamiferos' , '4' , 'gris y corto' ],
12
+ ['marmota' , 'mamiferos' , '4' , 'color natural y corto']
13
+ ]
14
+ end
15
+
16
+ def setup
17
+ @table = Tablizer::Table.new
18
+ end
19
+
20
+ def test_has_zero_columns_with_no_data
21
+ assert_equal @table.columns, 0
22
+ end
23
+
24
+ def test_outputs_an_empty_string_if_no_data
25
+ assert_equal @table.to_s, ""
26
+ end
27
+
28
+ def test_knows_its_size
29
+ @table.set_content(data)
30
+ assert_equal @table.size, 25
31
+ end
32
+
33
+ def test_each_works
34
+ @table.set_content([
35
+ ["1","2"],
36
+ ["3","4","5"],
37
+ ["6"]
38
+ ])
39
+ assert_equal @table.each.to_a, ["1", "2", "", "3", "4", "5", "6", "", ""]
40
+ end
41
+
42
+ def test_it_works
43
+ @table.set_content(data)
44
+ @table.options[:header] = false
45
+
46
+ output = (<<-OUTPUT).gsub(/^\s+/, "")
47
+ +------------+---------+-----------------+---------------------+----+
48
+ |animal |familia |cantidad de patas|pelaje | |
49
+ |ornitorrinco|mamiferos|4 |mediano y marron |caca|
50
+ |pato |aves |2 |plumas blancas | |
51
+ |gato |mamiferos|4 |gris y corto | |
52
+ |marmota |mamiferos|4 |color natural y corto| |
53
+ +------------+---------+-----------------+---------------------+----+
54
+ OUTPUT
55
+
56
+ assert_equal @table.to_s, output
57
+ end
58
+
59
+ def test_it_can_set_a_single_element
60
+ @table.set_content(data)
61
+ @table.options[:header] = true
62
+ @table.options[:align] = 'ansi_rjust'
63
+
64
+ @table[3,2] = 'trololo'
65
+
66
+ output = (<<-OUTPUT).gsub(/^\s+/, "")
67
+ +------------+---------+-----------------+---------------------+----+
68
+ | Animal | Familia |Cantidad de patas| Pelaje | |
69
+ +------------+---------+-----------------+---------------------+----+
70
+ |ornitorrinco|mamiferos| 4| mediano y marron|caca|
71
+ | pato| aves| 2| trololo| |
72
+ | gato|mamiferos| 4| gris y corto| |
73
+ | marmota|mamiferos| 4|color natural y corto| |
74
+ +------------+---------+-----------------+---------------------+----+
75
+ OUTPUT
76
+
77
+ assert_equal @table.to_s, output
78
+ end
79
+
80
+ # def test_works_with_ansi_strings
81
+ # @table.set_content(data)
82
+ # @table.options[:header] = false
83
+
84
+ # @table[0, 0] = @table[0, 1].color(:red)
85
+ # @table[1, 1] = @table[1, 1].color(:green)
86
+ # @table[2, 2] = @table[2, 2].color(:blue)
87
+
88
+ # puts @table.to_s_with_margins
89
+ # end
90
+
91
+ def test_can_be_filled_gradually
92
+ @table[0, 0] = "hola"
93
+ @table[1, 1] = "manola"
94
+ @table[2, 2] = "que tul"
95
+
96
+ output = (<<-OUTPUT).gsub(/^\s+/, "")
97
+ +----+------+-------+
98
+ |hola| | |
99
+ | |manola| |
100
+ | | |que tul|
101
+ +----+------+-------+
102
+ OUTPUT
103
+
104
+ assert_equal @table.to_s, output
105
+ end
106
+
107
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tablizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas Oga
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Table object generator for ruby.
15
+ email: 2112.oga@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ - LICENSE
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/tablizer.rb
26
+ - lib/tablizer/ansi_string.rb
27
+ - lib/tablizer/conditional.rb
28
+ - lib/tablizer/table.rb
29
+ - tablizer.gemspec
30
+ - test/test_tablizer.rb
31
+ homepage: http://nicooga.github.com/Tablizer
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: tablizer
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Table object generator for ruby.
55
+ test_files:
56
+ - test/test_tablizer.rb