Class: Clin::Text::TableRow

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/clin/text/table.rb

Overview

Table Row container class

Direct Known Subclasses

TableSeparatorRow

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, cells) ⇒ TableRow

Returns a new instance of TableRow.



161
162
163
164
# File 'lib/clin/text/table.rb', line 161

def initialize(table, cells)
  @table = table
  @cells = cells.flatten.each_with_index.map { |x, i| TableCell.new(table, i, x) }
end

Instance Attribute Details

#cellsObject

List of cells in the row.



159
160
161
# File 'lib/clin/text/table.rb', line 159

def cells
  @cells
end

Instance Method Details

#border(text, separator = '') ⇒ Object



172
173
174
175
# File 'lib/clin/text/table.rb', line 172

def border(text, separator = '')
  return text unless @table.border?
  @table.vertical_border + separator + text + separator + @table.vertical_border
end

#delimiter_at(index) ⇒ Object

Get the delimiter to insert after the cell at index Will return the corresponding delimiter and for the last cell will return ”

Parameters:

  • index (Integer)

    Cell index.



180
181
182
183
184
185
186
# File 'lib/clin/text/table.rb', line 180

def delimiter_at(index)
  delim = @table.delimiter_at(index)
  if !@table.separate_blank? && (@cells[index].blank? || @cells[index + 1].blank?)
    delim = ' ' * delim.size
  end
  delim
end

#each(&block) ⇒ Object



166
167
168
169
170
# File 'lib/clin/text/table.rb', line 166

def each(&block)
  @table.column_length.size.times.each do |i|
    block.call(@cells[i] || '')
  end
end

#to_sObject



188
189
190
191
192
193
194
195
# File 'lib/clin/text/table.rb', line 188

def to_s
  out = ''
  each_with_index do |cell, i|
    out << cell.to_s
    out << delimiter_at(i)
  end
  border(out, ' ')
end