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

column_mapper[R]
row_mapper[RW]

Public Class Methods

new(rows = []) click to toggle source

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
read(file, options = {}) click to toggle source
# 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

<<(row) click to toggle source

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
[](index) click to toggle source

Return Row at zero-based index, or nil if Row is out of bounds

# File lib/tabular/table.rb, line 42
def [](index)
  rows[index]
end
column_mapper=(mapper) click to toggle source
# File lib/tabular/table.rb, line 140
def column_mapper=(mapper)
  @columns = nil if rows.nil? || rows.empty?
  @column_mapper = mapper
end
columns() click to toggle source

Instance of Tabular::Columns

# File lib/tabular/table.rb, line 74
def columns
  @columns ||= Tabular::Columns.new(self, [], column_mapper)
end
delete_blank_columns!(*options) click to toggle source

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
delete_blank_rows!() click to toggle source
# 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
delete_column(key) click to toggle source
# File lib/tabular/table.rb, line 123
def delete_column(key)
  rows.each do |row|
    row.delete key
  end
  columns.delete key
end
delete_homogenous_columns!(*options) click to toggle source

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
inspect() click to toggle source
# File lib/tabular/table.rb, line 69
def inspect
  rows.map { |row| row.join(",") }.join("\n")
end
metadata() click to toggle source

Last-resort storage for client code data

# File lib/tabular/table.rb, line 146
def metadata
  @metadata ||= {}
end
renderer=(value) click to toggle source

Set default Renderer. If present, will be used for all cells and Column headers.

# File lib/tabular/table.rb, line 131
def renderer=(value)
  columns.renderer = value
end
renderers() click to toggle source

List of Renderers

# File lib/tabular/table.rb, line 136
def renderers
  columns.renderers
end
rows() click to toggle source
# File lib/tabular/table.rb, line 28
def rows
  @rows ||= []
end
rows=(source_rows = []) click to toggle source

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
strip!() click to toggle source

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
to_s() click to toggle source
# File lib/tabular/table.rb, line 154
def to_s
  "#<#{self.class} #{rows.size}>"
end
to_space_delimited() click to toggle source
# File lib/tabular/table.rb, line 150
def to_space_delimited
  ([columns] + rows).map(&:to_space_delimited).join("\n") << "\n"
end

Private Instance Methods

extract_exceptions(options) click to toggle source
# File lib/tabular/table.rb, line 160
def extract_exceptions(options)
  if options.first && options.first[:except]
    options.first[:except]
  else
    []
  end
end
strip_decimal(value) click to toggle source
# 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