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.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Attributes included from Equatable
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this row is equivalent to another row.
-
#[](attribute) ⇒ Object
Lookup a value in the row given an attribute allowing for Array or Hash like indexing.
-
#[]=(attribute, value) ⇒ Object
Set value at index.
-
#call(attribute) ⇒ Object
Lookup attribute without evaluation.
-
#hash ⇒ Object
Provide a unique hash value.
-
#height ⇒ Integer
Find maximum row height.
-
#initialize(data, header = nil) ⇒ undefined
constructor
Initialize a Row.
-
#inspect ⇒ Object
String representation of a row with its fields.
-
#map!(&block) ⇒ Object
Map field values.
-
#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
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
#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 |
#fields ⇒ Object (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
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
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 |
#hash ⇒ Object
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 |
#height ⇒ Integer
Find maximum row height
139 140 141 |
# File 'lib/tty/table/row.rb', line 139 def height fields.map { |field| field.height }.max end |
#inspect ⇒ Object
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 |
#size ⇒ Integer Also known as: length
Number of data items in a row
129 130 131 |
# File 'lib/tty/table/row.rb', line 129 def size data.size end |
#to_ary ⇒ Array
Convert the Row into 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(=nil) Field.new() end |
#to_hash ⇒ Hash
Convert the Row into 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 |