Class: Termtable::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/termtable/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Cell

Returns a new instance of Cell.



9
10
11
# File 'lib/termtable/cell.rb', line 9

def initialize(value)
  @value = value.to_s
end

Instance Attribute Details

#alignmentObject

Returns the value of attribute alignment.



4
5
6
# File 'lib/termtable/cell.rb', line 4

def alignment
  @alignment
end

#max_column_sizeObject

Returns the value of attribute max_column_size.



4
5
6
# File 'lib/termtable/cell.rb', line 4

def max_column_size
  @max_column_size
end

#paddingObject

Returns the value of attribute padding.



4
5
6
# File 'lib/termtable/cell.rb', line 4

def padding
  @padding
end

#total_paddingObject

Returns the value of attribute total_padding.



4
5
6
# File 'lib/termtable/cell.rb', line 4

def total_padding
  @total_padding
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/termtable/cell.rb', line 3

def value
  @value
end

Instance Method Details

#expand?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/termtable/cell.rb', line 13

def expand?
  max_column_size != value.to_s.size + total_padding
end

#left_paddingObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/termtable/cell.rb', line 17

def left_padding
  count = case alignment
          when 'right'
            expand? ? max_column_size - value.to_s.size : padding
          when 'center'
            expand? ? (max_column_size - value.to_s.size) / 2 : padding
          else
            0
          end
  ' ' * count
end

#renderObject



47
48
49
# File 'lib/termtable/cell.rb', line 47

def render
  "#{left_padding}#{value}#{right_padding}"
end

#right_paddingObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/termtable/cell.rb', line 29

def right_padding
  count = case alignment
          when 'right'
            0
          when 'center'
            if expand? && max_column_size.even?
              (max_column_size - value.to_s.size) / 2
            elsif expand? && max_column_size.odd?
              (max_column_size - value.to_s.size) / 2 + 1
            else
              padding
            end
          else
            expand? ? max_column_size - value.to_s.size : padding
          end
  ' ' * count
end