Class: TTY::Table::Header

Inherits:
Vector
  • Object
show all
Extended by:
Forwardable
Includes:
Equatable
Defined in:
lib/tty/table/header.rb

Overview

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

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Instance Method Summary collapse

Methods included from Equatable

#attr_reader, included, #inherited

Methods inherited from Vector

[], #each, #empty?, #to_a

Methods included from Conversion

#convert_to_array

Constructor Details

#initialize(attributes = []) ⇒ undefined

Initialize a Header



37
38
39
40
# File 'lib/tty/table/header.rb', line 37

def initialize(attributes=[])
  @attributes    = attributes.map { |attr| to_field(attr) }
  @attribute_for = Hash[@attributes.each_with_index.map.to_a]
end

Instance Attribute Details

#attributesArray (readonly)

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.

The header attributes

Returns:

  • (Array)


30
31
32
# File 'lib/tty/table/header.rb', line 30

def attributes
  @attributes
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check if this header is equivalent to another header

Returns:

  • (Boolean)


101
102
103
# File 'lib/tty/table/header.rb', line 101

def ==(other)
  to_a == other.to_a
end

#[](attribute) ⇒ Object Also known as: call

Lookup a column in the header given a name



59
60
61
62
63
64
65
66
67
68
# File 'lib/tty/table/header.rb', line 59

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) ⇒ Object

Set value at index

Examples:

header[attribute] = value


77
78
79
# File 'lib/tty/table/header.rb', line 77

def []=(attribute, value)
  self.attributes[attribute] = to_field(value)
end

#filterObject

Filter header names including styling background, text color and capitalization



53
54
# File 'lib/tty/table/header.rb', line 53

def filter
end

#sizeObject Also known as: length

Size of the header



84
85
86
# File 'lib/tty/table/header.rb', line 84

def size
  to_ary.size
end

#to_aryObject

Convert the Header into an Array



92
93
94
# File 'lib/tty/table/header.rb', line 92

def to_ary
  attributes.map { |attr| attr.value if attr.value }
end

#to_field(options = nil) ⇒ Object

Instantiates a new field



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

def to_field(options=nil)
  Field.new(options)
end

#to_hashObject

Provide an unique hash value



109
110
111
# File 'lib/tty/table/header.rb', line 109

def to_hash
  to_a.hash
end