Sha256: b9c2779c5a364932eff583bc4e826e1bb87c261310602049bd0a2914ee258ef4

Contents?: true

Size: 1.63 KB

Versions: 24

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module FatTable
  # A subclass of Formatter for rendering the table as a Ruby Array of Hashes.
  # Each row of the Array is a Hash representing one row of the table with the
  # keys being the symbolic form of the headers. Each cell is a value in a row
  # Hash formatted as a string in accordance with the formatting directives. All
  # footers are included as extra Hashes of the output. AoaFormatter supports no
  # +options+
  class AohFormatter < Formatter
    private

    def evaluate?
      true
    end

    def pre_table
      '['
    end

    def post_table
      ']'
    end

    # We include no row for the header because the keys of each hash serve as
    # the headers.
    def include_header_row?
      false
    end

    def pre_row
      '{'
    end

    def pre_cell(head)
      ":#{head.as_sym} => '"
    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(val)
      if val.match?(/'/)
        # Use a negative look-behind to only quote single-quotes that are not
        # already preceded by a backslash
        val.gsub(/(?<!\\)'/, "'" => "\\'")
      else
        val
      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

24 entries across 24 versions & 1 rubygems

Version Path
fat_table-0.9.7 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.9.5 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.9.3 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.9.2 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.9.1 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.9.0 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.8.0 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.7.0 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.6 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.4 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.3 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.2 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.1 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.6.0 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.5.5 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.5.4 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.5.3 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.5.2 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.5.1 lib/fat_table/formatters/aoh_formatter.rb
fat_table-0.4.2 lib/fat_table/formatters/aoh_formatter.rb