module TablePal class Cell using Colours attr_reader :row, :column, :content, :formatter, :justification, :colour 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 def formatted(justified: false, coloured: false) result = format(content) result = justify(result) if justified result = colourize(result) if coloured result end def format(result) return '' if result == '' formatter.call(result).to_s end def justify(result) result.send(justification_to_method, 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] end end end