Class: TTY::Table::Row Private
- Inherits:
-
Object
- Object
- TTY::Table::Row
- 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
-
#attributes ⇒ Array
readonly
private
The row attributes that describe each element.
-
#data ⇒ Hash
readonly
private
The row data.
-
#fields ⇒ Object
readonly
The row fields.
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.
-
#coerce_to_fields(values) ⇒ Array[TTY::Table::Field]
Coerces values to field instances.
-
#each ⇒ self
Iterate over each element in the Row.
-
#empty? ⇒ Boolean
Check if there are no elements.
-
#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_a ⇒ Array
Return the Row elements in an array.
-
#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.
Constructor Details
#initialize(data, header = nil) ⇒ undefined
Initialize a Row
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
#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 that describe each element
34 35 36 |
# File 'lib/tty/table/row.rb', line 34 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
41 42 43 |
# File 'lib/tty/table/row.rb', line 41 def data @data end |
#fields ⇒ Object (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
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
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
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
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 |
#each ⇒ self
Iterate over each element in the Row
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
174 175 176 |
# File 'lib/tty/table/row.rb', line 174 def empty? to_ary.empty? 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.
232 233 234 |
# File 'lib/tty/table/row.rb', line 232 def hash to_a.hash end |
#height ⇒ Integer
Find maximum row height
183 184 185 |
# File 'lib/tty/table/row.rb', line 183 def height fields.map(&:height).max end |
#inspect ⇒ Object
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 |
#size ⇒ Integer Also known as: length
Number of data items in a row
164 165 166 |
# File 'lib/tty/table/row.rb', line 164 def size data.size end |
#to_a ⇒ Array
Return the Row elements in an array.
204 205 206 |
# File 'lib/tty/table/row.rb', line 204 def to_a to_ary.dup end |
#to_ary ⇒ Array
Convert the Row into 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( = nil) Field.new() end |
#to_hash ⇒ Hash
Convert the Row into 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 |