Class: Clin::Text::TableCell

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

Overview

Table cell container class

Instance Method Summary collapse

Constructor Details

#initialize(table, index, value) ⇒ TableCell

Returns a new instance of TableCell.



226
227
228
229
230
231
# File 'lib/clin/text/table.rb', line 226

def initialize(table, index, value)
  @table = table
  @index = index
  @value = value.to_s
  @table.update_column_length(index, @value.length)
end

Instance Method Details

#alignSymbol

Get the alignment for this cell Can be:

  • :left

  • :center

  • :right

Returns:

  • (Symbol)


249
250
251
252
253
# File 'lib/clin/text/table.rb', line 249

def align
  return @table.align if @table.align.is_a? Symbol
  fail Clin::Error, 'Align must either be a symbol or an Array!' unless @table.align.is_a? Array
  @table.align[@index]
end

#blank?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/clin/text/table.rb', line 239

def blank?
  @value.blank?
end

#lengthInteger

Get the length of the cell

Returns:

  • (Integer)


235
236
237
# File 'lib/clin/text/table.rb', line 235

def length
  @table.column_length[@index]
end

#to_sString

Convert the cell to_s using the length of the column and the column alignment

Returns:

  • (String)


257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/clin/text/table.rb', line 257

def to_s
  case align
  when :left
    @value.to_s.ljust(length)
  when :right
    @value.to_s.rjust(length)
  when :center
    @value.to_s.center(length)
  else
    fail Clin::Error, "Invalid align #{align}"
  end
end