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



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

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

#fieldsObject (readonly)

Returns the value of attribute fields.



38
39
40
# File 'lib/tty/table/row.rb', line 38

def fields
  @fields
end

Instance Method Details

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

Check if this row is equivalent to another row

Returns:

  • (Boolean)


170
171
172
# File 'lib/tty/table/row.rb', line 170

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



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

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


114
115
116
117
118
119
120
121
122
# File 'lib/tty/table/row.rb', line 114

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

#call(attribute) ⇒ Object

Lookup attribute without evaluation



104
105
106
# File 'lib/tty/table/row.rb', line 104

def call(attribute)
  data[attributes[attribute]]
end

#hashObject

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



179
180
181
# File 'lib/tty/table/row.rb', line 179

def hash
  to_a.hash
end

#heightInteger

Find maximum row height

Returns:

  • (Integer)


139
140
141
# File 'lib/tty/table/row.rb', line 139

def height
  fields.map { |field| field.height }.max
end

#inspectObject

String representation of a row with its fields



195
196
197
# File 'lib/tty/table/row.rb', line 195

def inspect
  "#<#{self.class.name} fields=#{to_a}>"
end

#map!(&block) ⇒ Object

Map field values



186
187
188
189
190
# File 'lib/tty/table/row.rb', line 186

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)


129
130
131
# File 'lib/tty/table/row.rb', line 129

def size
  data.size
end

#to_aryArray

Convert the Row into Array

Examples:

array = row.to_ary

Returns:

  • (Array)


151
152
153
# File 'lib/tty/table/row.rb', line 151

def to_ary
  to_hash.values_at(*attributes)
end

#to_field(options = nil) ⇒ Object

Instantiates a new field



77
78
79
# File 'lib/tty/table/row.rb', line 77

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

#to_hashHash

Convert the Row into hash

Returns:

  • (Hash)


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

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