text-table 1.2.2 → 1.2.3

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5637e2fb2a77c1c4c6f019b4120e6d1dfc547336
4
+ data.tar.gz: da83a6185a3c4acac5e857594dd187fe43544b76
5
+ SHA512:
6
+ metadata.gz: da85599487daf6768f1a466e20950418cc65905782a22972a27e0b59c70b4d63df5ab41c945a59e378f065b7c224026a9d712e4b18b174523c0276d0bb67307a
7
+ data.tar.gz: 359c082e3f514a9b706ff5a82a1df813ab82c89ee0802c1883f8630eb8a87eff4e70aa400bfc79936c9bf36ae3b0d8c81e81b5d2585930c7dfbdb769c46bae50
data/.document CHANGED
@@ -1,5 +1,4 @@
1
- README.rdoc
1
+ CHANGELOG.rdoc
2
2
  lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
3
+ README.rdoc
5
4
  LICENSE
data/.gitignore CHANGED
@@ -1,24 +1,6 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## NETBEANS
17
- nbproject
18
-
19
- ## PROJECT::GENERAL
20
- coverage
21
- rdoc
22
- pkg
23
-
24
- ## PROJECT::SPECIFIC
1
+ bin/
2
+ .bundle/
3
+ doc/
4
+ Gemfile.lock
5
+ tmp/
6
+ vendor/bundle/
@@ -1,6 +1,10 @@
1
+ === 1.2.3 (2013-04-15)
2
+
3
+ * Fixed performance issue with large number of rows (thanks Kaoru Maeda)
4
+
1
5
  === 1.2.2 (2010-03-29)
2
6
 
3
- * Fix Ruby 1.9 warnings on shadowed outer local variables (thanks Claudio Bustos)
7
+ * Fixed Ruby 1.9 warnings on shadowed outer local variables (thanks Claudio Bustos)
4
8
 
5
9
  === 1.2.1 (2010-01-05)
6
10
 
@@ -18,4 +22,4 @@
18
22
 
19
23
  === 1.0.1 (2009-12-21)
20
24
 
21
- * Initial stable release.
25
+ * Initial stable release.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -17,13 +17,33 @@ Text::Table is compatible with Ruby 1.8.6, 1.8.7 and 1.9.1 and includes a compre
17
17
  gem install text-table
18
18
 
19
19
 
20
+ == Examples
21
+
22
+ require 'text-table'
23
+
24
+ table = Text::Table.new
25
+ table.head = ['A', 'B']
26
+ table.rows = [['a1', 'b1']]
27
+ table.rows << ['a2', 'b2']
28
+
29
+ table.to_s
30
+
31
+ # +----+----+
32
+ # | A | B |
33
+ # +----+----+
34
+ # | a1 | b1 |
35
+ # | a2 | b2 |
36
+ # +----+----+
37
+
38
+ You can also pass an options hash when instantiating a Text::Table:
39
+
40
+ table = Text::Table.new(:head => ['A', 'B'], :rows => [['a1', 'b1'], ['a2', 'b2']])
41
+
42
+
20
43
  == Calling the <tt>to_table</tt> Method
21
44
 
22
45
  Just call the <tt>to_table</tt> method (or <tt>to_text_table</tt> if the former is already defined) on Arrays (and other Enumerables).
23
46
 
24
- require 'rubygems'
25
- require 'text-table'
26
-
27
47
  array = [
28
48
  ['Student', 'Mid-Terms', 'Finals'],
29
49
  ['Sam', 94, 93],
@@ -66,30 +86,6 @@ You could also specify that the last row is the table footer.
66
86
  # +---------+-----------+--------+
67
87
 
68
88
 
69
- == Creating a New Text::Table Object
70
-
71
- You could create a Text::Table object by passing an options hash:
72
-
73
- table = Text::Table.new(:head => ['A', 'B'], :rows => [['a1', 'b1'], ['a2', 'b2']])
74
-
75
- Or by passing a block:
76
-
77
- table = Text::Table.new do |t|
78
- t.head = ['A', 'B']
79
- t.rows = [['a1', 'b1']]
80
- t.rows << ['a2', 'b2']
81
- end
82
-
83
- table.to_s
84
-
85
- # +----+----+
86
- # | A | B |
87
- # +----+----+
88
- # | a1 | b1 |
89
- # | a2 | b2 |
90
- # +----+----+
91
-
92
-
93
89
  == Aligning Cells and Spanning Columns
94
90
 
95
91
  Alignment and column span can be specified by passing a cell as a Hash object.
@@ -98,13 +94,12 @@ The acceptable aligments are <tt>:left</tt>, <tt>:center</tt> and <tt>:right</tt
98
94
 
99
95
  Cells and footers are aligned to the left by default, while headers are centered by default.
100
96
 
101
- table = Text::Table.new do |t|
102
- t.head = ['Heading A', 'Heading B']
103
- t.rows << ['a1', 'b1']
104
- t.rows << ['a2', {:value => 'b2', :align => :right}]
105
- t.rows << ['a3', 'b3']
106
- t.rows << [{:value => 'a4', :colspan => 2, :align => :center}]
107
- end
97
+ table = Text::Table.new
98
+ table.head = ['Heading A', 'Heading B']
99
+ table.rows << ['a1', 'b1']
100
+ table.rows << ['a2', {:value => 'b2', :align => :right}]
101
+ table.rows << ['a3', 'b3']
102
+ table.rows << [{:value => 'a4', :colspan => 2, :align => :center}]
108
103
 
109
104
  puts table
110
105
 
@@ -187,9 +182,12 @@ Thanks to the authors and contributors of these projects.
187
182
 
188
183
  == Contributors
189
184
 
190
- * Claudio Bustos (clbustos[link:http://github.com/clbustos])
185
+ * Claudio Bustos (clbustos[http://github.com/clbustos])
191
186
  * Fix Ruby 1.9 warnings on shadowed outer local variables
192
187
 
188
+ * Kaoru Maeda (mad-p[https://github.com/mad-p])
189
+ * Fixed performance issue with large number of rows
190
+
193
191
 
194
192
  == Copyright
195
193
 
@@ -151,6 +151,7 @@ module Text #:nodoc:
151
151
  end
152
152
 
153
153
  def column_widths #:nodoc:
154
+ @column_widths ||= \
154
155
  all_text_table_rows.reject {|row| row.cells == :separator}.map do |row|
155
156
  row.cells.map {|cell| [(cell.value.length/cell.colspan.to_f).ceil] * cell.colspan}.flatten
156
157
  end.transpose.map(&:max)
@@ -0,0 +1,5 @@
1
+ module Text
2
+ class Table
3
+ VERSION = '1.2.3'
4
+ end
5
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe Text::Table::Cell do
4
4
  before(:each) do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe Enumerable do
4
4
  describe Array do
@@ -11,45 +11,45 @@ describe Enumerable do
11
11
  end
12
12
 
13
13
  it "to Text::Table" do
14
- @arr.to_text_table.to_s.should == <<-EOS.deindent
14
+ @arr.to_text_table.to_s.should == deindent(%q{
15
15
  +----+-----+------+
16
16
  | 11 | 2 | 3333 |
17
17
  | 44 | 56 | 6 |
18
18
  | 7 | 888 | 99 |
19
19
  +----+-----+------+
20
- EOS
20
+ })
21
21
  end
22
22
 
23
23
  it "to Text::Table using flat array" do
24
- [11, 44, 7].to_text_table.to_s.should == <<-EOS.deindent
24
+ [11, 44, 7].to_text_table.to_s.should == deindent(%q{
25
25
  +----+
26
26
  | 11 |
27
27
  | 44 |
28
28
  | 7 |
29
29
  +----+
30
- EOS
30
+ })
31
31
  end
32
32
 
33
33
  it "to Text::Table, setting first row as head" do
34
- @arr.to_text_table(:first_row_is_head => true).to_s.should == <<-EOS.deindent
34
+ @arr.to_text_table(:first_row_is_head => true).to_s.should == deindent(%q{
35
35
  +----+-----+------+
36
36
  | 11 | 2 | 3333 |
37
37
  +----+-----+------+
38
38
  | 44 | 56 | 6 |
39
39
  | 7 | 888 | 99 |
40
40
  +----+-----+------+
41
- EOS
41
+ })
42
42
  end
43
43
 
44
44
  it "to Text::Table, setting last row as foot" do
45
- @arr.to_text_table(:last_row_is_foot => true).to_s.should == <<-EOS.deindent
45
+ @arr.to_text_table(:last_row_is_foot => true).to_s.should == deindent(%q{
46
46
  +----+-----+------+
47
47
  | 11 | 2 | 3333 |
48
48
  | 44 | 56 | 6 |
49
49
  +----+-----+------+
50
50
  | 7 | 888 | 99 |
51
51
  +----+-----+------+
52
- EOS
52
+ })
53
53
  end
54
54
  end
55
55
 
@@ -61,32 +61,32 @@ describe Enumerable do
61
61
  }
62
62
  end
63
63
  it "to Text::Table" do
64
- @hsh.to_text_table.to_s.should == <<-EOS.deindent
64
+ @hsh.to_text_table.to_s.should == deindent(%q{
65
65
  +---+----+
66
66
  | k | vv |
67
67
  | k | vv |
68
68
  +---+----+
69
- EOS
69
+ })
70
70
  end
71
71
 
72
72
  it "to Text::Table, setting first row as head" do
73
- @hsh.to_text_table(:first_row_is_head => true).to_s.should == <<-EOS.deindent
73
+ @hsh.to_text_table(:first_row_is_head => true).to_s.should == deindent(%q{
74
74
  +---+----+
75
75
  | k | vv |
76
76
  +---+----+
77
77
  | k | vv |
78
78
  +---+----+
79
- EOS
79
+ })
80
80
  end
81
81
 
82
82
  it "to Text::Table, setting last row as foot" do
83
- @hsh.to_text_table(:last_row_is_foot => true).to_s.should == <<-EOS.deindent
83
+ @hsh.to_text_table(:last_row_is_foot => true).to_s.should == deindent(%q{
84
84
  +---+----+
85
85
  | k | vv |
86
86
  +---+----+
87
87
  | k | vv |
88
88
  +---+----+
89
- EOS
89
+ })
90
90
  end
91
91
  end
92
92
  end
@@ -0,0 +1,183 @@
1
+ require 'integration_helper'
2
+
3
+ describe Text::Table do
4
+ let(:table) { Text::Table.new :rows => rows, :head => head, :foot => foot }
5
+ let(:rows) { @rows }
6
+ let(:head) { @head }
7
+ let(:foot) { @foot }
8
+ subject { table.to_s }
9
+
10
+ describe 'header alignment' do
11
+ let(:head) { @head.map { |heading| { :value => heading, :align => align } } }
12
+ let(:foot) { nil }
13
+
14
+ context 'when left' do
15
+ let(:align) { :left }
16
+
17
+ it { should == deindent(%q{
18
+ +-----+------+------+------+
19
+ | a | bb | ccc | dddd |
20
+ +-----+------+------+------+
21
+ | aa | bbb | cccc | d |
22
+ | aaa | bbbb | c | dd |
23
+ +-----+------+------+------+
24
+ }) }
25
+ end
26
+
27
+ context 'when center' do
28
+ let(:align) { :center }
29
+
30
+ it { should == deindent(%q{
31
+ +-----+------+------+------+
32
+ | a | bb | ccc | dddd |
33
+ +-----+------+------+------+
34
+ | aa | bbb | cccc | d |
35
+ | aaa | bbbb | c | dd |
36
+ +-----+------+------+------+
37
+ }) }
38
+ end
39
+
40
+ context 'when right' do
41
+ let(:align) { :right }
42
+
43
+ it { should == deindent(%q{
44
+ +-----+------+------+------+
45
+ | a | bb | ccc | dddd |
46
+ +-----+------+------+------+
47
+ | aa | bbb | cccc | d |
48
+ | aaa | bbbb | c | dd |
49
+ +-----+------+------+------+
50
+ }) }
51
+ end
52
+ end
53
+
54
+ describe 'cell alignment' do
55
+ let(:rows) { @rows.map { |row| row.map { |cell| { :value => cell, :align => align } } } }
56
+
57
+ context 'when left' do
58
+ let(:align) { :left }
59
+
60
+ it { should == deindent(%q{
61
+ +------+------+------+------+
62
+ | a | bb | ccc | dddd |
63
+ +------+------+------+------+
64
+ | aa | bbb | cccc | d |
65
+ | aaa | bbbb | c | dd |
66
+ +------+------+------+------+
67
+ | aaaa | b | cc | ddd |
68
+ +------+------+------+------+
69
+ }) }
70
+ end
71
+
72
+ context 'when center' do
73
+ let(:align) { :center }
74
+
75
+ it { should == deindent(%q{
76
+ +------+------+------+------+
77
+ | a | bb | ccc | dddd |
78
+ +------+------+------+------+
79
+ | aa | bbb | cccc | d |
80
+ | aaa | bbbb | c | dd |
81
+ +------+------+------+------+
82
+ | aaaa | b | cc | ddd |
83
+ +------+------+------+------+
84
+ }) }
85
+ end
86
+
87
+ context 'when right' do
88
+ let(:align) { :right }
89
+
90
+ it { should == deindent(%q{
91
+ +------+------+------+------+
92
+ | a | bb | ccc | dddd |
93
+ +------+------+------+------+
94
+ | aa | bbb | cccc | d |
95
+ | aaa | bbbb | c | dd |
96
+ +------+------+------+------+
97
+ | aaaa | b | cc | ddd |
98
+ +------+------+------+------+
99
+ }) }
100
+ end
101
+ end
102
+
103
+ describe 'footer alignment' do
104
+ let(:head) { nil }
105
+ let(:foot) { @foot.map { |footing| { :value => footing, :align => align } } }
106
+
107
+ context 'when left' do
108
+ let(:align) { :left }
109
+
110
+ it { should == deindent(%q{
111
+ +------+------+------+-----+
112
+ | aa | bbb | cccc | d |
113
+ | aaa | bbbb | c | dd |
114
+ +------+------+------+-----+
115
+ | aaaa | b | cc | ddd |
116
+ +------+------+------+-----+
117
+ }) }
118
+ end
119
+
120
+ context 'when center' do
121
+ let(:align) { :center }
122
+
123
+ it { should == deindent(%q{
124
+ +------+------+------+-----+
125
+ | aa | bbb | cccc | d |
126
+ | aaa | bbbb | c | dd |
127
+ +------+------+------+-----+
128
+ | aaaa | b | cc | ddd |
129
+ +------+------+------+-----+
130
+ }) }
131
+ end
132
+
133
+ context 'when right' do
134
+ let(:align) { :right }
135
+
136
+ it { should == deindent(%q{
137
+ +------+------+------+-----+
138
+ | aa | bbb | cccc | d |
139
+ | aaa | bbbb | c | dd |
140
+ +------+------+------+-----+
141
+ | aaaa | b | cc | ddd |
142
+ +------+------+------+-----+
143
+ }) }
144
+ end
145
+ end
146
+
147
+ describe 'table-wide alignment' do
148
+ context 'of columns' do
149
+ before do
150
+ table.rows[1][2] = {:value => 'c', :align => :center}
151
+ table.rows << [{:value => 'x', :colspan => 2}, 'c', 'd']
152
+ table.rows << ['a', 'b', {:value => 'x', :colspan => 2}]
153
+ table.align_column 2, :right
154
+ table.align_column 3, :right
155
+ end
156
+
157
+ it { should == deindent(%q{
158
+ +------+------+------+------+
159
+ | a | bb | ccc | dddd |
160
+ +------+------+------+------+
161
+ | aa | bbb | cccc | d |
162
+ | aaa | bbbb | c | dd |
163
+ | x | c | d |
164
+ | a | b | x |
165
+ +------+------+------+------+
166
+ | aaaa | b | cc | ddd |
167
+ +------+------+------+------+
168
+ }) }
169
+ end
170
+
171
+ context 'of rows' do
172
+ pending
173
+ end
174
+
175
+ context 'of headers' do
176
+ pending
177
+ end
178
+
179
+ context 'of footers' do
180
+ pending
181
+ end
182
+ end
183
+ end