table_display 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ba8622f69a321f9bf5642f544c08f8cec21c8173
4
- data.tar.gz: 87a83652fd7ca20ed437b44bb2ce88c9235d5a94
2
+ SHA256:
3
+ metadata.gz: c3013953f835a9c3948a8197eeebb9ffe9a53e8fb2ad14471a6802155205c03d
4
+ data.tar.gz: b569caed7b3c6b65327c6391b3fd63b68b0dd815b6068160fdfafed811fe76ed
5
5
  SHA512:
6
- metadata.gz: bc1f6b943a1be7442744c1cdec0b484b10c45847391ab9e331c132bd9a242eac13f118f5bfd0b2d062e22482ed7862d68b25a70c637399a4fb610832aa7751af
7
- data.tar.gz: 22de4f2301b19d812d54de2484df9f2856cc525a0c42f3943990904a93cc9ebeb1cb95f63caddc0622514b6a09a761ed80d06f7d37528aab30a0563edebe345c
6
+ metadata.gz: 3592665702f00ad8602e9a32a3c6e51fb8af848c302d2bd9220221cf116d315de2c3bbd705556266e3c4685055d1039f62f21a1479a2d22568aa6d1caabd0aec
7
+ data.tar.gz: f42df4ce0723ec6bdd8d4c18b323ca05b48c8c0579b6119edecb0117d41408ab1b856f19e5d4a7af44b3bbcb45ea74bb8c4cf71f7c156361c031f7a1553338e9
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Will Bryant, Sekuda Ltd
1
+ Copyright (c) 2009-2018 Will Bryant, Sekuda Ltd
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
@@ -0,0 +1,97 @@
1
+ Table Display
2
+ =============
3
+
4
+ Adds support for displaying your ActiveRecord tables, named scopes, collections, or
5
+ plain arrays in a table view when working in rails console, shell, or email template.
6
+
7
+ `Enumerable#to_table_display` returns the printable strings; `Object#pt` calls `#to_table_display`
8
+ on its first argument and puts out the result.
9
+
10
+ Columns you haven't loaded (eg. from using `:select`) are omitted, and derived/calculated
11
+ columns (eg. again, from using `:select`) are added.
12
+
13
+ Both `#to_table_display` and `Object#pt` methods take `:only`, `:except`, and `:methods` which
14
+ change what attributes/methods are output, like they do on the `#to_xml` method.
15
+
16
+ The normal output uses `#inspect` on the data values to make them printable, so you can
17
+ see what type the values had. When that's inconvenient or you'd prefer direct display,
18
+ you can pass the option `:inspect => false` to disable inspection.
19
+
20
+
21
+ Example
22
+ -------
23
+
24
+ You can call the `to_table_display` method:
25
+
26
+ >> puts Project.find(31).tasks.to_table_display
27
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
28
+ | id | project_id | description | due_on | completed_at | created_at | updated_at |
29
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
30
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | Tue Mar 24 23:17:05 +1300 2009 | Mon Mar 23 09:11:02 +1300 2009 | Tue Mar 24 23:17:05 +1300 2009 |
31
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | Mon Mar 23 09:11:46 +1300 2009 | Mon Mar 23 09:11:46 +1300 2009 |
32
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
33
+
34
+ Or equivalently, use `pt` (like `pp`, but in a table):
35
+
36
+ >> pt Customer.find(31).purchases
37
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
38
+ | id | project_id | description | due_on | completed_at | created_at | updated_at |
39
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
40
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | Tue Mar 24 23:17:05 +1300 2009 | Mon Mar 23 09:11:02 +1300 2009 | Tue Mar 24 23:17:05 +1300 2009 |
41
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | Mon Mar 23 09:11:46 +1300 2009 | Mon Mar 23 09:11:46 +1300 2009 |
42
+ +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
43
+
44
+
45
+ Like `to_xml`, you can pass a `:methods` option to add the output methods on your models, and you
46
+ can pass `:only` or `:except` to (respectively) show only certain columns or show all except certain columns:
47
+
48
+ >> puts Customer.find(31).purchases.to_table_display(:only => [:id, :description], :methods => [:met_due_date?])
49
+ +----+------------------------+---------------+
50
+ | id | description | met_due_date? |
51
+ +----+------------------------+---------------+
52
+ | 1 | "Write a handy plugin" | true |
53
+ | 2 | "Blog the plugin" | nil |
54
+ +----+------------------------+---------------+
55
+
56
+ `pt` accepts and passes on all options as well:
57
+
58
+ >> pt Customer.find(31).purchases, :only => [:id, :description], :methods => [:met_due_date?]
59
+ +----+------------------------+---------------+
60
+ | id | description | met_due_date? |
61
+ +----+------------------------+---------------+
62
+ | 1 | "Write a handy plugin" | true |
63
+ | 2 | "Blog the plugin" | nil |
64
+ +----+------------------------+---------------+
65
+
66
+ There's a convenient equivalent syntax for displaying an ordered list of columns, like `:only` and `:methods`:
67
+
68
+ >> puts Customer.find(31).purchases.to_table_display :id, :description, :met_due_date?
69
+
70
+ which provides:
71
+
72
+ >> pt Customer.find(31).purchases, :id, :description, :met_due_date?
73
+
74
+ resulting in the same output as above.
75
+
76
+
77
+ If `:inspect => false` is used, the values will be shown in `#to_s` form rather than `#inspect` form:
78
+
79
+ >> pt Customer.find(31).purchases, :only => [:id, :description, :due_on, :completed_at]
80
+ +----+----------------------+------------+--------------------------------+
81
+ | id | description | due_on | completed_at |
82
+ +----+----------------------+------------+--------------------------------+
83
+ | 1 | Write a handy plugin | 2009-03-25 | Tue Mar 24 23:17:05 +1300 2009 |
84
+ | 2 | Blog the plugin | 2009-04-05 | |
85
+ +----+----------------------+------------+--------------------------------+
86
+
87
+ It is possible to use objects that respond to `#call` -- such as methods or procs -- to set up columns. They will be
88
+ passed the record as their sole argument. If these have names (as with methods), those will be used as the headers.
89
+ Otherwise, the default `to_s` behaviour will be used.
90
+
91
+ Note that in all cases, values whose class descends from `Numeric` are right-aligned, while all other values are left-aligned.
92
+
93
+ Thanks
94
+ ------
95
+ * Michael Fowler (@mkrfowler)
96
+
97
+ Copyright (c) 2009-2018 Will Bryant, Sekuda Ltd, released under the MIT license
@@ -1,8 +1,8 @@
1
1
  module TableDisplay
2
2
  def to_table_display(*args)
3
3
  options = args.last.is_a?(Hash) ? args.pop : {}
4
- extra_methods = args.length > 0 ? args.collect(&:to_s) : []
5
- extra_methods += Array(options.delete(:methods)) if options[:methods]
4
+ extra_entries = args.collect { |arg| arg.respond_to?(:call) ? arg : arg.to_s }
5
+ extra_entries += Array(options.delete(:methods)) if options[:methods]
6
6
  only_attributes = Array(options.delete(:only)) if options[:only]
7
7
  only_attributes ||= [] if args.length > 0
8
8
  except_attributes = Array(options.delete(:except)) if options[:except]
@@ -12,7 +12,6 @@ module TableDisplay
12
12
 
13
13
  column_lengths = ActiveSupport::OrderedHash.new
14
14
 
15
- data = []
16
15
  if only_attributes
17
16
  # we've been given an explicit list of attributes to display
18
17
  only_attributes.each {|attribute| column_lengths[attribute] = 0}
@@ -50,12 +49,18 @@ module TableDisplay
50
49
  end
51
50
 
52
51
  # also add any :methods given to the list
53
- extra_methods.each {|name| column_lengths[name] = 0}
52
+ extra_entries.each {|name| column_lengths[name] = 0}
54
53
 
55
- each do |record|
54
+ data = collect do |record|
56
55
  # add the values for all the columns in our list in order they are
57
- data << column_lengths.collect do |attribute, max_width|
58
- value = record.is_a?(Hash) ? record[attribute] : record.send(attribute)
56
+ column_lengths.collect do |attribute, max_width|
57
+ value = if attribute.respond_to?(:call)
58
+ attribute.call(record)
59
+ elsif record.is_a?(Hash)
60
+ record[attribute]
61
+ else
62
+ record.send(attribute)
63
+ end
59
64
  string_value = display_inspect ? value.inspect : (value.is_a?(String) ? value : value.to_s)
60
65
  column_lengths[attribute] = string_value.mb_chars.length if string_value.mb_chars.length > max_width
61
66
  value.is_a?(Numeric) ? value : string_value # keep Numeric values as-is for now, so we can handle them specially in the output below
@@ -69,7 +74,7 @@ module TableDisplay
69
74
  heading_string = "|"
70
75
  column_lengths.each do |attribute, max_width|
71
76
  next unless max_width > 0 # skip any columns we never actually saw
72
- name = attribute.to_s
77
+ name = (attribute.respond_to?(:name) ? attribute.name : attribute).to_s
73
78
 
74
79
  # the column needs to fit the column header as well as the values
75
80
  if name.mb_chars.length > max_width
@@ -83,7 +88,7 @@ module TableDisplay
83
88
  rows = [separator_string, heading_string, separator_string]
84
89
  data.each do |data_row|
85
90
  data_string = "|"
86
- column_lengths.each_with_index do |(attribute, max_width), index|
91
+ column_lengths.each_with_index do |(_attribute, max_width), index|
87
92
  next unless max_width > 0 # skip any columns we never actually saw
88
93
  value = data_row[index]
89
94
  if value.is_a?(Numeric)
@@ -1,3 +1,3 @@
1
1
  module TableDisplay
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -21,7 +21,7 @@ end
21
21
  class Task < ActiveRecord::Base
22
22
  belongs_to :project
23
23
 
24
- scope :completed, :conditions => 'completed_at IS NOT NULL'
24
+ scope :completed, -> { where('completed_at IS NOT NULL') }
25
25
 
26
26
  def completed?
27
27
  !completed_at.nil?
@@ -47,7 +47,7 @@ class TableDisplayTest < ActiveSupport::TestCase
47
47
 
48
48
  test "#to_table_display is available on ActiveRecord find results" do # which should be arrays, in fact
49
49
  assert_nothing_raised do
50
- Task.find(:all).to_table_display
50
+ Task.all.to_table_display
51
51
  end
52
52
  end
53
53
 
@@ -76,36 +76,36 @@ class TableDisplayTest < ActiveSupport::TestCase
76
76
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
77
77
  | id | project_id | description | due_on | completed_at | created_at | updated_at |
78
78
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
79
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 23:17:05 +1300 | 2009-03-23 09:11:02 +1300 | 2009-03-24 23:17:05 +1300 |
80
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-23 09:11:46 +1300 | 2009-03-23 09:11:46 +1300 |
79
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 10:17:05 +0000 | 2009-03-22 20:11:02 +0000 | 2009-03-24 10:17:05 +0000 |
80
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-22 20:11:46 +0000 | 2009-03-22 20:11:46 +0000 |
81
81
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
82
82
  END
83
83
  end
84
84
 
85
85
  test "#to_table_display by default includes all the database columns in database order even when not called on a typeless array" do
86
- assert_equal <<END.strip, @project.tasks.find(:all).to_table_display.join("\n")
86
+ assert_equal <<END.strip, @project.tasks.all.to_table_display.join("\n")
87
87
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
88
88
  | id | project_id | description | due_on | completed_at | created_at | updated_at |
89
89
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
90
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 23:17:05 +1300 | 2009-03-23 09:11:02 +1300 | 2009-03-24 23:17:05 +1300 |
91
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-23 09:11:46 +1300 | 2009-03-23 09:11:46 +1300 |
90
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 10:17:05 +0000 | 2009-03-22 20:11:02 +0000 | 2009-03-24 10:17:05 +0000 |
91
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-22 20:11:46 +0000 | 2009-03-22 20:11:46 +0000 |
92
92
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+
93
93
  END
94
94
  end
95
95
 
96
96
  test "#to_table_display leaves out any attributes not loaded" do
97
- assert_equal <<END.strip, @project.tasks.find(:all, :select => "id, project_id, completed_at").to_table_display.join("\n")
97
+ assert_equal <<END.strip, @project.tasks.select("id, project_id, completed_at").to_table_display.join("\n")
98
98
  +----+------------+---------------------------+
99
99
  | id | project_id | completed_at |
100
100
  +----+------------+---------------------------+
101
- | 1 | 31 | 2009-03-24 23:17:05 +1300 |
101
+ | 1 | 31 | 2009-03-24 10:17:05 +0000 |
102
102
  | 2 | 31 | nil |
103
103
  +----+------------+---------------------------+
104
104
  END
105
105
  end
106
106
 
107
107
  test "#to_table_display also shows any attributes that are not columns on the underlying table" do
108
- assert_equal <<END.strip, @project.tasks.find(:all, :joins => :project, :select => "tasks.id, project_id, projects.description AS BigProjectDescription").to_table_display.join("\n")
108
+ assert_equal <<END.strip, @project.tasks.joins(:project).select("tasks.id, project_id, projects.description AS BigProjectDescription").to_table_display.join("\n")
109
109
  +----+------------+-----------------------------------------------------------------------------------------------------+
110
110
  | id | project_id | BigProjectDescription |
111
111
  +----+------------+-----------------------------------------------------------------------------------------------------+
@@ -120,8 +120,8 @@ END
120
120
  +----+------------+------------------------+------------------+---------------------------+
121
121
  | id | project_id | description | due_on | updated_at |
122
122
  +----+------------+------------------------+------------------+---------------------------+
123
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 23:17:05 +1300 |
124
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | 2009-03-23 09:11:46 +1300 |
123
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 10:17:05 +0000 |
124
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | 2009-03-22 20:11:46 +0000 |
125
125
  +----+------------+------------------------+------------------+---------------------------+
126
126
  END
127
127
  end
@@ -183,19 +183,56 @@ END
183
183
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+------------+------------------------+
184
184
  | id | project_id | description | due_on | completed_at | created_at | updated_at | completed? | project_name |
185
185
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+------------+------------------------+
186
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 23:17:05 +1300 | 2009-03-23 09:11:02 +1300 | 2009-03-24 23:17:05 +1300 | true | "table_display plugin" |
187
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-23 09:11:46 +1300 | 2009-03-23 09:11:46 +1300 | false | "table_display plugin" |
186
+ | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | 2009-03-24 10:17:05 +0000 | 2009-03-22 20:11:02 +0000 | 2009-03-24 10:17:05 +0000 | true | "table_display plugin" |
187
+ | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | 2009-03-22 20:11:46 +0000 | 2009-03-22 20:11:46 +0000 | false | "table_display plugin" |
188
188
  +----+------------+------------------------+------------------+---------------------------+---------------------------+---------------------------+------------+------------------------+
189
189
  END
190
190
  end
191
-
191
+
192
+ test "#to_table_display also shows any named callables given as columns" do
193
+ named_callable = Object.new.tap do |object|
194
+ object.define_singleton_method(:name) { :sample }
195
+ object.define_singleton_method(:call) { |record| "(id #{record.id})" }
196
+ end
197
+
198
+ assert_equal <<END.strip, @project.tasks.to_table_display('id', :due_on, :completed?, named_callable).join("\n")
199
+ +----+------------------+------------+----------+
200
+ | id | due_on | completed? | sample |
201
+ +----+------------------+------------+----------+
202
+ | 1 | Wed, 25 Mar 2009 | true | "(id 1)" |
203
+ | 2 | Sun, 05 Apr 2009 | false | "(id 2)" |
204
+ +----+------------------+------------+----------+
205
+ END
206
+ end
207
+
208
+ test "#to_table_display shows multiple callables given as columns in the order provided" do
209
+ callable = Object.new.tap do |object|
210
+ object.define_singleton_method(:call) { |record| "(id #{record.id})" }
211
+ object.define_singleton_method(:to_s) { "arbitrary to_s" }
212
+ end
213
+
214
+ instrument = Object.new.tap { |o| o.define_singleton_method(:creation) { |record| "(#{record.created_at.to_date})" } }
215
+
216
+ due_on_proc = -> (record) { record.due_on }
217
+ due_on_proc.define_singleton_method(:to_s) { "due_on_proc" }
218
+
219
+ assert_equal <<END.strip, @project.tasks.to_table_display('id', due_on_proc, instrument.method(:creation), callable).join("\n")
220
+ +----+------------------+----------------+----------------+
221
+ | id | due_on_proc | creation | arbitrary to_s |
222
+ +----+------------------+----------------+----------------+
223
+ | 1 | Wed, 25 Mar 2009 | "(2009-03-22)" | "(id 1)" |
224
+ | 2 | Sun, 05 Apr 2009 | "(2009-03-22)" | "(id 2)" |
225
+ +----+------------------+----------------+----------------+
226
+ END
227
+ end
228
+
192
229
  test "#to_table_display shows the #to_s format rather than the #inspect format when :inspect => false is set" do
193
230
  assert_equal <<END.strip, @project.tasks.to_table_display(:inspect => false).join("\n")
194
231
  +----+------------+----------------------+------------+---------------------------+---------------------------+---------------------------+
195
232
  | id | project_id | description | due_on | completed_at | created_at | updated_at |
196
233
  +----+------------+----------------------+------------+---------------------------+---------------------------+---------------------------+
197
- | 1 | 31 | Write a handy plugin | 2009-03-25 | 2009-03-24 23:17:05 +1300 | 2009-03-23 09:11:02 +1300 | 2009-03-24 23:17:05 +1300 |
198
- | 2 | 31 | Blog the plugin | 2009-04-05 | | 2009-03-23 09:11:46 +1300 | 2009-03-23 09:11:46 +1300 |
234
+ | 1 | 31 | Write a handy plugin | 2009-03-25 | 2009-03-24 10:17:05 +0000 | 2009-03-22 20:11:02 +0000 | 2009-03-24 10:17:05 +0000 |
235
+ | 2 | 31 | Blog the plugin | 2009-04-05 | | 2009-03-22 20:11:46 +0000 | 2009-03-22 20:11:46 +0000 |
199
236
  +----+------------+----------------------+------------+---------------------------+---------------------------+---------------------------+
200
237
  END
201
238
  # note the strings no longer have quotes, the nil is not shown, and the date format happens to be different
@@ -1,21 +1,12 @@
1
1
  require 'rubygems'
2
2
  require "../../../config/boot.rb" if File.exist?("../../../config/boot.rb")
3
3
 
4
- $KCODE = 'u' if RUBY_VERSION < '1.9' # as per railties initializer.rb: set unicode mode for Ruby 1.8 (Ruby 1.9 just works)
5
-
6
- require 'test/unit'
4
+ require 'minitest/autorun'
7
5
  require 'active_support'
8
6
  require 'active_support/test_case'
9
7
  require 'active_record'
10
8
  require 'active_record/fixtures'
11
9
 
12
- begin
13
- require 'ruby-debug'
14
- Debugger.start
15
- rescue LoadError
16
- # ruby-debug not installed, no debugging for you
17
- end
18
-
19
10
  ENV['RAILS_ENV'] ||= 'test'
20
11
  FileUtils.mkdir File.join(File.dirname(__FILE__), "log") rescue nil
21
12
  RAILS_DEFAULT_LOGGER = ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "log", "#{ENV['RAILS_ENV']}.log"))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_display
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-04 00:00:00.000000000 Z
11
+ date: 2018-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -90,7 +90,7 @@ files:
90
90
  - ".gitignore"
91
91
  - Gemfile
92
92
  - MIT-LICENSE
93
- - README
93
+ - README.md
94
94
  - Rakefile
95
95
  - init.rb
96
96
  - lib/table_display.rb
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  version: '0'
122
122
  requirements: []
123
123
  rubyforge_project:
124
- rubygems_version: 2.5.1
124
+ rubygems_version: 2.7.6
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: Adds support for displaying your ActiveRecord tables, named scopes, collections,
data/README DELETED
@@ -1,82 +0,0 @@
1
- Table Display
2
- =============
3
-
4
- Adds support for displaying your ActiveRecord tables, named scopes, collections, or
5
- plain arrays in a table view when working in rails console, shell, or email template.
6
-
7
- Enumerable#to_table_display returns the printable strings; Object#pt calls #to_table_display on its
8
- first argument and puts out the result.
9
-
10
- Columns you haven't loaded (eg. from using :select) are omitted, and derived/calculated
11
- columns (eg. again, from using :select) are added.
12
-
13
- Both #to_table_display and Object#pt methods take :only, :except, and :methods which work like
14
- the #to_xml method to change what attributes/methods are output.
15
-
16
- The normal output uses #inspect on the data values to make them printable, so you can
17
- see what type the values had. When that's inconvenient or you'd prefer direct display,
18
- you can pass the option :inspect => false to disable inspection.
19
-
20
-
21
- Example
22
- =======
23
-
24
- # You can call the to_table_display method:
25
- >> puts Project.find(31).tasks.to_table_display
26
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
27
- | id | project_id | description | due_on | completed_at | created_at | updated_at |
28
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
29
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | Tue Mar 24 23:17:05 +1300 2009 | Mon Mar 23 09:11:02 +1300 2009 | Tue Mar 24 23:17:05 +1300 2009 |
30
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | Mon Mar 23 09:11:46 +1300 2009 | Mon Mar 23 09:11:46 +1300 2009 |
31
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
32
-
33
- # Or equivalently, use "pt" (like pp, but in a table):
34
- >> pt Customer.find(31).purchases
35
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
36
- | id | project_id | description | due_on | completed_at | created_at | updated_at |
37
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
38
- | 1 | 31 | "Write a handy plugin" | Wed, 25 Mar 2009 | Tue Mar 24 23:17:05 +1300 2009 | Mon Mar 23 09:11:02 +1300 2009 | Tue Mar 24 23:17:05 +1300 2009 |
39
- | 2 | 31 | "Blog the plugin" | Sun, 05 Apr 2009 | nil | Mon Mar 23 09:11:46 +1300 2009 | Mon Mar 23 09:11:46 +1300 2009 |
40
- +----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+
41
-
42
-
43
- # Like to_xml, you can pass a :methods option to add the output methods on your models, and you can pass :only or :except
44
- # to (respectively) show only certain columns or show all except certain columns:
45
- >> puts Customer.find(31).purchases.to_table_display(:only => [:id, :description], :methods => [:met_due_date?])
46
- +----+------------------------+---------------+
47
- | id | description | met_due_date? |
48
- +----+------------------------+---------------+
49
- | 1 | "Write a handy plugin" | true |
50
- | 2 | "Blog the plugin" | nil |
51
- +----+------------------------+---------------+
52
-
53
- # pt accepts and passes on all options as well:
54
- >> pt Customer.find(31).purchases, :only => [:id, :description], :methods => [:met_due_date?]
55
- +----+------------------------+---------------+
56
- | id | description | met_due_date? |
57
- +----+------------------------+---------------+
58
- | 1 | "Write a handy plugin" | true |
59
- | 2 | "Blog the plugin" | nil |
60
- +----+------------------------+---------------+
61
-
62
- # There's a convenient equivalent syntax for displaying an ordered list of columns, like :only and :methods:
63
- >> puts Customer.find(31).purchases.to_table_display :id, :description, :met_due_date?
64
- # which provides:
65
- >> pt Customer.find(31).purchases, :id, :description, :met_due_date?
66
- # resulting in the same output as above.
67
-
68
-
69
- # If :inspect => false is used, the values will be shown in #to_s form rather than #inspect form:
70
- >> pt Customer.find(31).purchases, :only => [:id, :description, :due_on, :completed_at]
71
- +----+----------------------+------------+--------------------------------+
72
- | id | description | due_on | completed_at |
73
- +----+----------------------+------------+--------------------------------+
74
- | 1 | Write a handy plugin | 2009-03-25 | Tue Mar 24 23:17:05 +1300 2009 |
75
- | 2 | Blog the plugin | 2009-04-05 | |
76
- +----+----------------------+------------+--------------------------------+
77
-
78
-
79
- # Note that in all cases, values descending from Numeric are right-aligned, while all other values are left-aligned.
80
-
81
-
82
- Copyright (c) 2009 Will Bryant, Sekuda Ltd, released under the MIT license