class Tabular::Column

Attributes

column_type[R]
key[R]

Public Class Methods

new(table, columns, key = nil) click to toggle source

table – parent Table column – parent Columns key should be a normalized, downcase, underscored symbol

# File lib/tabular/column.rb, line 10
def initialize(table, columns, key = nil)
  @columns = columns
  @table = table
  @key = self.columns.column_mapper.map(key)

  @column_type = if @key && @key.to_s["date"]
                   :date
                 elsif @key && @key.to_s[/\?\z/]
                   :boolean
                 else
                   :string
                 end
end

Public Instance Methods

cells() click to toggle source

All cells value under this Column

# File lib/tabular/column.rb, line 29
def cells
  rows.map { |r| r[key] }
end
inspect() click to toggle source
# File lib/tabular/column.rb, line 62
def inspect
  "#<Tabular::Column #{key} #{column_type}>"
end
max() click to toggle source

Maximum value for cells in the Column. Determine with Ruby max

# File lib/tabular/column.rb, line 34
def max
  cells.compact.max
end
precision() click to toggle source

Number of zeros to the right of the decimal point. Useful for formtting time data.

# File lib/tabular/column.rb, line 39
def precision
  @precision ||= cells.map(&:to_f).map { |n| n.round(3) }.map { |n| n.to_s.split(".").last.gsub(/0+$/, "").length }.max
end
render() click to toggle source

Human-friendly header string. Delegate to renderer's render_header method.

# File lib/tabular/column.rb, line 49
def render
  renderer.render_header self
end
renderer() click to toggle source

Renderer

# File lib/tabular/column.rb, line 54
def renderer
  @columns.renderer(key)
end
rows() click to toggle source
# File lib/tabular/column.rb, line 24
def rows
  @table.rows
end
to_s() click to toggle source
# File lib/tabular/column.rb, line 66
def to_s
  key.to_s
end
to_space_delimited() click to toggle source
# File lib/tabular/column.rb, line 58
def to_space_delimited
  to_s.ljust width
end
width() click to toggle source

Widest string in column

# File lib/tabular/column.rb, line 44
def width
  @width ||= (cells.map(&:to_s) << to_s).map(&:size).max
end

Protected Instance Methods

columns() click to toggle source
# File lib/tabular/column.rb, line 72
def columns
  @columns ||= Columns.new
end