Class: TablePal::Cell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row:, column:, content: '', formatter: nil, justification: nil, colour: nil) ⇒ Cell

Returns a new instance of Cell.



8
9
10
11
12
13
14
15
# File 'lib/cell.rb', line 8

def initialize(row:, column:, content: '', formatter: nil, justification: nil, colour: nil)
  @row           = row
  @column        = column
  @content       = content
  @formatter     = formatter || row.formatter || NoFormatting
  @justification = justification || row.justification || column.justification
  @colour        = colour || row.colour || column.colour || :itself
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



6
7
8
# File 'lib/cell.rb', line 6

def colour
  @colour
end

#columnObject (readonly)

Returns the value of attribute column.



6
7
8
# File 'lib/cell.rb', line 6

def column
  @column
end

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/cell.rb', line 6

def content
  @content
end

#formatterObject (readonly)

Returns the value of attribute formatter.



6
7
8
# File 'lib/cell.rb', line 6

def formatter
  @formatter
end

#justificationObject (readonly)

Returns the value of attribute justification.



6
7
8
# File 'lib/cell.rb', line 6

def justification
  @justification
end

#rowObject (readonly)

Returns the value of attribute row.



6
7
8
# File 'lib/cell.rb', line 6

def row
  @row
end

Instance Method Details

#colourize(result) ⇒ Object



35
36
37
# File 'lib/cell.rb', line 35

def colourize(result)
  result.send(colour)
end

#format(result) ⇒ Object



25
26
27
28
29
# File 'lib/cell.rb', line 25

def format(result)
  return '' if result == ''

  formatter.call(result).to_s
end

#formatted(justified: false, coloured: false) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cell.rb', line 17

def formatted(justified: false, coloured: false)
  result = format(content)
  result = justify(result) if justified
  result = colourize(result) if coloured

  result
end

#justification_to_methodObject



43
44
45
46
47
48
49
# File 'lib/cell.rb', line 43

def justification_to_method
  {
    left:   :ljust,
    right:  :rjust,
    center: :center
  }[justification]
end

#justify(result) ⇒ Object



31
32
33
# File 'lib/cell.rb', line 31

def justify(result)
  result.send(justification_to_method, column.width)
end

#widthObject



39
40
41
# File 'lib/cell.rb', line 39

def width
  formatted.length
end