Sha256: fa45bf6cc3d24d7d4ba411afa59fa259c9b6f1f8e2be845c618ed669193aea84

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module FatTable
  # A subclass of Formatter for rendering the table as a Ruby Array of Arrays.
  # Each cell is formatted as a string in accordance with the formatting
  # directives. All footers are included as extra Arrays of the output.
  # AoaFormatter supports no +options+
  class AoaFormatter < Formatter

    private

    def evaluate?
      true
    end

    def pre_table
      '['
    end

    def post_table
      ']'
    end

    def pre_header(_widths)
      ''
    end

    def post_header(_widths)
      ''
    end

    def pre_row
      '['
    end

    def pre_cell(_h)
      "'"
    end

    # Because the cell, after conversion to a single-quoted string will be
    # eval'ed, we need to escape any single-quotes (') that appear in the
    # string.
    def quote_cell(v)
      if v =~ /'/
        # Use a negative look-behind to only quote single-quotes that are not
        # already preceded by a backslash
        v.gsub(/(?<!\\)'/, "'" => "\\'")
      else
        v
      end
    end

    def post_cell
      "'"
    end

    def inter_cell
      ','
    end

    def post_row
      "],\n"
    end

    def hline(_widths)
      "nil,\n"
    end

    def pre_group
      ''
    end

    def post_group
      ''
    end

    def pre_gfoot
      ''
    end

    def post_gfoot
      ''
    end

    def pre_foot
      ''
    end

    def post_foot
      ''
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fat_table-0.2.6 lib/fat_table/formatters/aoa_formatter.rb
fat_table-0.2.4 lib/fat_table/formatters/aoa_formatter.rb