class TTY::Table::Header
A set of header elements that correspond to values in each row
Attributes
The header attributes
@return [Array]
@api private
The header attributes
@return [Array]
@api private
Public Class Methods
Initialize a Header
@return [undefined]
@api public
# File lib/tty/table/header.rb, line 39 def initialize(attributes = []) @attributes = attributes.map { |attr| to_field(attr) } @attribute_for = Hash[@attributes.each_with_index.map.to_a] end
Public Instance Methods
Check if this header is equivalent to another header
@return [Boolean]
@api public
# File lib/tty/table/header.rb, line 155 def ==(other) to_a == other.to_a end
Lookup a column in the header given a name
@param [Integer, String] attribute
the attribute to look up by
@api public
# File lib/tty/table/header.rb, line 75 def [](attribute) case attribute when Integer @attributes[attribute].value else @attribute_for.fetch(to_field(attribute)) do |header_name| raise UnknownAttributeError, "the header '#{header_name.value}' is unknown" end end end
Set value at index
@example
header[attribute] = value
@api public
# File lib/tty/table/header.rb, line 100 def []=(attribute, value) attributes[attribute] = to_field(value) end
Lookup attribute without evaluation
@api public
# File lib/tty/table/header.rb, line 90 def call(attribute) @attributes[attribute] end
Iterate over each element in the vector
@example
header = TTY::Table::Header.new [1,2,3] header.each { |element| ... }
@return [self]
@api public
# File lib/tty/table/header.rb, line 53 def each return to_enum unless block_given? to_ary.each { |element| yield element } self end
Check if there are no elements.
@return [Boolean]
@api public
# File lib/tty/table/header.rb, line 146 def empty? to_ary.empty? end
Find maximum header height
@return [Integer]
@api public
# File lib/tty/table/header.rb, line 119 def height attributes.map { |field| field.height }.max end
# File lib/tty/table/header.rb, line 167 def inspect "#<#{self.class.name} fields=#{to_a}>" end
Size of the header
@return [Integer]
@api public
# File lib/tty/table/header.rb, line 109 def size to_ary.size end
Return the header elements in an array.
@return [Array]
@api public
# File lib/tty/table/header.rb, line 137 def to_a to_ary.dup end
Convert the Header
into an Array
@return [Array]
@api public
# File lib/tty/table/header.rb, line 128 def to_ary attributes.map { |attr| attr.value if attr } end
Instantiates a new field
@param [String,Hash] attribute
the attribute value to convert to field object
@api public
# File lib/tty/table/header.rb, line 65 def to_field(attribute = nil) Field.new(attribute) end
Provide an unique hash value
@api public
# File lib/tty/table/header.rb, line 163 def to_hash to_a.hash end