Class: TTY::Table::Header
Overview
A set of header elements that correspond with values in each row
Instance Attribute Summary collapse
-
#attributes ⇒ Array
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
(also: #call)
Lookup a column in the header given a name.
-
#[]=(attribute, value) ⇒ Object
Set value at index.
-
#filter ⇒ Object
Filter header names including styling background, text color and capitalization.
-
#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
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)
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
101 102 103 |
# File 'lib/tty/table/header.rb', line 101 def ==(other) to_a == other.to_a end |
#[](attribute) ⇒ Object Also known as: call
Lookup a column in the header given a name
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tty/table/header.rb', line 59 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 |
#filter ⇒ Object
Filter header names including styling background, text color and capitalization
53 54 |
# File 'lib/tty/table/header.rb', line 53 def filter 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
92 93 94 |
# File 'lib/tty/table/header.rb', line 92 def to_ary attributes.map { |attr| attr.value if attr.value } end |
#to_field(options = nil) ⇒ Object
Instantiates a new field
45 46 47 |
# File 'lib/tty/table/header.rb', line 45 def to_field(=nil) Field.new() end |
#to_hash ⇒ Object
Provide an unique hash value
109 110 111 |
# File 'lib/tty/table/header.rb', line 109 def to_hash to_a.hash end |