class Tabular::Row

Associate list of cells. Each Table has a list of Rows. Access Row cells via symbols. Ex: row

Attributes

index[RW]
source[R]

Public Class Methods

new(table, cells = [], source = nil) click to toggle source

tableTable cells – array (not neccessarily Strings) source – original data before mapped to Hash or Array (optional)

# File lib/tabular/row.rb, line 18
def initialize(table, cells = [], source = nil)
  @table = table
  @source = source || cells

  if cells.respond_to?(:keys)
    @array = cells.values
    @hash = {}
    cells.each do |key, value|
      @hash[key_to_sym(key)] = value
    end
  else
    @array = cells
    @hash = nil
  end

  @index = table.rows.size
end

Public Instance Methods

[](key) click to toggle source

Cell value by symbol. E.g., row

# File lib/tabular/row.rb, line 37
def [](key)
  hash[key]
end
[]=(key, value) click to toggle source

Set cell value. Adds cell to end of Row and adds new Column if there is no Column for +key_

# File lib/tabular/row.rb, line 42
def []=(key, value)
  if columns.key?(key)
    @array[columns.index(key)] = value
  else
    @array << value
    columns << key
  end
  hash[key] = value
end
blank?() click to toggle source
# File lib/tabular/row.rb, line 97
def blank?
  @array.all? { |cell| is_blank?(cell) }
end
columns() click to toggle source

Tabluar::Columns

# File lib/tabular/row.rb, line 102
def columns
  @table.columns
end
delete(key) click to toggle source
# File lib/tabular/row.rb, line 77
def delete(key)
  @array.delete key
  hash.delete key
end
each(&block) click to toggle source

Call block for each cell

# File lib/tabular/row.rb, line 53
def each(&block)
  @array.each(&block)
end
each_key(&block) click to toggle source

Call block for each key

# File lib/tabular/row.rb, line 58
def each_key(&block)
  keys.each(&block)
end
each_with_key(&block) click to toggle source

Call block for each cell

# File lib/tabular/row.rb, line 63
def each_with_key(&block)
  hash.each(&block)
end
inspect() click to toggle source
# File lib/tabular/row.rb, line 130
def inspect
  hash.inspect
end
join(sep = nil) click to toggle source

For pretty-printing cell values

# File lib/tabular/row.rb, line 73
def join(sep = nil)
  @array.join(sep)
end
keys() click to toggle source

Keys for all columns

# File lib/tabular/row.rb, line 68
def keys
  hash.keys
end
last?() click to toggle source

Is this the last row?

# File lib/tabular/row.rb, line 93
def last?
  index == @table.rows.size - 1
end
metadata() click to toggle source
# File lib/tabular/row.rb, line 112
def metadata
  @table.metadata
end
next() click to toggle source

Next Row

# File lib/tabular/row.rb, line 88
def next
  @table.rows[index + 1]
end
previous() click to toggle source

Previous Row

# File lib/tabular/row.rb, line 83
def previous
  @table.rows[index - 1] if index.positive?
end
render(key) click to toggle source

By default, return self. Customize by setting Table#renderer or Column#renderers

# File lib/tabular/row.rb, line 107
def render(key)
  column = columns[key]
  column.renderer.render column, self
end
to_hash() click to toggle source
# File lib/tabular/row.rb, line 116
def to_hash
  hash.dup
end
to_s() click to toggle source
# File lib/tabular/row.rb, line 134
def to_s
  @array.join(", ").to_s
end
to_space_delimited() click to toggle source
# File lib/tabular/row.rb, line 120
def to_space_delimited
  cells = []

  hash.each_key do |key|
    cells << (render(key) || "").ljust(columns[key].width)
  end

  cells.join "   "
end

Private Instance Methods

add_century_to(year) click to toggle source
# File lib/tabular/row.rb, line 206
def add_century_to(year)
  if year >= 0 && year < 69
    2000 + year
  elsif year > 69 && year < 100
    1900 + year
  elsif year < 1900 || year > 2050
    nil
  else
    year
  end
end
date?(value) click to toggle source
# File lib/tabular/row.rb, line 174
def date?(value)
  value.is_a?(Date) || value.is_a?(DateTime) || value.is_a?(Time)
end
parse_date(value, key, index) click to toggle source
# File lib/tabular/row.rb, line 178
def parse_date(value, key, index)
  return nil unless value

  begin
    Date.parse(value.to_s, true)
  rescue ArgumentError
    date = parse_invalid_date(value)
    date || raise(ArgumentError, "'#{key}' index #{index} #{value}' is not a valid date")
  end
end
parse_invalid_date(value) click to toggle source

Handle common m/d/yy case that Date.parse dislikes

# File lib/tabular/row.rb, line 190
def parse_invalid_date(value)
  return unless value

  parts = value.to_s.split("/")
  return unless parts.size == 3

  month = parts[0].to_i
  day = parts[1].to_i
  year = parts[2].to_i
  return unless month >= 1 && month <= 12 && day >= 1 && day <= 31

  year = add_century_to(year)

  Date.new(year, month, day)
end