Class: TTY::Table::AlignmentSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tty/table/alignment_set.rb

Overview

A class responsible for column alignments

Used internally by Operation::Alignment

Constant Summary collapse

DEFAULT =
:left

Instance Method Summary collapse

Constructor Details

#initialize(alignments) ⇒ AlignmentSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize an AlignmentSet

Parameters:

  • alignments (AlignmentSet, Array)

    the alignments for the renderer



19
20
21
# File 'lib/tty/table/alignment_set.rb', line 19

def initialize(alignments)
  @alignments = Array(alignments).map(&:to_sym)
end

Instance Method Details

#[](index) ⇒ Symbol

Lookup an alignment by index

Parameters:

  • index (Integer)

Returns:

  • (Symbol)

    alignment



45
46
47
# File 'lib/tty/table/alignment_set.rb', line 45

def [](index)
  alignments.fetch(index, DEFAULT)
end

#eachself

Iterate over each element in the alignment set

Examples:

alignment = AlignmentSet.new [1,2,3]
alignment.each { |element| ... }

Returns:

  • (self)


32
33
34
35
36
# File 'lib/tty/table/alignment_set.rb', line 32

def each
  return to_enum unless block_given?
  to_ary.each { |element| yield element }
  self
end

#to_aryArray

Convert to array

Returns:

  • (Array)


54
55
56
# File 'lib/tty/table/alignment_set.rb', line 54

def to_ary
  alignments.to_a
end

#to_sString

String representation of aligments

Returns:

  • (String)


63
64
65
# File 'lib/tty/table/alignment_set.rb', line 63

def to_s
  to_ary
end