Class: TTY::Table::Row
Overview
A class that represents a row in a table.
Instance Attribute Summary collapse
-
#attributes ⇒ Array
readonly
private
The row attributes.
-
#data ⇒ Hash
readonly
private
The row data.
Attributes included from Equatable
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this row is equivalent to another row.
-
#[](attribute) ⇒ Object
(also: #call)
Lookup a value in the row given an attribute allowing for Array or Hash like indexing.
-
#[]=(attribute, value) ⇒ Object
Set value at index.
-
#hash ⇒ Object
Provide a unique hash value.
-
#initialize(data, header = nil) ⇒ undefined
constructor
Initialize a Row.
- #map!(&block) ⇒ Object
-
#size ⇒ Integer
(also: #length)
Number of data items in a row.
-
#to_ary ⇒ Array
Convert the Row into Array.
-
#to_field(options = nil) ⇒ Object
Instantiates a new field.
-
#to_hash ⇒ Hash
Convert the Row into hash.
Methods included from Equatable
#attr_reader, included, #inherited
Methods inherited from Vector
Methods included from Conversion
Constructor Details
#initialize(data, header = nil) ⇒ undefined
Initialize a Row
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
#attributes ⇒ Array (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
29 30 31 |
# File 'lib/tty/table/row.rb', line 29 def attributes @attributes end |
#data ⇒ Hash (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
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
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
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 |
#hash ⇒ Object
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 |
#size ⇒ Integer Also known as: length
Number of data items in a row
121 122 123 |
# File 'lib/tty/table/row.rb', line 121 def size data.size end |
#to_ary ⇒ Array
Convert the Row into 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(=nil) Field.new() end |
#to_hash ⇒ Hash
Convert the Row into 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 |