Class: TTY::Table::Header
Overview
A set of header elements that correspond with values in each row
Instance Attribute Summary collapse
-
#attributes ⇒ Array
(also: #fields)
readonly
private
The header attributes.
Attributes included from Equatable
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 ⇒ Object
(also: #length)
Size of the header.
-
#to_ary ⇒ Object
Convert the Header into an Array.
-
#to_field(options = nil) ⇒ Object
Instantiates a new field.
-
#to_hash ⇒ Object
Provide an unique hash value.
Methods included from Equatable
#attr_reader, included, #inherited
Methods inherited from Vector
Methods included from Conversion
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
#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
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
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
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 |
#height ⇒ Integer
Find maximum header height
94 95 96 |
# File 'lib/tty/table/header.rb', line 94 def height attributes.map { |field| field.height }.max end |
#size ⇒ Object 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_ary ⇒ Object
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(=nil) Field.new() end |
#to_hash ⇒ Object
Provide an unique hash value
118 119 120 |
# File 'lib/tty/table/header.rb', line 118 def to_hash to_a.hash end |