class PostRunner::FlexiTable::Cell

Public Class Methods

new(table, row, content, attributes) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 48
def initialize(table, row, content, attributes)
  @table = table
  @row = row
  @content = content
  @attributes = attributes

  @column_index = nil
  @row_index = nil
end

Public Instance Methods

min_terminal_width() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 58
def min_terminal_width
  @content.to_s.length
end
set_indicies(col_idx, row_idx) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 62
def set_indicies(col_idx, row_idx)
  @column_index = col_idx
  @row_index = row_idx
end
to_html(doc) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 85
def to_html(doc)
  text_align = get_attribute(:halign)
  attrs = { :class => 'ft_cell' }
  width = get_attribute(:width)
  attrs[:width] = width if width
  attrs[:style] = "text-align: #{text_align.to_s}" if text_align
  if @content.respond_to?('to_html')
    doc.td(attrs) {
      @content.to_html(doc)
    }
  else
    doc.td(@content.to_s, attrs)
  end
end
to_s() click to toggle source
# File lib/postrunner/FlexiTable.rb, line 67
def to_s
  s = @content.to_s
  width = get_attribute(:min_terminal_width)
  case get_attribute(:halign)
  when :left, nil
    s + ' ' * (width - s.length)
  when :right
    ' ' * (width - s.length) + s
  when :center
    w = width - s.length
    left_padding = w / 2
    right_padding = w / 2 + w % 2
    ' ' * left_padding + s + ' ' * right_padding
  else
    raise "Unknown alignment"
  end
end

Private Instance Methods

get_attribute(name) click to toggle source
# File lib/postrunner/FlexiTable.rb, line 102
def get_attribute(name)
  @attributes[name] ||
    @row.attributes[name] ||
    (@table.column_attributes[@column_index] ?
     @table.column_attributes[@column_index][name] : nil)
end