Class: TTY::Table::Header

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
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

Constructor Details

#initialize(attributes = []) ⇒ undefined

Initialize a Header



39
40
41
42
# 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

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)


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

def attributes
  @attributes
end

Instance Method Details

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

Check if this header is equivalent to another header

Returns:

  • (Boolean)


155
156
157
# File 'lib/tty/table/header.rb', line 155

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



75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

Set value at index

Examples:

header[attribute] = value


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

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

#call(attribute) ⇒ Object

Lookup attribute without evaluation



90
91
92
# File 'lib/tty/table/header.rb', line 90

def call(attribute)
  @attributes[attribute]
end

#eachself

Iterate over each element in the vector

Examples:

header = TTY::Table::Header.new [1,2,3]
header.each { |element| ... }

Returns:

  • (self)


53
54
55
56
57
# 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?Boolean

Check if there are no elements.

Returns:

  • (Boolean)


146
147
148
# File 'lib/tty/table/header.rb', line 146

def empty?
  to_ary.empty?
end

#heightInteger

Find maximum header height

Returns:

  • (Integer)


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

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

#inspectObject



167
168
169
# File 'lib/tty/table/header.rb', line 167

def inspect
  "#<#{self.class.name} fields=#{to_a}>"
end

#sizeInteger Also known as: length

Size of the header

Returns:

  • (Integer)


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

def size
  to_ary.size
end

#to_aArray

Return the header elements in an array.

Returns:

  • (Array)


137
138
139
# File 'lib/tty/table/header.rb', line 137

def to_a
  to_ary.dup
end

#to_aryArray

Convert the Header into an Array

Returns:

  • (Array)


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

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



65
66
67
# File 'lib/tty/table/header.rb', line 65

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

#to_hashObject

Provide an unique hash value



163
164
165
# File 'lib/tty/table/header.rb', line 163

def to_hash
  to_a.hash
end