class TTY::Table::Row
A class that represents a row in a table.
Used internally by {Table} to store row represenation by converting {Array} into {Row} instance.
@api private
Attributes
The row attributes that describe each element
@return [Array]
@api private
The row data
@return [Hash]
@api private
The row fields
@api public
Public Class Methods
Initialize a Row
@example
row = new TTY::Table::Row.new [1,2,3] row[1] # => 2 row = new TTY::Table::Row.new [1,2,3], %w[a b c] row[0] # => 1 row["a"] # => 1 row = new TTY::Table::Row.new {"a": 1, "b": 2, "c": 3} row[0] # => 1 row["a"] # => 1
@param [#to_ary] data
the row data
@return [undefined]
@api public
# File lib/tty/table/row.rb, line 70 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
Public Instance Methods
Check if this row is equivalent to another row
@return [Boolean]
@api public
# File lib/tty/table/row.rb, line 223 def ==(other) to_a == other.to_a end
Lookup a value in the row given an attribute allowing for Array or Hash like indexing
@example
row[1] row[:id] row.call(:id)
@api public
# File lib/tty/table/row.rb, line 110 def [](attribute) case attribute when Integer data[attributes[attribute]].value else data.fetch(attribute) do |name| raise UnknownAttributeError, "the attribute #{name} is unkown" end.value end end
Set value at index
@example
row[attribute] = value
@api public
# File lib/tty/table/row.rb, line 134 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
Lookup attribute without evaluation
@api public
# File lib/tty/table/row.rb, line 124 def call(attribute) data[attributes[attribute]] end
Iterate over each element in the Row
@example
vec = Row.new [1,2,3], ["a","b","c"] vec.each { |element| ... }
@return [self]
@api public
# File lib/tty/table/row.rb, line 153 def each return to_enum unless block_given? to_ary.each { |element| yield element } self end
Check if there are no elements
@return [Boolean]
@api public
# File lib/tty/table/row.rb, line 174 def empty? to_ary.empty? end
Provide a unique hash value. If a row contains the same data as another row, they will hash to the same value.
@api public
# File lib/tty/table/row.rb, line 232 def hash to_a.hash end
Find maximum row height
@return [Integer]
@api public
# File lib/tty/table/row.rb, line 183 def height fields.map(&:height).max end
String representation of a row with its fields
@api public
# File lib/tty/table/row.rb, line 248 def inspect "#<#{self.class.name} fields=#{to_a}>" end
Map field values
@api public
# File lib/tty/table/row.rb, line 239 def map!(&block) data.values_at(*attributes).each do |field| field.value = block.call(field) end end
Number of data items in a row
@return [Integer]
@api public
# File lib/tty/table/row.rb, line 164 def size data.size end
Return the Row
elements in an array.
@return [Array]
@api public
# File lib/tty/table/row.rb, line 204 def to_a to_ary.dup end
Convert the Row
into Array
@example
array = row.to_ary
@return [Array]
@api public
# File lib/tty/table/row.rb, line 195 def to_ary to_hash.values_at(*attributes) end
Instantiates a new field
@api public
# File lib/tty/table/row.rb, line 97 def to_field(options = nil) Field.new(options) end
Convert the Row
into hash
@return [Hash]
@api public
# File lib/tty/table/row.rb, line 213 def to_hash hash = data.dup hash.update(hash) { |_, val| val.value if val } end