Class: TTY::Table::Row

Inherits:
Vector
  • Object
show all
Extended by:
Forwardable
Includes:
Equatable
Defined in:
lib/tty/table/row.rb

Overview

A class that represents a row in a table.

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Instance Method Summary collapse

Methods included from Equatable

#attr_reader, included, #inherited

Methods inherited from Vector

[], #each, #empty?, #to_a

Methods included from Conversion

#convert_to_array

Constructor Details

#initialize(data, header = nil) ⇒ undefined

Initialize a Row

Examples:

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

Parameters:

  • data (#to_ary)

    the row data



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tty/table/row.rb', line 58

def initialize(data, header=nil)
  case data
  when Array
    @attributes = (header || (0...data.length)).to_a
    fields = data.inject([]) { |arr, datum| arr << to_field(datum) }
    @data = Hash[@attributes.zip(fields)]
  when Hash
    @data = data.dup
    fields = @data.values.inject([]){|arr, datum| arr << to_field(datum) }
    @attributes = (header || data.keys).to_a
    @data = Hash[@attributes.zip(fields)]
  end
end

Instance Attribute Details

#attributesArray (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

Returns:

  • (Array)


29
30
31
# File 'lib/tty/table/row.rb', line 29

def attributes
  @attributes
end

#dataHash (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

Returns:

  • (Hash)


36
37
38
# File 'lib/tty/table/row.rb', line 36

def data
  @data
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check if this row is equivalent to another row

Returns:

  • (Boolean)


153
154
155
# File 'lib/tty/table/row.rb', line 153

def ==(other)
  to_a == other.to_a
end

#[](attribute) ⇒ Object Also known as: call

Lookup a value in the row given an attribute allowing for Array or Hash like indexing



88
89
90
91
92
93
94
95
96
97
# File 'lib/tty/table/row.rb', line 88

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) ⇒ Object

Set value at index

Examples:

row[attribute] = value


106
107
108
109
110
111
112
113
114
# File 'lib/tty/table/row.rb', line 106

def []=(attribute, value)
  case attribute
  when Integer
    self.data[attributes[attribute]] = to_field(value)
  else
    self.data[attribute] = to_field(value)
    self.attributes << attribute unless attributes.include?(attribute)
  end
end

#hashObject

Provide a unique hash value. If a row contains the same data as another row, they will hash to the same value.



162
163
164
# File 'lib/tty/table/row.rb', line 162

def hash
  to_a.hash
end

#map!(&block) ⇒ Object



166
167
168
169
170
# File 'lib/tty/table/row.rb', line 166

def map!(&block)
  data.values_at(*attributes).each do |field|
    field.value = block.call(field)
  end
end

#sizeInteger Also known as: length

Number of data items in a row

Returns:

  • (Integer)


121
122
123
# File 'lib/tty/table/row.rb', line 121

def size
  data.size
end

#to_aryArray

Convert the Row into Array

Examples:

array = row.to_ary

Returns:

  • (Array)


134
135
136
# File 'lib/tty/table/row.rb', line 134

def to_ary
  to_hash.values_at(*attributes)
end

#to_field(options = nil) ⇒ Object

Instantiates a new field



75
76
77
# File 'lib/tty/table/row.rb', line 75

def to_field(options=nil)
  Field.new(options)
end

#to_hashHash

Convert the Row into hash

Returns:

  • (Hash)


143
144
145
146
# File 'lib/tty/table/row.rb', line 143

def to_hash
  hash = data.dup
  hash.update(hash) { |key, val| val.value if val }
end