lib/tablesmith/table.rb in tablesmith-0.4.1 vs lib/tablesmith/table.rb in tablesmith-0.5.0

- old
+ new

@@ -1,23 +1,29 @@ +# frozen_string_literal: true + require 'text-table' require 'csv' module Tablesmith class Table < Array def method_missing(meth_id, *args) count = 1 - self.map do |t| - $stderr.print '.' if count.divmod(100)[1] == 0 + map do |t| + $stderr.print '.' if (count.divmod(100)[1]).zero? count += 1 t.send(meth_id, *args) end end def respond_to_missing? super end + def to_s + text_table.to_s + end + # irb def inspect pretty_inspect end @@ -30,13 +36,13 @@ def pretty_print(pp) pp.text pretty_inspect end def text_table - return ['(empty)'].to_text_table if self.empty? + return ['(empty)'].to_text_table if empty? - rows = self.map { |item| convert_item_to_hash_row(item) }.compact + rows = map { |item| convert_item_to_hash_row(item) }.compact normalize_keys(rows) sort_columns(rows) @@ -46,16 +52,17 @@ def to_csv CSV.generate do |csv| text_table.rows.each do |row| next if row == :separator + csv << row.map do |cell| case cell - when Hash - cell[:value] - else - cell + when Hash + cell[:value] + else + cell end end end end end @@ -68,35 +75,36 @@ def row_values(row) row end # override in subclass or mixin - def sort_columns(rows) - end + def sort_columns(rows); end # override in subclass or mixin def convert_item_to_hash_row(item) item end # override in subclass or mixin - def normalize_keys(rows) - end + def normalize_keys(rows); end # override in subclass or mixin def column_order [] end - def columns - @columns - end + attr_reader :columns def create_headers(rows) - top_row = rows.first - column_names = top_row.first.is_a?(Array) ? top_row.map(&:first) : top_row - grouped_headers(column_names) + [apply_column_aliases(column_names), :separator] + first_element = rows.first + if first_element.is_a?(Array) + top_row = first_element + column_names = top_row.first.is_a?(Array) ? top_row.map(&:first) : top_row + grouped_headers(column_names) + [apply_column_aliases(column_names), :separator] + else + [] + end end def grouped_headers(column_names) groups = Hash.new { |h, k| h[k] = 0 } column_names.map! do |name| @@ -109,36 +117,36 @@ [] 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} + row << { value: name, align: :center, colspan: span } end [row, :separator] end end def apply_column_aliases(column_names) column_names.map do |name| instance = columns.detect { |ca| ca.name.to_s == name.to_s } value = instance ? instance.display_name : name - {:value => value, :align => :center} + { value: value, align: :center } end end end class Column attr_accessor :source, :name, :alias - def initialize(attributes={}) + def initialize(attributes = {}) @source = attributes.delete(:source) @name = attributes.delete(:name) @alias = attributes.delete(:alias) end def display_name - "#{@alias || @name}" + (@alias || @name).to_s end def full_unaliased_name "#{@source ? "#{@source}." : ''}#{@name}" end @@ -155,28 +163,22 @@ # TODO: redesign such that every row is reacted to appropriately, # so mixed content could be supported. Maybe every cell could be # rendered appropriately, with nested tables. if defined?(ActiveRecord) && defined?(ActiveRecord::Base) - if b.first && b.first.is_a?(ActiveRecord::Base) - b.extend Tablesmith::ActiveRecordSource - end + b.extend Tablesmith::ActiveRecordSource if b.first&.is_a?(ActiveRecord::Base) end - if b.first && b.first.is_a?(Hash) - b.extend Tablesmith::HashRowsSource - end + b.extend Tablesmith::HashRowsSource if b.first&.is_a?(Hash) - if b.first && b.first.is_a?(Array) - b.extend Tablesmith::ArrayRowsSource - end + b.extend Tablesmith::ArrayRowsSource if b.first&.is_a?(Array) b end end class Hash def to_table b = Tablesmith::Table.new([self]) b.extend Tablesmith::HashRowsSource end -end \ No newline at end of file +end