Class: TTY::Table::Row Private
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class that represents a row in a table.
Used internally by TTY::Table to store row represenation by converting Array into Row instance.
Instance Attribute Summary collapse
-
#attributes ⇒ Array
readonly
private
The row attributes that describe each element.
-
#data ⇒ Hash
readonly
private
The row data.
-
#fields ⇒ Object
readonly
The row fields.
Attributes included from Equatable
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this row is equivalent to another row.
-
#[](attribute) ⇒ Object
Lookup a value in the row given an attribute allowing for Array or Hash like indexing.
-
#[]=(attribute, value) ⇒ Object
Set value at index.
-
#call(attribute) ⇒ Object
Lookup attribute without evaluation.
-
#coerce_to_fields(values) ⇒ Array[TTY::Table::Field]
Coerces values to field instances.
-
#hash ⇒ Object
Provide a unique hash value.
-
#height ⇒ Integer
Find maximum row height.
-
#initialize(data, header = nil) ⇒ undefined
constructor
Initialize a Row.
-
#inspect ⇒ Object
String representation of a row with its fields.
-
#map!(&block) ⇒ Object
Map field values.
-
#size ⇒ Integer
(also: #length)
Number of data items in a row.
-
#to_ary ⇒ Array
Convert the Row into Array.
-
#to_field(options = nil) ⇒ Object
Instantiates a new field.
-
#to_hash ⇒ Hash
Convert the Row into hash.
Methods included from Equatable
#attr_reader, included, #inherited
Methods inherited from Vector
Methods included from Conversion
Constructor Details
#initialize(data, header = nil) ⇒ undefined
Initialize a Row
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tty/table/row.rb', line 67 def initialize(data, header = nil) case data when Array @attributes = (header || (0...data.length)).to_a @fields = coerce_to_fields(data) when Hash @data = data.dup @fields = coerce_to_fields(@data.values) @attributes = (header || data.keys).to_a end @data = Hash[@attributes.zip(fields)] 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 row attributes that describe each element
31 32 33 |
# File 'lib/tty/table/row.rb', line 31 def attributes @attributes end |
#data ⇒ Hash (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 row data
38 39 40 |
# File 'lib/tty/table/row.rb', line 38 def data @data end |
#fields ⇒ Object (readonly)
The row fields
43 44 45 |
# File 'lib/tty/table/row.rb', line 43 def fields @fields end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check if this row is equivalent to another row
187 188 189 |
# File 'lib/tty/table/row.rb', line 187 def ==(other) to_a == other.to_a end |
#[](attribute) ⇒ Object
Lookup a value in the row given an attribute allowing for Array or Hash like indexing
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tty/table/row.rb', line 107 def [](attribute) case attribute when Integer data[attributes[attribute]].value else data.fetch(attribute) do |name| fail UnknownAttributeError, "the attribute #{name} is unkown" end.value end end |
#[]=(attribute, value) ⇒ Object
Set value at index
131 132 133 134 135 136 137 138 139 |
# File 'lib/tty/table/row.rb', line 131 def []=(attribute, value) case attribute when Integer data[attributes[attribute]] = to_field(value) else data[attribute] = to_field(value) attributes << attribute unless attributes.include?(attribute) end end |
#call(attribute) ⇒ Object
Lookup attribute without evaluation
121 122 123 |
# File 'lib/tty/table/row.rb', line 121 def call(attribute) data[attributes[attribute]] end |
#coerce_to_fields(values) ⇒ Array[TTY::Table::Field]
Coerces values to field instances
87 88 89 |
# File 'lib/tty/table/row.rb', line 87 def coerce_to_fields(values) values.reduce([]) { |acc, el| acc << to_field(el) } end |
#hash ⇒ Object
Provide a unique hash value. If a row contains the same data as another row, they will hash to the same value.
196 197 198 |
# File 'lib/tty/table/row.rb', line 196 def hash to_a.hash end |
#height ⇒ Integer
Find maximum row height
156 157 158 |
# File 'lib/tty/table/row.rb', line 156 def height fields.map(&:height).max end |
#inspect ⇒ Object
String representation of a row with its fields
212 213 214 |
# File 'lib/tty/table/row.rb', line 212 def inspect "#<#{self.class.name} fields=#{to_a}>" end |
#map!(&block) ⇒ Object
Map field values
203 204 205 206 207 |
# File 'lib/tty/table/row.rb', line 203 def map!(&block) data.values_at(*attributes).each do |field| field.value = block.call(field) end end |
#size ⇒ Integer Also known as: length
Number of data items in a row
146 147 148 |
# File 'lib/tty/table/row.rb', line 146 def size data.size end |
#to_ary ⇒ Array
Convert the Row into Array
168 169 170 |
# File 'lib/tty/table/row.rb', line 168 def to_ary to_hash.values_at(*attributes) end |
#to_field(options = nil) ⇒ Object
Instantiates a new field
94 95 96 |
# File 'lib/tty/table/row.rb', line 94 def to_field( = nil) Field.new() end |
#to_hash ⇒ Hash
Convert the Row into hash
177 178 179 180 |
# File 'lib/tty/table/row.rb', line 177 def to_hash hash = data.dup hash.update(hash) { |_, val| val.value if val } end |