Class: Tabulo::Row

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tabulo/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sourceObject (readonly)

Returns the element of the Table's underlying enumerable to which this Tabulo::Row corresponds.

Returns:

  • the element of the Table's underlying enumerable to which this Tabulo::Row corresponds


7
8
9
# File 'lib/tabulo/row.rb', line 7

def source
  @source
end

Instance Method Details

#eachObject

Calls the given block once for each Cell in the Tabulo::Row, passing that Cell as parameter.

Examples:

table = Tabulo::Table.new([1, 10], columns: %i(itself even?))
row = table.first
row.each do |cell|
  puts cell.value   # => 1,       => false
end

24
25
26
27
28
# File 'lib/tabulo/row.rb', line 24

def each
  @table.column_registry.each do |_, column|
    yield column.body_cell(@source)
  end
end

#to_hObject

Returns a Hash representation of the Tabulo::Row, with column labels acting as keys and the Cells the values.

Returns:

  • a Hash representation of the Tabulo::Row, with column labels acting as keys and the Cells the values.


42
43
44
# File 'lib/tabulo/row.rb', line 42

def to_h
  @table.column_registry.map { |label, column| [label, column.body_cell(@source)] }.to_h
end

#to_sObject

Returns a String being an "ASCII" graphical representation of the Tabulo::Row, including any column headers that appear just above it in the Table (depending on where this Row is in the Table and how the Table was configured with respect to header frequency).

Returns:

  • a String being an "ASCII" graphical representation of the Tabulo::Row, including any column headers that appear just above it in the Table (depending on where this Row is in the Table and how the Table was configured with respect to header frequency).


33
34
35
36
37
38
39
# File 'lib/tabulo/row.rb', line 33

def to_s
  if @table.column_registry.any?
    @table.formatted_body_row(@source, header: @header)
  else
    ""
  end
end