class Tabular::Columns

The Table's header: a list of Columns.

Attributes

column_mapper[W]
renderer[W]

Public Class Methods

new(table = Table.new, names = [], column_mapper = nil) click to toggle source
# File lib/tabular/columns.rb, line 20
def initialize(table = Table.new, names = [], column_mapper = nil)
  @table = table
  @renderer = nil
  self.column_mapper = column_mapper

  @column_indexes = {}
  @columns_by_key = {}

  set_columns table, names
end

Public Instance Methods

<<(key) click to toggle source

Add a new Column with key

# File lib/tabular/columns.rb, line 78
def <<(key)
  column = Column.new(@table, self, key)
  return if is_blank?(column.key) || key?(key)

  @column_indexes[column.key] = @columns.size
  @column_indexes[@columns.size] = column
  @columns_by_key[column.key] = column
  @columns << column
end
[](key) click to toggle source

Column for key

# File lib/tabular/columns.rb, line 63
def [](key)
  @columns_by_key[key_to_sym(key)]
end
column_mapper() click to toggle source

tableTable data – array of header names

# File lib/tabular/columns.rb, line 16
def column_mapper
  @column_mapper ||= Tabular::ColumnMapper.new
end
delete(key) click to toggle source
# File lib/tabular/columns.rb, line 88
def delete(key)
  @columns.delete_if { |column| column.key == key }
  @columns_by_key.delete key
  @column_indexes.delete key

  @columns.each.with_index do |column, index|
    @column_indexes[column.key] = index
  end
end
each(&block) click to toggle source

Call block for each Column

# File lib/tabular/columns.rb, line 73
def each(&block)
  @columns.each(&block)
end
empty?() click to toggle source
# File lib/tabular/columns.rb, line 47
def empty?
  size.zero?
end
has_key?(key) click to toggle source

Deprecated

# File lib/tabular/columns.rb, line 52
def has_key?(key) # rubocop:disable Naming/PredicateName
  key? key
end
index(key) click to toggle source

Zero-based index of Column for key

# File lib/tabular/columns.rb, line 68
def index(key)
  @column_indexes[key]
end
key?(key) click to toggle source

Is the a Column with this key? Keys are lower-case, underscore symbols. Example: :postal_code

# File lib/tabular/columns.rb, line 58
def key?(key)
  @columns.any? { |column| column.key == key }
end
renderer(key) click to toggle source

Renderer for Column key. Default to Table#Renderer.

# File lib/tabular/columns.rb, line 104
def renderer(key)
  renderers[key] || @renderer || Renderer
end
renderers() click to toggle source

List of Renderers

# File lib/tabular/columns.rb, line 109
def renderers
  @renderers ||= {}
end
set_columns(table = Table.new, names = []) click to toggle source
# File lib/tabular/columns.rb, line 31
def set_columns(table = Table.new, names = [])
  index = 0

  names = names.keys if names.respond_to?(:keys)

  @columns = names.map do |name|
    new_column = Tabular::Column.new(table, self, name)
    unless is_blank?(new_column.key)
      @column_indexes[new_column.key] = index
      @columns_by_key[new_column.key] = new_column
    end
    index += 1
    new_column
  end
end
size() click to toggle source

Count of Columns#columns

# File lib/tabular/columns.rb, line 99
def size
  @columns.size
end
to_space_delimited() click to toggle source
# File lib/tabular/columns.rb, line 113
def to_space_delimited
  map(&:to_space_delimited).join "   "
end