table_print 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/features/configuring_output.feature +5 -5
- data/lib/table_print/column.rb +8 -4
- data/lib/table_print/config_resolver.rb +6 -1
- data/lib/table_print/formatter.rb +3 -5
- data/lib/table_print/version.rb +1 -1
- data/spec/column_spec.rb +28 -5
- data/spec/config_resolver_spec.rb +5 -5
- data/spec/formatter_spec.rb +0 -13
- metadata +2 -2
@@ -5,13 +5,13 @@ Feature: Configuring output
|
|
5
5
|
|
6
6
|
Given Blog has attributes title, author
|
7
7
|
|
8
|
-
When I instantiate a Blog with {:title => "post!", :author => 'Ryan'}
|
9
|
-
And table_print Blog, {:include => {:author => {:width =>
|
8
|
+
When I instantiate a Blog with {:title => "post!", :author => 'Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ryan'}
|
9
|
+
And table_print Blog, {:include => {:author => {:width => 40}}}
|
10
10
|
Then the output should contain
|
11
11
|
"""
|
12
|
-
TITLE | AUTHOR
|
13
|
-
|
14
|
-
post! | Ryan
|
12
|
+
TITLE | AUTHOR
|
13
|
+
------------------------------------------------
|
14
|
+
post! | Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ry...
|
15
15
|
"""
|
16
16
|
Scenario: Specifying configuration on a per-object basis
|
17
17
|
Given a class named Blog
|
data/lib/table_print/column.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
module TablePrint
|
2
2
|
class Column
|
3
3
|
attr_reader :formatters
|
4
|
-
|
5
|
-
attr_accessor :name, :data, :time_format
|
4
|
+
attr_accessor :name, :data, :time_format, :default_width
|
6
5
|
|
7
6
|
def initialize(attr_hash={})
|
8
7
|
@formatters = []
|
@@ -35,11 +34,16 @@ module TablePrint
|
|
35
34
|
end
|
36
35
|
|
37
36
|
def data_width
|
38
|
-
[name.length].concat(data.compact.collect(&:to_s).collect(&:length)).max
|
37
|
+
[name.length].concat(Array(data).compact.collect(&:to_s).collect(&:length)).max
|
39
38
|
end
|
40
39
|
|
41
40
|
def width
|
42
|
-
|
41
|
+
[(default_width || max_width), data_width].min
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def max_width
|
46
|
+
TablePrint::Config.max_width
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
@@ -68,11 +68,16 @@ module TablePrint
|
|
68
68
|
else
|
69
69
|
option = {:name => option}
|
70
70
|
end
|
71
|
+
|
72
|
+
if option.has_key? :width
|
73
|
+
option[:default_width] = option.delete(:width)
|
74
|
+
end
|
75
|
+
|
71
76
|
c = Column.new(option)
|
72
77
|
@column_hash[c.name] = c
|
73
78
|
c
|
74
79
|
end
|
75
|
-
|
80
|
+
|
76
81
|
def usable_column_names
|
77
82
|
base = @default_columns
|
78
83
|
base = @only_columns unless @only_columns.empty?
|
@@ -18,18 +18,16 @@ module TablePrint
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class FixedWidthFormatter
|
21
|
+
attr_accessor :width
|
22
|
+
|
21
23
|
def initialize(width)
|
22
|
-
|
24
|
+
self.width = width
|
23
25
|
end
|
24
26
|
|
25
27
|
def format(value)
|
26
28
|
"%-#{width}s" % truncate(value)
|
27
29
|
end
|
28
30
|
|
29
|
-
def width
|
30
|
-
[@width, TablePrint::Config.max_width].min
|
31
|
-
end
|
32
|
-
|
33
31
|
private
|
34
32
|
def truncate(value)
|
35
33
|
return "" unless value
|
data/lib/table_print/version.rb
CHANGED
data/spec/column_spec.rb
CHANGED
@@ -58,13 +58,36 @@ describe Column do
|
|
58
58
|
end
|
59
59
|
|
60
60
|
describe "#width" do
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
context "when default width is specified" do
|
62
|
+
it "uses the default width" do
|
63
|
+
c.default_width = 10
|
64
|
+
c.stub(:data_width => 15)
|
65
|
+
c.stub(:max_width => 20)
|
66
|
+
c.width.should == 10
|
67
|
+
end
|
68
|
+
|
69
|
+
it "isn't limited by the config width" do
|
70
|
+
c.default_width = 40
|
71
|
+
c.stub(:data_width => 50)
|
72
|
+
c.stub(:max_width => 20)
|
73
|
+
c.width.should == 40
|
74
|
+
end
|
64
75
|
end
|
65
76
|
|
66
|
-
|
67
|
-
|
77
|
+
context "When default width is not specified" do
|
78
|
+
it "uses the data width" do
|
79
|
+
c.default_width = nil
|
80
|
+
c.stub(:data_width => 10)
|
81
|
+
c.stub(:max_width => 20)
|
82
|
+
c.width.should == 10
|
83
|
+
end
|
84
|
+
|
85
|
+
it "is limited by the config width" do
|
86
|
+
c.default_width = nil
|
87
|
+
c.stub(:data_width => 30)
|
88
|
+
c.stub(:max_width => 20)
|
89
|
+
c.width.should == 20
|
90
|
+
end
|
68
91
|
end
|
69
92
|
end
|
70
93
|
end
|
@@ -100,7 +100,7 @@ describe TablePrint::ConfigResolver do
|
|
100
100
|
c.columns.first.name.should == 'title'
|
101
101
|
|
102
102
|
c.columns.last.name.should == 'foo'
|
103
|
-
c.columns.last.
|
103
|
+
c.columns.last.default_width.should == 10
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
@@ -189,11 +189,11 @@ describe TablePrint::ConfigResolver do
|
|
189
189
|
end
|
190
190
|
end
|
191
191
|
context "width" do
|
192
|
-
it "sets the width" do
|
192
|
+
it "sets the default width" do
|
193
193
|
c = TablePrint::ConfigResolver.new(Object, [:title], :title => {:width => 100})
|
194
194
|
c.columns.length.should == 1
|
195
195
|
c.columns.first.name.should == 'title'
|
196
|
-
c.columns.first.
|
196
|
+
c.columns.first.default_width.should == 100
|
197
197
|
end
|
198
198
|
end
|
199
199
|
context "formatters" do
|
@@ -226,9 +226,9 @@ describe TablePrint::ConfigResolver do
|
|
226
226
|
context "with a hash" do
|
227
227
|
it "returns a column named foo and the specified options" do
|
228
228
|
c = TablePrint::ConfigResolver.new(Object, [])
|
229
|
-
column = c.option_to_column({:foo => {:
|
229
|
+
column = c.option_to_column({:foo => {:default_width => 10}})
|
230
230
|
column.name.should == 'foo'
|
231
|
-
column.
|
231
|
+
column.default_width.should == 10
|
232
232
|
end
|
233
233
|
end
|
234
234
|
end
|
data/spec/formatter_spec.rb
CHANGED
@@ -61,17 +61,4 @@ describe TablePrint::FixedWidthFormatter do
|
|
61
61
|
@f.format(123).should == "123 "
|
62
62
|
end
|
63
63
|
end
|
64
|
-
|
65
|
-
describe "#width" do
|
66
|
-
it "returns the width" do
|
67
|
-
@f.width.should == 10
|
68
|
-
end
|
69
|
-
|
70
|
-
it "respects the config'd max_width" do
|
71
|
-
max = TablePrint::Config.max_width
|
72
|
-
TablePrint::Config.max_width = 5
|
73
|
-
@f.width.should == 5
|
74
|
-
TablePrint::Config.max_width = max
|
75
|
-
end
|
76
|
-
end
|
77
64
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cat
|