Class: TTY::Table::Row Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
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

Instance Method Summary collapse

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



70
71
72
73
74
75
76
77
78
79
80
81
# 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

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)


34
35
36
# File 'lib/tty/table/row.rb', line 34

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)


41
42
43
# File 'lib/tty/table/row.rb', line 41

def data
  @data
end

#fieldsObject (readonly)

The row fields



46
47
48
# File 'lib/tty/table/row.rb', line 46

def fields
  @fields
end

Instance Method Details

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

Check if this row is equivalent to another row

Returns:

  • (Boolean)


223
224
225
# File 'lib/tty/table/row.rb', line 223

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

Examples:

row[1]
row[:id]
row.call(:id)


110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object

Set value at index

Examples:

row[attribute] = value


134
135
136
137
138
139
140
141
142
# 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) ⇒ Object

Lookup attribute without evaluation



124
125
126
# File 'lib/tty/table/row.rb', line 124

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:



90
91
92
# File 'lib/tty/table/row.rb', line 90

def coerce_to_fields(values)
  values.reduce([]) { |acc, el| acc << to_field(el) }
end

#eachself

Iterate over each element in the Row

Examples:

vec = Row.new [1,2,3], ["a","b","c"]
vec.each { |element| ... }

Returns:

  • (self)


153
154
155
156
157
# 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?Boolean

Check if there are no elements

Returns:

  • (Boolean)


174
175
176
# File 'lib/tty/table/row.rb', line 174

def empty?
  to_ary.empty?
end

#hashObject

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



232
233
234
# File 'lib/tty/table/row.rb', line 232

def hash
  to_a.hash
end

#heightInteger

Find maximum row height

Returns:

  • (Integer)


183
184
185
# File 'lib/tty/table/row.rb', line 183

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

#inspectObject

String representation of a row with its fields



248
249
250
# File 'lib/tty/table/row.rb', line 248

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

#map!(&block) ⇒ Object

Map field values



239
240
241
242
243
# 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

#sizeInteger Also known as: length

Number of data items in a row

Returns:

  • (Integer)


164
165
166
# File 'lib/tty/table/row.rb', line 164

def size
  data.size
end

#to_aArray

Return the Row elements in an array.

Returns:

  • (Array)


204
205
206
# File 'lib/tty/table/row.rb', line 204

def to_a
  to_ary.dup
end

#to_aryArray

Convert the Row into Array

Examples:

array = row.to_ary

Returns:

  • (Array)


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

def to_ary
  to_hash.values_at(*attributes)
end

#to_field(options = nil) ⇒ Object

Instantiates a new field



97
98
99
# File 'lib/tty/table/row.rb', line 97

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

#to_hashHash

Convert the Row into hash

Returns:

  • (Hash)


213
214
215
216
# File 'lib/tty/table/row.rb', line 213

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