table_print 1.5.2 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTViMjhhZmQ0MTQ4MjAzOWE2YjY3YmQzYzMyMjNkMmNiYzI5MzFhZg==
5
- data.tar.gz: !binary |-
6
- M2U2ZGI4ZjA1OTUyOWY1YTY1OGVhY2Q4OWE5NWY3YWJhMWFiNTdkOQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NWRlZGRjZmY5ZWY1OWU0YzYxODdhNmJkMzAwZjdhOGU3MGY2MjhiY2UxZmY3
10
- M2Q3OTVmZGMyZDI3MTE0NDJiZjNkZjkxNzM4MjFhMmIzNWIxYWE0NWYwZGY4
11
- OGM3ZmU5MzliYmI1M2I2MTg1Y2YxMTcxOTk4ZmY3OTk4NjAxYzQ=
12
- data.tar.gz: !binary |-
13
- Y2QzYTFiOWQ2ZmQyMTJjZDE3OWQ5ZDQyNzFhOTc2MjI1MjNkZmQ1ZGJhMGMx
14
- ZTViYWRlNTI4MTNmMDYzMmYyMDU4OTE5ODQ2ZDRmMzM1Y2NjZDc5NTJiNzI0
15
- NzU4OGE2MGQwYTM0ZjRlYjhlODBlYWIyZTU0OTFmYzNkZjdlZWM=
2
+ SHA256:
3
+ metadata.gz: 4d550bded70ef9a5ae903bdf98d2436d282ef8d89bf760dd65c0a90476131bb4
4
+ data.tar.gz: 858aee5efcc4d4ce91c0834cf215cb29e4899964d4bc4d3c624c49c9902b592d
5
+ SHA512:
6
+ metadata.gz: f7980699dada79051fdfbb5611815377eebc3200745c814401ee24955daa7d6d1d2838a72da1f942e9eca64d926974d2d044955067548a3e10b7a1be48c00e4c
7
+ data.tar.gz: 7f8eb1af627f5efc816d2751be6bc641cdca8ae74aab482e56af936266b83158d6d5eebbadf66cd48a508f974635200c045cfeafc2e8f7ba57dfee20a28cb5ce
@@ -1,12 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
- - 2.0.0
3
+ - 2.4
4
+ - 2.5
5
+ - 2.6
6
+ - 2.7
7
7
  - jruby-18mode
8
8
  - jruby-19mode
9
- - rbx-18mode
10
- - rbx-19mode
11
- - ree
12
9
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec :name => 'table_print'
4
4
 
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Chris Doyle
1
+ Copyright (c) 2012-2016 Chris Doyle
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -1,6 +1,6 @@
1
1
  = table_print
2
2
 
3
- {<img src="https://travis-ci.org/arches/table_print.png?branch=master" />}[http://travis-ci.org/arches/table_print]
3
+ {<img src="https://travis-ci.org/arches/table_print.svg?branch=master" />}[http://travis-ci.org/arches/table_print]
4
4
  {<img src="https://codeclimate.com/github/arches/table_print.png" />}[https://codeclimate.com/github/arches/table_print]
5
5
 
6
6
  TablePrint shows objects in nicely formatted columns for easy reading. It even lets you nest other tables
@@ -10,13 +10,15 @@ see exactly the data you care about.
10
10
  This {three minute screencast}[http://tableprintgem.com] will give you a quick overview of table_print's most
11
11
  powerful features and how to use them.
12
12
 
13
+ http://arches.io/tp_example.gif
14
+
13
15
  == Installation
14
16
 
15
17
  # Install as a standalone gem
16
18
  $ gem install table_print
17
19
 
18
- # Install within rails
19
- In your Gemfile: gem "table_print", "~> 1.0.0"
20
+ # Or install within rails
21
+ In your Gemfile: gem "table_print"
20
22
  $ bundle install
21
23
 
22
24
  == Usage
@@ -36,7 +38,7 @@ You should see something like this:
36
38
  AUTHOR | SUMMARY | TITLE
37
39
  ------------------|---------------------------------|------------------
38
40
  Michael Connelly | Another book by Michael Con... | The Fifth Witness
39
- Manning Mardale | From acclaimed historian Ma... | Malcolm X
41
+ Manning Marable | From acclaimed historian Ma... | Malcolm X
40
42
  Tina Fey | Worth it. -Trees | Bossypants
41
43
 
42
44
  TablePrint tries to use sensible defaults to choose the columns to show. If you're inspecting ActiveRecord objects, it
@@ -85,6 +87,8 @@ Available column options:
85
87
  * *formatters* - array of objects - each will be called in turn as the cells are printed. Must define a 'format' method. See below.
86
88
  * *time_format* - string - passed to strftime[http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime], only affects time columns
87
89
  * *width* - integer - how wide you want your column.
90
+ * *display_name* - string - useful if you want spaces in your column name
91
+ * *separator* - string - default is vertical bar for console and markdown, change it to a comma for CSV output
88
92
 
89
93
  ==== Display method
90
94
 
@@ -96,12 +100,16 @@ Columns are named after hash keys. To rename a column, use the name you want as
96
100
 
97
101
  You can pass a proc as the display_method for a column:
98
102
 
99
- tp User.all, :email, :monthly_payment, :yearly_payment => lambda{|u| u.monthly_payment * 12}
103
+ tp User.all, :email, :monthly_payment, yearly_payment: lambda{|u| u.monthly_payment * 12}
100
104
 
101
105
  Or if you want to pass other options along with the lambda:
102
106
 
103
107
  tp User.all, :yearly_payment => {:display_method => lambda{|u| u.monthly_payment * 12}, :width => 10}
104
108
 
109
+ Make sure to add curly braces if you have more than one column with a lambda or if this column isn't the last one.
110
+
111
+ tp User.all, :email, { daily_payment: lambda { |u| u.monthly_payment / 30} }, :monthly_payment, { yearly_payment: lambda { |u| u.monthly_payment * 12} }
112
+
105
113
  ==== Column formatters
106
114
 
107
115
  Similar to a lambda column, you can use a column formatter to reuse code across prints. Any object with a 'format' method
@@ -115,6 +123,22 @@ can be used to filter a column. This could also be used for coloring output.
115
123
 
116
124
  tp User.all, :bio => {:formatters => [NoNewlineFormatter.new]} # strip newlines from user bios
117
125
 
126
+ === HTML Output
127
+
128
+ Currently the best way to show table_print output on an HTML page is using a <pre> tag. You can create a helper
129
+ method to send table_print output directly into your <pre> tag:
130
+
131
+ def tp_pre data, options={}
132
+ content_tag :pre, TablePrint::Printer.new(data, options).table_print
133
+ end
134
+
135
+ === CSV Output
136
+
137
+ Set the column separator to get an output you can save as a CSV:
138
+
139
+ tp.set :separator, ","
140
+
141
+ Since your CSV viewer probably already handles column widths for you, setting +max_width+ to something very large will help you export your full data set. Otherwise, this CSV output will still be truncated to the default max_width of 30 characters.
118
142
 
119
143
  === Config
120
144
 
@@ -151,6 +175,15 @@ You can also set global options:
151
175
 
152
176
  tp.set :max_width, 10 # columns won't exceed 10 characters
153
177
  tp.set :time_format, "%Y" # date columns will only show the year
178
+ tp.set :capitalize_headers, false # don't capitalize column headers
179
+
180
+ You can redirect output:
181
+
182
+ f = File.open("users.txt", "w")
183
+ tp.set :io, f
184
+ tp User.all # written to users.txt instead of STDOUT
185
+ tp.clear :io
186
+ tp User.all # writing to STDOUT again
154
187
 
155
188
  === Multibyte
156
189
 
@@ -13,6 +13,49 @@ Feature: Configuring output
13
13
  ------|-----------------------------------------
14
14
  post! | Ryan Ryan Ryan Ryan Ryan Ryan Ryan Ry...
15
15
  """
16
+
17
+ Scenario: Setting a minimum width for an individual column
18
+ Given a class named Blog
19
+
20
+ Given Blog has attributes title, author
21
+
22
+ When I instantiate a Blog with {:title => "post!", :author => 'Ryan Ryan'}
23
+ And table_print Blog, {:include => {:author => {:min_width => 40}}}
24
+ Then the output should contain
25
+ """
26
+ TITLE | AUTHOR
27
+ ------|-----------------------------------------
28
+ post! | Ryan Ryan
29
+ """
30
+
31
+ Scenario: Setting a fixed width for an individual column, when data width is greater than fixed width
32
+ Given a class named Blog
33
+
34
+ Given Blog has attributes title, author
35
+
36
+ When I instantiate a Blog with {:title => "post!", :author => 'Ryan Ryan Ryan Ryan Ryan Ryan Ryan'}
37
+ And table_print Blog, {:include => {:author => {:fixed_width => 15}}}
38
+ Then the output should contain
39
+ """
40
+ TITLE | AUTHOR
41
+ ------|----------------
42
+ post! | Ryan Ryan Ry...
43
+ """
44
+
45
+ Scenario: Setting a fixed width for an individual column, when data width is less than fixed width
46
+ Given a class named Blog
47
+
48
+ Given Blog has attributes title, author
49
+
50
+ When I instantiate a Blog with {:title => "post!", :author => 'Ryan Ryan'}
51
+ And table_print Blog, {:include => {:author => {:fixed_width => 15}}}
52
+ Then the output should contain
53
+ """
54
+ TITLE | AUTHOR
55
+ ------|----------------
56
+ post! | Ryan Ryan
57
+ """
58
+
16
59
  Scenario: Specifying configuration on a per-object basis
17
60
  Given a class named Blog
18
61
 
@@ -55,3 +98,33 @@ Feature: Configuring output
55
98
  ------
56
99
  Ryan
57
100
  """
101
+
102
+ Scenario: Setting a lowercase column name
103
+ Given a class named Blog
104
+
105
+ Given Blog has attributes title, author
106
+
107
+ When I instantiate a Blog with {:title => "post!", :author => 'Ryan'}
108
+ And I configure capitalize_headers with false
109
+ And table_print Blog, {:author => {:display_name => "Wom Bat"}}
110
+ Then the output should contain
111
+ """
112
+ Wom Bat
113
+ -------
114
+ Ryan
115
+ """
116
+
117
+ Scenario: Setting the column separator
118
+ Given a class named Blog
119
+
120
+ Given Blog has attributes title, author
121
+
122
+ When I instantiate a Blog with {:title => "post!", :author => 'Ryan'}
123
+ And I configure separator with ','
124
+ And table_print Blog
125
+ Then the output should contain
126
+ """
127
+ TITLE , AUTHOR
128
+ ------,-------
129
+ post! , Ryan
130
+ """
@@ -1,3 +1,5 @@
1
1
  Before do
2
2
  Sandbox.cleanup!
3
+ TablePrint::Config.clear(:capitalize_headers)
4
+ TablePrint::Config.clear(:separator)
3
5
  end
@@ -62,6 +62,14 @@ When /^I configure multibyte with (.*)$/ do |value|
62
62
  TablePrint::Config.set(:multibyte, [value == "true"])
63
63
  end
64
64
 
65
+ When /^I configure capitalize_headers with (.*)$/ do |value|
66
+ TablePrint::Config.set(:capitalize_headers, [value == "true"])
67
+ end
68
+
69
+ When /^I configure separator with '(.*)'$/ do |value|
70
+ TablePrint::Config.set(:separator, [value])
71
+ end
72
+
65
73
  When /^configure (.*) with (.*)$/ do |klass, config|
66
74
  klass = Sandbox.const_get_from_string(klass)
67
75
  TablePrint::Config.set(klass, eval(config))
@@ -79,7 +87,7 @@ When /table_print ([\w\.:]*)$/ do |klass|
79
87
  end
80
88
 
81
89
  Then /^the output should contain$/ do |string|
82
- output = @r.lines.to_a
90
+ output = @r.each_line.to_a
83
91
 
84
92
  output.zip(string.split("\n")).each do |actual, expected|
85
93
  actual.gsub(/\s/m, "").split(//).sort.join.should == expected.gsub(" ", "").split(//).sort.join
@@ -51,7 +51,10 @@ module TablePrint
51
51
 
52
52
  def message
53
53
  return "Printed with config" if configged?
54
- Time.now - @start_time
54
+ (Time.now - @start_time).to_s
55
+ # the message is used to initiate Returnable
56
+ # whose argument is regarded in ruby 2.7.1 as string
57
+ # (ruby 2.7.1 calls ".includes?" method on this argument)
55
58
  end
56
59
 
57
60
  private
@@ -1,7 +1,7 @@
1
1
  module TablePrint
2
2
  class Column
3
3
  attr_reader :formatters
4
- attr_accessor :name, :data, :time_format, :default_width
4
+ attr_accessor :name, :data, :time_format, :default_width, :min_width, :fixed_width
5
5
 
6
6
  def initialize(attr_hash={})
7
7
  @formatters = []
@@ -48,7 +48,10 @@ module TablePrint
48
48
  end
49
49
 
50
50
  def width
51
- [(default_width || max_width), data_width].min
51
+ return fixed_width if fixed_width
52
+
53
+ width = [(default_width || max_width), data_width].min
54
+ [(min_width || 0), width].max
52
55
  end
53
56
 
54
57
  private
@@ -3,12 +3,16 @@ module TablePrint
3
3
 
4
4
  DEFAULT_MAX_WIDTH = 30
5
5
  DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
6
- DEFAULT_IO = STDOUT
6
+ DEFAULT_IO = $stdout
7
+ DEFAULT_CAPITALIZE_HEADERS = true
8
+ DEFAULT_SEPARATOR = "|"
7
9
 
8
10
  @@max_width = DEFAULT_MAX_WIDTH
9
11
  @@time_format = DEFAULT_TIME_FORMAT
10
12
  @@multibyte = false
11
- @@io = STDOUT
13
+ @@io = DEFAULT_IO
14
+ @@capitalize_headers = true
15
+ @@separator = DEFAULT_SEPARATOR
12
16
 
13
17
  @@klasses = {}
14
18
 
@@ -57,6 +61,22 @@ module TablePrint
57
61
  @@time_format = format
58
62
  end
59
63
 
64
+ def self.capitalize_headers
65
+ @@capitalize_headers
66
+ end
67
+
68
+ def self.capitalize_headers=(caps)
69
+ @@capitalize_headers = caps
70
+ end
71
+
72
+ def self.separator
73
+ @@separator
74
+ end
75
+
76
+ def self.separator=(separator)
77
+ @@separator = separator
78
+ end
79
+
60
80
  def self.io
61
81
  @@io
62
82
  end
@@ -73,6 +73,11 @@ module TablePrint
73
73
  option[:default_width] = option.delete(:width)
74
74
  end
75
75
 
76
+ if option.has_key? :display_name
77
+ option[:display_method] = option[:name]
78
+ option[:name] = option.delete(:display_name)
79
+ end
80
+
76
81
  c = Column.new(option)
77
82
  @column_hash[c.name] = c
78
83
  c
@@ -24,6 +24,6 @@ module TablePrint
24
24
 
25
25
  def inspect
26
26
  to_s
27
- end
27
+ end
28
28
  end
29
29
  end
@@ -64,7 +64,7 @@ module TablePrint
64
64
  def horizontal_separator
65
65
  columns.collect do |column|
66
66
  '-' * column.width
67
- end.join('-|-')
67
+ end.join("-#{TablePrint::Config.separator}-")
68
68
  end
69
69
 
70
70
  def header
@@ -73,7 +73,10 @@ module TablePrint
73
73
  f.format(column.name)
74
74
  end
75
75
 
76
- padded_names.join(" | ").upcase
76
+ header_string = padded_names.join(" #{TablePrint::Config.separator} ")
77
+ header_string.upcase! if TablePrint::Config.capitalize_headers
78
+
79
+ header_string
77
80
  end
78
81
 
79
82
  def add_formatter(name, formatter)
@@ -174,7 +177,7 @@ module TablePrint
174
177
  def format
175
178
  column_names = columns.collect(&:name)
176
179
 
177
- output = [column_names.collect { |name| apply_formatters(name, @cells[name]) }.join(" | ")]
180
+ output = [column_names.collect { |name| apply_formatters(name, @cells[name]) }.join(" #{TablePrint::Config.separator} ")]
178
181
  output.concat @children.collect { |g| g.format }
179
182
 
180
183
  output.join("\n")
@@ -218,8 +221,8 @@ module TablePrint
218
221
  formatters << FixedWidthFormatter.new(column_for(column_name).width)
219
222
 
220
223
  # successively apply the formatters for a column
221
- formatters.inject(value) do |value, formatter|
222
- formatter.format(value)
224
+ formatters.inject(value) do |inner_value, formatter|
225
+ formatter.format(inner_value)
223
226
  end
224
227
  end
225
228
 
@@ -1,4 +1,4 @@
1
1
  module TablePrint
2
- VERSION = "1.5.2"
2
+ VERSION = "1.5.7"
3
3
  end
4
4
 
@@ -206,6 +206,14 @@ describe TablePrint::ConfigResolver do
206
206
  c.columns.first.formatters.should == [f1, f2]
207
207
  end
208
208
  end
209
+ context "display_name" do
210
+ it "sets the display name on the column" do
211
+ c = TablePrint::ConfigResolver.new(Object, [], :title => {:display_name => "Ti Tle"})
212
+ c.columns.length.should == 1
213
+ c.columns.first.name.should == 'Ti Tle'
214
+ c.columns.first.display_method.should == "title"
215
+ end
216
+ end
209
217
  end
210
218
 
211
219
  describe "#option_to_column" do
@@ -25,8 +25,10 @@ describe TablePrint::Printer do
25
25
  end
26
26
 
27
27
  describe "message" do
28
- it "defaults to the time the print took" do
29
- Printer.new([]).message.should be_a Numeric
28
+ it "defaults to the time the print took, but in string" do
29
+ message = Printer.new([]).message
30
+ message.should be_a String
31
+ expect(message.to_f.to_s).to eq(message)
30
32
  end
31
33
 
32
34
  it "shows a warning if the printed objects have config" do
@@ -5,8 +5,8 @@ Gem::Specification.new do |gem|
5
5
  gem.name = "table_print"
6
6
 
7
7
  gem.authors = ["Chris Doyle"]
8
- gem.email = ["archslide@gmail.com"]
9
- gem.email = "archslide@gmail.com"
8
+ gem.email = ["chris@arches.io"]
9
+ gem.email = "chris@arches.io"
10
10
 
11
11
  gem.description = "TablePrint turns objects into nicely formatted columns for easy reading. Works great in rails console, works on pure ruby objects, autodetects columns, lets you traverse ActiveRecord associations. Simple, powerful."
12
12
  gem.summary = "Turn objects into nicely formatted columns for easy reading"
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.require_paths = ["lib"]
20
20
 
21
21
  gem.add_development_dependency 'cat', '~> 0.2.1'
22
- gem.add_development_dependency 'cucumber', '~> 1.2.1'
22
+ gem.add_development_dependency 'cucumber', '~> 2.4.0'
23
23
  gem.add_development_dependency 'rspec', '~> 2.11.0'
24
24
  gem.add_development_dependency 'rake', '~> 0.9.2'
25
25
  end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-10 00:00:00.000000000 Z
11
+ date: 2020-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cat
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.2.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.2.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: cucumber
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.2.1
33
+ version: 2.4.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 1.2.1
40
+ version: 2.4.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 2.11.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.11.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.9.2
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 0.9.2
69
69
  description: TablePrint turns objects into nicely formatted columns for easy reading.
70
70
  Works great in rails console, works on pure ruby objects, autodetects columns, lets
71
71
  you traverse ActiveRecord associations. Simple, powerful.
72
- email: archslide@gmail.com
72
+ email: chris@arches.io
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - .document
78
- - .gitignore
79
- - .rspec
80
- - .travis.yml
77
+ - ".document"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
81
  - Gemfile
82
82
  - LICENSE.txt
83
83
  - README.rdoc
@@ -124,17 +124,16 @@ require_paths:
124
124
  - lib
125
125
  required_ruby_version: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ! '>='
127
+ - - ">="
128
128
  - !ruby/object:Gem::Version
129
129
  version: '0'
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  requirements:
132
- - - ! '>='
132
+ - - ">="
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
- rubyforge_project:
137
- rubygems_version: 2.0.7
136
+ rubygems_version: 3.0.3
138
137
  signing_key:
139
138
  specification_version: 4
140
139
  summary: Turn objects into nicely formatted columns for easy reading