Class: PostRunner::FlexiTable::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/postrunner/FlexiTable.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, row, content, attributes) ⇒ Cell

Returns a new instance of Cell.



48
49
50
51
52
53
54
55
56
# 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

Instance Method Details

#min_terminal_widthObject



58
59
60
# File 'lib/postrunner/FlexiTable.rb', line 58

def min_terminal_width
  @content.to_s.length
end

#set_indicies(col_idx, row_idx) ⇒ Object



62
63
64
65
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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_sObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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