Class: TTY::Table::Header
- Inherits:
-
Object
- Object
- TTY::Table::Header
- 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
-
#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.
-
#each ⇒ self
Iterate over each element in the vector.
-
#empty? ⇒ Boolean
Check if there are no elements.
-
#height ⇒ Integer
Find maximum header height.
-
#initialize(attributes = []) ⇒ undefined
constructor
Initialize a Header.
- #inspect ⇒ Object
-
#size ⇒ Integer
(also: #length)
Size of the header.
-
#to_a ⇒ Array
Return the header elements in an array.
-
#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.
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
#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
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
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
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
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 |
#each ⇒ self
Iterate over each element in the vector
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.
146 147 148 |
# File 'lib/tty/table/header.rb', line 146 def empty? to_ary.empty? end |
#height ⇒ Integer
Find maximum header height
119 120 121 |
# File 'lib/tty/table/header.rb', line 119 def height attributes.map { |field| field.height }.max end |
#inspect ⇒ Object
167 168 169 |
# File 'lib/tty/table/header.rb', line 167 def inspect "#<#{self.class.name} fields=#{to_a}>" end |
#size ⇒ Integer Also known as: length
Size of the header
109 110 111 |
# File 'lib/tty/table/header.rb', line 109 def size to_ary.size end |
#to_a ⇒ Array
Return the header elements in an array.
137 138 139 |
# File 'lib/tty/table/header.rb', line 137 def to_a to_ary.dup end |
#to_ary ⇒ Array
Convert the Header into an 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
65 66 67 |
# File 'lib/tty/table/header.rb', line 65 def to_field(attribute = nil) Field.new(attribute) end |
#to_hash ⇒ Object
Provide an unique hash value
163 164 165 |
# File 'lib/tty/table/header.rb', line 163 def to_hash to_a.hash end |