class TablePrint::Row

Attributes

cells[R]

Public Class Methods

new() click to toggle source
Calls superclass method TablePrint::RowRecursion.new
# File lib/table_print/row_group.rb, line 136
def initialize
  super
  @cells = {}
end

Public Instance Methods

absorb_children(column_names, rollup) click to toggle source
# File lib/table_print/row_group.rb, line 186
def absorb_children(column_names, rollup)
  @children.each do |group|
    next unless can_absorb?(group)
    group.skip_first_row!

    column_names.collect do |name|
      next unless group.children and group.children.length > 0
      value = group.children.first.cells[name]
      rollup[name] = value if value
    end
  end
end
apply_formatters(column_name, value) click to toggle source
# File lib/table_print/row_group.rb, line 211
def apply_formatters(column_name, value)
  column_name = column_name.to_s
  return value unless column_for(column_name)

  column = column_for(column_name)
  formatters = []
  formatters.concat(Array(column.formatters))

  formatters << TimeFormatter.new(column.time_format)
  formatters << NoNewlineFormatter.new
  formatters << FixedWidthFormatter.new(column_for(column_name).width)

  # successively apply the formatters for a column
  formatters.inject(value) do |value, formatter|
    formatter.format(value)
  end
end
can_absorb?(group) click to toggle source
# File lib/table_print/row_group.rb, line 229
def can_absorb?(group)
  return true if group.child_count == 1

  return false if @already_absorbed_a_multigroup
  @already_absorbed_a_multigroup = true # only call this method once
end
collapse!() click to toggle source
# File lib/table_print/row_group.rb, line 147
def collapse!
  children.each(&:collapse!)  # depth-first. start collapsing from the bottom and work our way up.

  to_absorb = []
  children.each do |group|
    next unless can_absorb?(group)
    to_absorb << group
  end

  to_absorb.each do |absorbable_group|
    absorbable_row = absorbable_group.children.shift

    # missing associations create groups with no rows
    children.delete(absorbable_group) and next unless absorbable_row

    @cells.merge!(absorbable_row.cells)

    i = children.index(absorbable_group)
    children.delete(absorbable_group) if absorbable_group.children.empty?
    insert_children(i, absorbable_row.children) if absorbable_row.children.any?
  end
end
format() click to toggle source
# File lib/table_print/row_group.rb, line 177
def format
  column_names = columns.collect(&:name)

  output = [column_names.collect { |name| apply_formatters(name, @cells[name]) }.join(" #{TablePrint::Config.separator} ")]
  output.concat @children.collect { |g| g.format }

  output.join("\n")
end
raw_column_data(column_name) click to toggle source
# File lib/table_print/row_group.rb, line 199
def raw_column_data(column_name)
  output = [@cells[column_name.to_s]]
  output << @children.collect { |g| g.raw_column_data(column_name) }
  output.flatten
end
raw_column_names() click to toggle source
# File lib/table_print/row_group.rb, line 205
def raw_column_names
  output = [@cells.keys]
  output << @children.collect { |g| g.raw_column_names }
  output.flatten.uniq
end
set_cell_values(values_hash) click to toggle source
# File lib/table_print/row_group.rb, line 170
def set_cell_values(values_hash)
  values_hash.each do |k, v|
    @cells[k.to_s] = v
  end
  self
end
vis(prefix="") click to toggle source

this is a development tool, to show the structure of the row/row_group tree

# File lib/table_print/row_group.rb, line 142
def vis(prefix="")
  puts "#{prefix}row #{cells.inspect.to_s}"
  children.each{|c| c.vis(prefix + "  ")}
end