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



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

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) Also known as: fields

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)


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

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

#[](attribute) ⇒ Object

Lookup a column in the header given a name



53
54
55
56
57
58
59
60
61
62
# File 'lib/tty/table/header.rb', line 53

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

#call(attribute) ⇒ Object

Lookup attribute without evaluation



67
68
69
# File 'lib/tty/table/header.rb', line 67

def call(attribute)
  @attributes[attribute]
end

#heightInteger

Find maximum header height

Returns:

  • (Integer)


94
95
96
# File 'lib/tty/table/header.rb', line 94

def height
  attributes.map { |field| field.height }.max
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



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

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

#to_field(options = nil) ⇒ Object

Instantiates a new field



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

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

#to_hashObject

Provide an unique hash value



118
119
120
# File 'lib/tty/table/header.rb', line 118

def to_hash
  to_a.hash
end