Sha256: 14659af43a6d2c0bbae5ebadbb619f9f5433b5b310b34a28e53962cf2a917f44

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

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    || (raise "Row must be supplied for cell with content: '#{content}' to exist in")
      @column        = column || (raise "Column must be supplied for cell with content: '#{content}' to exist in")
      @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(JUSTIFICATIONS[justification], column.width)
    end

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

    def width
      @width ||= formatted.length
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_pal-0.3.3 lib/cell.rb