table_display 2.1.0 → 3.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3013953f835a9c3948a8197eeebb9ffe9a53e8fb2ad14471a6802155205c03d
4
- data.tar.gz: b569caed7b3c6b65327c6391b3fd63b68b0dd815b6068160fdfafed811fe76ed
3
+ metadata.gz: f2dcb76090971646e3996055028e3187823e4cf17d0fce5c9d90e5f4cc1cc034
4
+ data.tar.gz: 2ec5ea3c4d25ee8fc4034f6ed169c89cd0579cd2f45789195bffdc3bdac2c3a6
5
5
  SHA512:
6
- metadata.gz: 3592665702f00ad8602e9a32a3c6e51fb8af848c302d2bd9220221cf116d315de2c3bbd705556266e3c4685055d1039f62f21a1479a2d22568aa6d1caabd0aec
7
- data.tar.gz: f42df4ce0723ec6bdd8d4c18b323ca05b48c8c0579b6119edecb0117d41408ab1b856f19e5d4a7af44b3bbcb45ea74bb8c4cf71f7c156361c031f7a1553338e9
6
+ metadata.gz: e7a7e98fbb462c031501deafd35b8604dd1fcb72c41aa5958d8ac7b966ffacfa00e17cfb7afd64c003274697b1e66d74b6fc705c947678000380758c6972d7c3
7
+ data.tar.gz: 3d6fda3edeb0dcc3714e0d40a630e2743c0749c71625eb0e6b925fbf987cbf4955faf1c3a39d6cb7cecb4db1e1bd05792c0e8de34ccbccc3e1a88d93eca483fa
data/README.md CHANGED
@@ -15,7 +15,7 @@ change what attributes/methods are output, like they do on the `#to_xml` method.
15
15
 
16
16
  The normal output uses `#inspect` on the data values to make them printable, so you can
17
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.
18
+ you can pass the option `inspect: false` to disable inspection.
19
19
 
20
20
 
21
21
  Example
@@ -45,7 +45,7 @@ Or equivalently, use `pt` (like `pp`, but in a table):
45
45
  Like `to_xml`, you can pass a `:methods` option to add the output methods on your models, and you
46
46
  can pass `:only` or `:except` to (respectively) show only certain columns or show all except certain columns:
47
47
 
48
- >> puts Customer.find(31).purchases.to_table_display(:only => [:id, :description], :methods => [:met_due_date?])
48
+ >> puts Customer.find(31).purchases.to_table_display(only: [:id, :description], methods: [:met_due_date?])
49
49
  +----+------------------------+---------------+
50
50
  | id | description | met_due_date? |
51
51
  +----+------------------------+---------------+
@@ -55,7 +55,7 @@ can pass `:only` or `:except` to (respectively) show only certain columns or sho
55
55
 
56
56
  `pt` accepts and passes on all options as well:
57
57
 
58
- >> pt Customer.find(31).purchases, :only => [:id, :description], :methods => [:met_due_date?]
58
+ >> pt Customer.find(31).purchases, only: [:id, :description], methods: [:met_due_date?]
59
59
  +----+------------------------+---------------+
60
60
  | id | description | met_due_date? |
61
61
  +----+------------------------+---------------+
@@ -74,9 +74,9 @@ which provides:
74
74
  resulting in the same output as above.
75
75
 
76
76
 
77
- If `:inspect => false` is used, the values will be shown in `#to_s` form rather than `#inspect` form:
77
+ If `inspect: false` is used, the values will be shown in `#to_s` form rather than `#inspect` form:
78
78
 
79
- >> pt Customer.find(31).purchases, :only => [:id, :description, :due_on, :completed_at]
79
+ >> pt Customer.find(31).purchases, only: [:id, :description, :due_on, :completed_at]
80
80
  +----+----------------------+------------+--------------------------------+
81
81
  | id | description | due_on | completed_at |
82
82
  +----+----------------------+------------+--------------------------------+
@@ -1,3 +1,3 @@
1
1
  module TableDisplay
2
- VERSION = '2.1.0'
2
+ VERSION = '3.0.0'
3
3
  end
data/lib/table_display.rb CHANGED
@@ -1,4 +1,4 @@
1
- module TableDisplay
1
+ module Enumerable
2
2
  def to_table_display(*args)
3
3
  options = args.last.is_a?(Hash) ? args.pop : {}
4
4
  extra_entries = args.collect { |arg| arg.respond_to?(:call) ? arg : arg.to_s }
@@ -6,11 +6,11 @@ module TableDisplay
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]
9
- except_attributes = (except_attributes + except_attributes.collect(&:to_s)).uniq if except_attributes.present? # we have to keep string and symbol arguments separate for hashes, which may not have 'indifferent access'
9
+ except_attributes = (except_attributes + except_attributes.collect(&:to_s)).uniq if except_attributes && !except_attributes.empty? # we have to keep string and symbol arguments separate for hashes, which may not have 'indifferent access'
10
10
  display_inspect = options.nil? || !options.has_key?(:inspect) || options.delete(:inspect)
11
- raise "unknown options passed to to_table_display: #{options.keys.to_sentence}" unless options.blank?
11
+ raise "unknown options passed to to_table_display: #{options.keys.to_sentence}" unless options.empty?
12
12
 
13
- column_lengths = ActiveSupport::OrderedHash.new
13
+ column_lengths = {}
14
14
 
15
15
  if only_attributes
16
16
  # we've been given an explicit list of attributes to display
@@ -39,7 +39,7 @@ module TableDisplay
39
39
 
40
40
  if attribute_names.any? {|name| column_lengths[name].nil?} # optimisation, in most use cases all records will have the same type and the same attributes, so we needn't run this for each - but we do handle varying attribute lists, and attributes that are not columns on the model (calculated columns etc.)
41
41
  # for ActiveRecord classes, we look at the .columns explicitly so we can keep them in the right order
42
- columns_to_check = record.is_a?(ActiveRecord::Base) ? ((record.class.columns.collect(&:name) & attribute_names) + attribute_names).uniq : attribute_names
42
+ columns_to_check = Module.const_defined?(:ActiveRecord) && record.is_a?(ActiveRecord::Base) ? ((record.class.columns.collect(&:name) & attribute_names) + attribute_names).uniq : attribute_names
43
43
  columns_to_check.each do |name|
44
44
  next if (only_attributes && !only_attributes.include?(name)) || (except_attributes && except_attributes.include?(name))
45
45
  column_lengths[name] = 0 # the values of columns are the maximum width of value seen; when we come to print out, if the max seen is zero then the attribute has never actually been seen (eg. when a find(:all, :select => ...) has been used to exclude some of the database columns from the resultset), and we hide the column.
@@ -62,7 +62,7 @@ module TableDisplay
62
62
  record.send(attribute)
63
63
  end
64
64
  string_value = display_inspect ? value.inspect : (value.is_a?(String) ? value : value.to_s)
65
- column_lengths[attribute] = string_value.mb_chars.length if string_value.mb_chars.length > max_width
65
+ column_lengths[attribute] = string_value.length if string_value.length > max_width
66
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
67
67
  end
68
68
  end
@@ -70,15 +70,15 @@ module TableDisplay
70
70
  return [] if data.empty?
71
71
 
72
72
  # build the table header
73
- separator_string = "+"
74
- heading_string = "|"
73
+ separator_string = "+".dup
74
+ heading_string = "|".dup
75
75
  column_lengths.each do |attribute, max_width|
76
76
  next unless max_width > 0 # skip any columns we never actually saw
77
77
  name = (attribute.respond_to?(:name) ? attribute.name : attribute).to_s
78
78
 
79
79
  # the column needs to fit the column header as well as the values
80
- if name.mb_chars.length > max_width
81
- column_lengths[attribute] = max_width = name.mb_chars.length
80
+ if name.length > max_width
81
+ column_lengths[attribute] = max_width = name.length
82
82
  end
83
83
 
84
84
  separator_string << '-'*(max_width + 2) << '+'
@@ -87,14 +87,14 @@ module TableDisplay
87
87
 
88
88
  rows = [separator_string, heading_string, separator_string]
89
89
  data.each do |data_row|
90
- data_string = "|"
90
+ data_string = "|".dup
91
91
  column_lengths.each_with_index do |(_attribute, max_width), index|
92
92
  next unless max_width > 0 # skip any columns we never actually saw
93
93
  value = data_row[index]
94
94
  if value.is_a?(Numeric)
95
- data_string << ' ' << (display_inspect ? value.inspect : value.to_s).mb_chars.rjust(max_width) << ' |'
95
+ data_string << ' ' << (display_inspect ? value.inspect : value.to_s).rjust(max_width) << ' |'
96
96
  else
97
- data_string << ' ' << value.mb_chars.ljust(max_width) << ' |'
97
+ data_string << ' ' << value.ljust(max_width) << ' |'
98
98
  end
99
99
  end
100
100
  rows << data_string
@@ -108,12 +108,3 @@ module Kernel
108
108
  puts target.respond_to?(:to_table_display) ? target.to_table_display(*options) : target
109
109
  end
110
110
  end
111
-
112
- # all Enumerable classes should get TableDisplay functionality...
113
- Enumerable.send(:include, TableDisplay)
114
-
115
- # including those that have already included Enumerable by the time this plugin is loaded.
116
- # Ruby doesn't recursively update through the module tree, so although any new classes/modules
117
- # that include Enumerable will get TableDisplay, we have to do it ourself for older ones.
118
- ObjectSpace.each_object(Module) {|o| o.send(:include, TableDisplay) if o.ancestors.include?(Enumerable)}
119
- ObjectSpace.each_object(Class) {|o| o.send(:include, TableDisplay) if o.ancestors.include?(Enumerable)}
@@ -22,7 +22,6 @@ The normal output uses #inspect on the data values to make them printable, so yo
22
22
  see what type the values had. When that's inconvenient or you'd prefer direct display,
23
23
  you can pass the option :inspect => false to disable inspection.
24
24
  EOF
25
- gem.has_rdoc = false
26
25
  gem.author = "Will Bryant"
27
26
  gem.email = "will.bryant@gmail.com"
28
27
  gem.homepage = "http://github.com/willbryant/table_display"
@@ -36,4 +35,5 @@ EOF
36
35
  gem.add_development_dependency "sqlite3"
37
36
  gem.add_development_dependency "activerecord"
38
37
  gem.add_development_dependency "test-unit"
38
+ gem.add_development_dependency "ostruct"
39
39
  end
@@ -4,6 +4,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'schema'))
4
4
  require 'ostruct'
5
5
 
6
6
  class Time
7
+ undef :to_s
8
+ undef :inspect
9
+
7
10
  def to_s(*args)
8
11
  return to_formatted_s(*args) unless args.empty?
9
12
  strftime("%Y-%m-%d %H:%M:%S %z")
data/test/test_helper.rb CHANGED
@@ -12,8 +12,8 @@ FileUtils.mkdir File.join(File.dirname(__FILE__), "log") rescue nil
12
12
  RAILS_DEFAULT_LOGGER = ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "log", "#{ENV['RAILS_ENV']}.log"))
13
13
 
14
14
  ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(File.dirname(__FILE__), "database.yml")))
15
- ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[ENV['RAILS_ENV']]
15
+ ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations.find_db_config(ENV['RAILS_ENV'])
16
16
  ActiveSupport::TestCase.send(:include, ActiveRecord::TestFixtures) if ActiveRecord.const_defined?('TestFixtures')
17
- ActiveSupport::TestCase.fixture_path = File.join(File.dirname(__FILE__), "fixtures")
17
+ ActiveSupport::TestCase.fixture_paths = [File.join(File.dirname(__FILE__), "fixtures")]
18
18
 
19
19
  require File.expand_path(File.join(File.dirname(__FILE__), '../init')) # load table_display
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_display
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Bryant
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2018-10-17 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake
@@ -66,6 +65,20 @@ dependencies:
66
65
  - - ">="
67
66
  - !ruby/object:Gem::Version
68
67
  version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: ostruct
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
69
82
  description: |
70
83
  Adds support for displaying your ActiveRecord tables, named scopes, collections, or
71
84
  plain arrays in a table view when working in rails console, shell, or email template.
@@ -105,7 +118,6 @@ files:
105
118
  homepage: http://github.com/willbryant/table_display
106
119
  licenses: []
107
120
  metadata: {}
108
- post_install_message:
109
121
  rdoc_options: []
110
122
  require_paths:
111
123
  - lib
@@ -120,9 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
132
  - !ruby/object:Gem::Version
121
133
  version: '0'
122
134
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.7.6
125
- signing_key:
135
+ rubygems_version: 3.6.9
126
136
  specification_version: 4
127
137
  summary: Adds support for displaying your ActiveRecord tables, named scopes, collections,
128
138
  or plain arrays in a table view when working in script/console, shell, or email