lib/cell.rb in table_pal-0.3.1 vs lib/cell.rb in table_pal-0.3.2

- old
+ new

@@ -1,10 +1,16 @@ module TablePal class Cell using Colours + JUSTIFICATIONS = { + left: :ljust, + right: :rjust, + center: :center + }.freeze + attr_reader :row, :column, :content, :formatter, :justification, :colour def initialize(row:, column:, content: '', formatter: nil, justification: nil, colour: nil) @row = row @column = column @@ -14,11 +20,11 @@ @colour = colour || row.colour || column.colour || :itself end def formatted(justified: false, coloured: false) result = format(content) - result = justify(result) if justified + result = justify(result) if justified result = colourize(result) if coloured result end @@ -27,26 +33,18 @@ formatter.call(result).to_s end def justify(result) - result.send(justification_to_method, column.width) + result.send(JUSTIFICATIONS[justification], column.width) end def colourize(result) result.send(colour) end def width - formatted.length - end - - def justification_to_method - { - left: :ljust, - right: :rjust, - center: :center - }[justification] + @width ||= formatted.length end end end