taulukko 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/taulukko.rb +77 -0
- metadata +46 -0
data/lib/taulukko.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
class Taulukko
|
2
|
+
attr_accessor :headings
|
3
|
+
attr_accessor :spacing
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@rows = []
|
7
|
+
@widths = []
|
8
|
+
@spacing = 2
|
9
|
+
end
|
10
|
+
|
11
|
+
def add array
|
12
|
+
update_widths array
|
13
|
+
@rows << array
|
14
|
+
end
|
15
|
+
alias :<< :add
|
16
|
+
|
17
|
+
def render
|
18
|
+
buffer = ''
|
19
|
+
|
20
|
+
unless @headings.nil?
|
21
|
+
update_widths @headings
|
22
|
+
buffer += render_row @headings
|
23
|
+
buffer += "\n"
|
24
|
+
buffer += render_separator
|
25
|
+
buffer += "\n" unless @rows.empty?
|
26
|
+
end
|
27
|
+
|
28
|
+
@rows.each_with_index do |row, index|
|
29
|
+
buffer += render_row row
|
30
|
+
buffer += "\n" unless (index == @rows.size - 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
buffer
|
34
|
+
end
|
35
|
+
alias :to_s :render
|
36
|
+
|
37
|
+
def render_row row
|
38
|
+
buffer = ''
|
39
|
+
|
40
|
+
row.each_with_index do |cell, index|
|
41
|
+
if index != row.size - 1
|
42
|
+
buffer += cell.to_s.ljust(@widths[index] + @spacing)
|
43
|
+
elsif
|
44
|
+
buffer += cell.to_s
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
buffer
|
49
|
+
end
|
50
|
+
|
51
|
+
def render_separator
|
52
|
+
buffer = ''
|
53
|
+
|
54
|
+
@widths.each_with_index do |size, index|
|
55
|
+
if index != @widths.size - 1
|
56
|
+
buffer += "".ljust(size, '-').ljust(size + @spacing)
|
57
|
+
elsif
|
58
|
+
buffer += "".ljust(size, '-')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
buffer
|
63
|
+
end
|
64
|
+
|
65
|
+
def update_widths array
|
66
|
+
array.each_with_index do |cell, index|
|
67
|
+
size = cell.to_s.size
|
68
|
+
|
69
|
+
if @widths[index].nil?
|
70
|
+
@widths[index] = size
|
71
|
+
elsif @widths[index] < size
|
72
|
+
@widths[index] = size
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: taulukko
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Enric Ruiz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-30 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Generates easy grepable and cutable tables
|
15
|
+
email:
|
16
|
+
- enric.ruizmartinez@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/taulukko.rb
|
22
|
+
homepage: http://www.github.com/enricruiz/taulukko
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.15
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Generates easy grepable and cutable tables
|
46
|
+
test_files: []
|