Class: TTY::Table::Row Private

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

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 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



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

#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 that describe each element

Returns:

  • (Array)


31
32
33
# File 'lib/tty/table/row.rb', line 31

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)


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

def data
  @data
end

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

Returns:

  • (Boolean)


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

Examples:

row[attribute] = value


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

Parameters:

  • values (Array[Object])

Returns:



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

#hashObject

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

#heightInteger

Find maximum row height

Returns:

  • (Integer)


156
157
158
# File 'lib/tty/table/row.rb', line 156

def height
  fields.map(&:height).max
end

#inspectObject

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

#sizeInteger Also known as: length

Number of data items in a row

Returns:

  • (Integer)


146
147
148
# File 'lib/tty/table/row.rb', line 146

def size
  data.size
end

#to_aryArray

Convert the Row into Array

Examples:

array = row.to_ary

Returns:

  • (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(options = nil)
  Field.new(options)
end

#to_hashHash

Convert the Row into hash

Returns:

  • (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