Class: TTY::Table::Header
- 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
-
#attributes ⇒ Array
(also: #fields)
readonly
private
The header attributes.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this header is equivalent to another header.
-
#[](attribute) ⇒ Object
Lookup a column in the header given a name.
-
#[]=(attribute, value) ⇒ Object
Set value at index.
-
#call(attribute) ⇒ Object
Lookup attribute without evaluation.
-
#height ⇒ Integer
Find maximum header height.
-
#initialize(attributes = []) ⇒ undefined
constructor
Initialize a Header.
-
#size ⇒ Integer
(also: #length)
Size of the header.
-
#to_ary ⇒ Array
Convert the Header into an Array.
-
#to_field(attribute = nil) ⇒ Object
Instantiates a new field.
-
#to_hash ⇒ Object
Provide an unique hash value.
Methods inherited from Vector
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
#attributes ⇒ Array (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
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
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
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
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 |
#height ⇒ Integer
Find maximum header height
102 103 104 |
# File 'lib/tty/table/header.rb', line 102 def height attributes.map { |field| field.height }.max end |
#size ⇒ Integer Also known as: length
Size of the header
92 93 94 |
# File 'lib/tty/table/header.rb', line 92 def size to_ary.size end |
#to_ary ⇒ Array
Convert the Header into an 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
48 49 50 |
# File 'lib/tty/table/header.rb', line 48 def to_field(attribute = nil) Field.new(attribute) end |
#to_hash ⇒ Object
Provide an unique hash value
128 129 130 |
# File 'lib/tty/table/header.rb', line 128 def to_hash to_a.hash end |