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

attributes[R]

The row attributes that describe each element

@return [Array]

@api private

data[R]

The row data

@return [Hash]

@api private

fields[R]

The row fields

@api public

Public Class Methods

new(data, header = nil) click to toggle source

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

==(other) click to toggle source

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
Also aliased as: eql?
[](attribute) click to toggle source

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
[]=(attribute, value) click to toggle source

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
call(attribute) click to toggle source

Lookup attribute without evaluation

@api public

# File lib/tty/table/row.rb, line 124
def call(attribute)
  data[attributes[attribute]]
end
coerce_to_fields(values) click to toggle source

Coerces values to field instances

@param [Array] values

@return [Array]

@api public

# File lib/tty/table/row.rb, line 90
def coerce_to_fields(values)
  values.reduce([]) { |acc, el| acc << to_field(el) }
end
each() { |element| ... } click to toggle source

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
empty?() click to toggle source

Check if there are no elements

@return [Boolean]

@api public

# File lib/tty/table/row.rb, line 174
def empty?
  to_ary.empty?
end
eql?(other)
Alias for: ==
hash() click to toggle source

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
height() click to toggle source

Find maximum row height

@return [Integer]

@api public

# File lib/tty/table/row.rb, line 183
def height
  fields.map(&:height).max
end
inspect() click to toggle source

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
length()
Alias for: size
map!(&block) click to toggle source

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
size() click to toggle source

Number of data items in a row

@return [Integer]

@api public

# File lib/tty/table/row.rb, line 164
def size
  data.size
end
Also aliased as: length
to_a() click to toggle source

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
to_ary() click to toggle source

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
to_field(options = nil) click to toggle source

Instantiates a new field

@api public

# File lib/tty/table/row.rb, line 97
def to_field(options = nil)
  Field.new(options)
end
to_hash() click to toggle source

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