Class: TablePal::Cell

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

Constant Summary collapse

JUSTIFICATIONS =
{
  left:   :ljust,
  right:  :rjust,
  center: :center
}.freeze

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.



14
15
16
17
18
19
20
21
# File 'lib/cell.rb', line 14

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.



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

def colour
  @colour
end

#columnObject (readonly)

Returns the value of attribute column.



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

def column
  @column
end

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#formatterObject (readonly)

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#justificationObject (readonly)

Returns the value of attribute justification.



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

def justification
  @justification
end

#rowObject (readonly)

Returns the value of attribute row.



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

def row
  @row
end

Instance Method Details

#colourize(result) ⇒ Object



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

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

#format(result) ⇒ Object



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

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

  formatter.call(result).to_s
end

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



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

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

  result
end

#justify(result) ⇒ Object



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

def justify(result)
  result.send(JUSTIFICATIONS[justification], column.width)
end

#widthObject



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

def width
  @width ||= formatted.length
end