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 to values in each row

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Vector

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

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


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

def attributes
  @attributes
end

Instance Method Details

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

Check if this header is equivalent to another header

Returns:

  • (Boolean)


120
121
122
# File 'lib/tty/table/header.rb', line 120

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

#[](attribute) ⇒ Object

Lookup a column in the header given a name

Parameters:

  • attribute (Integer, String)

    the attribute to look up by



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

def [](attribute)
  case attribute
  when Integer
    @attributes[attribute].value
  else
    @attribute_for.fetch(to_field(attribute)) do |header_name|
      fail UnknownAttributeError,
           "the header '#{header_name.value}' is unknown"
    end
  end
end

#[]=(attribute, value) ⇒ Object

Set value at index

Examples:

header[attribute] = value


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

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

#call(attribute) ⇒ Object

Lookup attribute without evaluation



73
74
75
# File 'lib/tty/table/header.rb', line 73

def call(attribute)
  @attributes[attribute]
end

#heightInteger

Find maximum header height

Returns:

  • (Integer)


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

def height
  attributes.map { |field| field.height }.max
end

#sizeInteger Also known as: length

Size of the header

Returns:

  • (Integer)


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

def size
  to_ary.size
end

#to_aryArray

Convert the Header into an Array

Returns:

  • (Array)


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

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

#to_field(attribute = nil) ⇒ Object

Instantiates a new field

Parameters:

  • attribute (String, Hash) (defaults to: nil)

    the attribute value to convert to field object



48
49
50
# File 'lib/tty/table/header.rb', line 48

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

#to_hashObject

Provide an unique hash value



128
129
130
# File 'lib/tty/table/header.rb', line 128

def to_hash
  to_a.hash
end