class Tabular::Table
Simple Enumerable list of Hashes. Use Table.read(file_path)
to read file. Can also create a Table
with Table.new
. Either pass in data or set options and then call row=.
Attributes
Public Class Methods
Pass data in as rows
. Expects rows to be an Enumerable of Enumerables. Maps rows to Hash-like Tabular::Rows.
# File lib/tabular/table.rb, line 23 def initialize(rows = []) @columns = nil self.rows = rows end
# File lib/tabular/table.rb, line 15 def self.read(file, options = {}) table = Table.new table.read file, options[:as], options[:sheet] table end
Public Instance Methods
Add row to end of table. Create missing columns and link the source row to Row#source
. To control how source data is added to the Table
, use Table#mapper= to set a class that implements map(row) and returns a Hash.
# File lib/tabular/table.rb, line 49 def <<(row) cells = if row_mapper row_mapper.map(row) else row end if @columns.nil? || @columns.empty? @columns = Tabular::Columns.new(self, cells, column_mapper) return columns unless cells.respond_to?(:keys) end new_row = Tabular::Row.new(self, cells, row) new_row.each_key do |key| columns << key end rows << new_row new_row end
# File lib/tabular/table.rb, line 140 def column_mapper=(mapper) @columns = nil if rows.nil? || rows.empty? @column_mapper = mapper end
Instance of Tabular::Columns
# File lib/tabular/table.rb, line 74 def columns @columns ||= Tabular::Columns.new(self, [], column_mapper) end
Remove all columns that only contain a blank string, zero, or nil
# File lib/tabular/table.rb, line 79 def delete_blank_columns!(*options) exceptions = extract_exceptions(options) (columns.map(&:key) - exceptions).each do |key| if rows.all? { |row| is_blank?(row[key]) || is_zero?(row[key]) } # rubocop:disable Style/IfUnlessModifier delete_column key end end end
# File lib/tabular/table.rb, line 101 def delete_blank_rows! @rows = rows.reject(&:blank?) rows.each.with_index do |row, index| row.index = index end end
# File lib/tabular/table.rb, line 123 def delete_column(key) rows.each do |row| row.delete key end columns.delete key end
Remove all columns that contain the same value in all rows
# File lib/tabular/table.rb, line 90 def delete_homogenous_columns!(*options) return if rows.size < 2 exceptions = extract_exceptions(options) (columns.map(&:key) - exceptions).each do |key| value = rows.first[key] delete_column key if rows.all? { |row| row[key] == value } end end
# File lib/tabular/table.rb, line 69 def inspect rows.map { |row| row.join(",") }.join("\n") end
Last-resort storage for client code data
# File lib/tabular/table.rb, line 146 def metadata @metadata ||= {} end
List of Renderers
# File lib/tabular/table.rb, line 136 def renderers columns.renderers end
# File lib/tabular/table.rb, line 28 def rows @rows ||= [] end
Set table rows. Calls row <<, which creates columns and links the source rows to Row#source
.
# File lib/tabular/table.rb, line 33 def rows=(source_rows = []) return unless source_rows source_rows.each do |row| self.<< row end end
Remove preceding and trailing whitespace from all cells. By default, Table
does not strip whitespace from cells.
# File lib/tabular/table.rb, line 110 def strip! rows.each do |row| columns.each do |column| value = row[column.key] if value.respond_to?(:strip) row[column.key] = value.strip elsif value.is_a?(Float) row[column.key] = strip_decimal(value) end end end end
# File lib/tabular/table.rb, line 154 def to_s "#<#{self.class} #{rows.size}>" end
# File lib/tabular/table.rb, line 150 def to_space_delimited ([columns] + rows).map(&:to_space_delimited).join("\n") << "\n" end
Private Instance Methods
# File lib/tabular/table.rb, line 160 def extract_exceptions(options) if options.first && options.first[:except] options.first[:except] else [] end end
# File lib/tabular/table.rb, line 168 def strip_decimal(value) if value && value.to_i == value.to_f value.to_i else value end end