class TTY::Table::Header

A set of header elements that correspond to values in each row

Attributes

attributes[R]

The header attributes

@return [Array]

@api private

fields[R]

The header attributes

@return [Array]

@api private

Public Class Methods

new(attributes = []) click to toggle source

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

==(other) click to toggle source

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
Also aliased as: eql?
[](attribute) click to toggle source

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
[]=(attribute, value) click to toggle source

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
call(attribute) click to toggle source

Lookup attribute without evaluation

@api public

# File lib/tty/table/header.rb, line 90
def call(attribute)
  @attributes[attribute]
end
each() { |element| ... } click to toggle source

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
empty?() click to toggle source

Check if there are no elements.

@return [Boolean]

@api public

# File lib/tty/table/header.rb, line 146
def empty?
  to_ary.empty?
end
eql?(other)
Alias for: ==
height() click to toggle source

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
inspect() click to toggle source
# File lib/tty/table/header.rb, line 167
def inspect
  "#<#{self.class.name} fields=#{to_a}>"
end
length()
Alias for: size
size() click to toggle source

Size of the header

@return [Integer]

@api public

# File lib/tty/table/header.rb, line 109
def size
  to_ary.size
end
Also aliased as: length
to_a() click to toggle source

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
to_ary() click to toggle source

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
to_field(attribute = nil) click to toggle source

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
to_hash() click to toggle source

Provide an unique hash value

@api public

# File lib/tty/table/header.rb, line 163
def to_hash
  to_a.hash
end