lib/tablesmith/table.rb in tablesmith-0.5.0 vs lib/tablesmith/table.rb in tablesmith-0.6.0
- old
+ new
@@ -2,21 +2,34 @@
require 'text-table'
require 'csv'
module Tablesmith
- class Table < Array
+ class Table < Tablesmith.delegated_array_class
+ def initialize(array = [])
+ super(array)
+ @array = array
+ end
+
def method_missing(meth_id, *args)
- count = 1
- map do |t|
- $stderr.print '.' if (count.divmod(100)[1]).zero?
- count += 1
- t.send(meth_id, *args)
- end
+ # In order to support `Kernel::puts` of a `Table`, we need to ignore
+ # `to_ary` calls here as well. See comments on `delegated_array_class`.
+ #
+ # While `DelegatorClass(Array)` proactively defines methods on `Table`
+ # that come from `Array`, it _also_ will pass calls through method_missing
+ # to the target object if it says it will respond to it.
+ #
+ # It seems a little redundant, but it is what it is, and so we must also
+ # cut off calls to `to_ary` in both places.
+ return nil if meth_id.to_sym == :to_ary
+
+ super
end
- def respond_to_missing?
+ def respond_to_missing?(meth_id, _include_all)
+ return false if meth_id.to_sym == :to_ary
+
super
end
def to_s
text_table.to_s
@@ -116,12 +129,10 @@
if groups.keys.length == 1 # TODO: add option to show group header row when only one exists
[]
else
row = []
# this relies on Ruby versions where hash retains add order
- groups.each_pair do |name, span|
- row << { value: name, align: :center, colspan: span }
- end
+ groups.each { |name, span| row << {value: name, align: :center, colspan: span} }
[row, :separator]
end
end
def apply_column_aliases(column_names)