Sha256: 73f521b51be8332acfa9d6368afa3d4389a0bdecd2bf5e5931f37cac2c11cc12

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

class Tabled
  class ContentShaper
    attr_accessor :data, :options, :columns_width

    def initialize(data, columns_width, options)
      @data = data
      @columns_width = columns_width
      @options = options
    end

    def shape
      content = []

      unless options[:titles].empty?
        content << Tabled::Template::Titles.render(options[:titles], columns_width, options[:framed])
        content << (options[:row_separator].to_s * row_length) unless options[:row_separator].nil?
      end

      content.concat(
        data
        .each_with_object([]) do |row, enumerator|
          enumerator << Tabled::Template::Row.render(row, columns_width, options[:framed])
          enumerator << Tabled::Template::RowFooter.render(row, columns_width, options[:framed])

          #  Row separator
          enumerator << (options[:row_separator].to_s * row_length) unless options[:row_separator].nil?
        end
        .compact
      )

      content = add_left_and_right_borders(content)
      add_top_bottom_borders(content)
    end

    private

    def row_length
      @row_length ||= columns_width.sum
    end

    def add_left_and_right_borders(content)
      return content unless options[:framed]

      content.each_with_object([]) do |row, enumerator|
        # For a row separator all symbols are the same
        row_is_separator = row.chars.uniq.size == 1

        enumerator << if row_is_separator && !options[:row_separator].nil?
                        (options[:row_separator] * 2) + row + options[:row_separator]
                      else
                        "| #{row}|"
                      end
      end
    end

    def add_top_bottom_borders(content)
      #  Top and bottom borders
      if options[:framed]
        [
          '-' * (row_length + 3),
          options[:row_separator] ? content[0..-2] : content,
          '-' * (row_length + 3)
        ].flatten
      else
        content
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tabled-1.2.1 lib/content_shaper.rb
tabled-1.2.0 lib/content_shaper.rb
tabled-1.1.0 lib/content_shaper.rb
tabled-1.0.0 lib/content_shaper.rb
tabled-0.0.6 lib/content_shaper.rb
tabled-0.0.5 lib/content_shaper.rb
tabled-0.0.4 lib/content_shaper.rb