Class: TablePal::Cell
- Inherits:
-
Object
- Object
- TablePal::Cell
- Defined in:
- lib/cell.rb
Constant Summary collapse
- JUSTIFICATIONS =
{ left: :ljust, right: :rjust, center: :center }.freeze
Instance Attribute Summary collapse
-
#colour ⇒ Object
readonly
Returns the value of attribute colour.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#justification ⇒ Object
readonly
Returns the value of attribute justification.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
Instance Method Summary collapse
- #colourize(result) ⇒ Object
- #format(result) ⇒ Object
- #formatted(justified: false, coloured: false) ⇒ Object
-
#initialize(row:, column:, content: '', formatter: nil, justification: nil, colour: nil) ⇒ Cell
constructor
A new instance of Cell.
- #justify(result) ⇒ Object
- #width ⇒ Object
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
#colour ⇒ Object (readonly)
Returns the value of attribute colour.
12 13 14 |
# File 'lib/cell.rb', line 12 def colour @colour end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
12 13 14 |
# File 'lib/cell.rb', line 12 def column @column end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
12 13 14 |
# File 'lib/cell.rb', line 12 def content @content end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
12 13 14 |
# File 'lib/cell.rb', line 12 def formatter @formatter end |
#justification ⇒ Object (readonly)
Returns the value of attribute justification.
12 13 14 |
# File 'lib/cell.rb', line 12 def justification @justification end |
#row ⇒ Object (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 |
#width ⇒ Object
45 46 47 |
# File 'lib/cell.rb', line 45 def width @width ||= formatted.length end |