Sha256: d2c95db5b243f59920add7088e224e5697798ef6505a2f492e898497db1d9044

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

# encoding: utf-8

require 'necromancer'

module TTY
  class Table
    # A class responsible for column alignments
    #
    # Used internally by {TTY::Table::Operation::Alignment}
    class AlignmentSet
      include Enumerable

      DEFAULT = :left

      # Initialize an AlignmentSet
      #
      # @param [AlignmentSet, Array] alignments
      #   the alignments for the renderer
      #
      # @api private
      def initialize(alignments)
        @alignments = Necromancer.convert(alignments).to(:array).map(&:to_sym)
      end

      # Iterate over each element in the alignment set
      #
      # @example
      #   alignment = AlignmentSet.new [1,2,3]
      #   alignment.each { |element| ... }
      #
      # @return [self]
      #
      # @api public
      def each
        return to_enum unless block_given?
        to_ary.each { |element| yield element }
        self
      end

      # Lookup an alignment by index
      #
      # @param [Integer]
      #
      # @return [Symbol] alignment
      #
      # @api public
      def [](index)
        alignments.fetch(index, DEFAULT)
      end

      # Convert to array
      #
      # @return [Array]
      #
      # @api public
      def to_ary
        alignments.to_a
      end

      # String representation of aligments
      #
      # @return [String]
      #
      # @api public
      def to_s
        to_ary
      end

      protected

      attr_reader :alignments
    end # AlignmentSet
  end # Table
end # TTY

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tty-table-0.10.0 lib/tty/table/alignment_set.rb
tty-table-0.9.0 lib/tty/table/alignment_set.rb
tty-table-0.8.0 lib/tty/table/alignment_set.rb