Class: Text::Table::Cell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cell

:nodoc:


19
20
21
22
23
24
# File 'lib/text-table/cell.rb', line 19

def initialize(options = {}) #:nodoc:
  @value  = options[:value].to_s
  @row     = options[:row]
  @align   = options[:align  ] || :left
  @colspan = options[:colspan] || 1
end

Instance Attribute Details

#alignObject

Text alignment. Acceptable values are :left (default), :center and :right


12
13
14
# File 'lib/text-table/cell.rb', line 12

def align
  @align
end

#colspanObject

Positive integer specifying the number of columns spanned


16
17
18
# File 'lib/text-table/cell.rb', line 16

def colspan
  @colspan
end

#rowObject (readonly)

:nodoc:


17
18
19
# File 'lib/text-table/cell.rb', line 17

def row
  @row
end

#valueObject

The object whose to_s method is called when rendering the cell.


7
8
9
# File 'lib/text-table/cell.rb', line 7

def value
  @value
end

Instance Method Details

#cell_widthObject

:nodoc:


45
46
47
# File 'lib/text-table/cell.rb', line 45

def cell_width #:nodoc:
  (0...colspan).map {|i| table.column_widths[column_index + i]}.inject(&:+) + (colspan - 1)*(2*table.horizontal_padding + table.horizontal_boundary.length)
end

#column_indexObject

:nodoc:


41
42
43
# File 'lib/text-table/cell.rb', line 41

def column_index #:nodoc:
  row.cells[0...row.cells.index(self)].map(&:colspan).inject(0, &:+)
end

#tableObject

:nodoc:


37
38
39
# File 'lib/text-table/cell.rb', line 37

def table #:nodoc:
  row.table
end

#to_sObject

:nodoc:


26
27
28
29
30
31
32
33
34
35
# File 'lib/text-table/cell.rb', line 26

def to_s #:nodoc:
([' ' * table.horizontal_padding]*2).join case align
  when :left
    value.ljust cell_width
  when :right
    value.rjust cell_width
  when :center
    value.center cell_width
  end
end