Module: Vapir::HasRowsAndColumns

Included in:
TBody, Table
Defined in:
lib/vapir-common/elements/elements.rb

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object

Returns the TableRow at the given index. indices start at 1.



563
564
565
# File 'lib/vapir-common/elements/elements.rb', line 563

def [](index)
  rows[index]
end

#cellsObject

returns all of the cells of this table. to get the cells including nested tables, use #table_cells, which is defined on all containers (including Table)



582
583
584
585
586
587
588
589
# File 'lib/vapir-common/elements/elements.rb', line 582

def cells
  ElementCollection.new(self, element_class_for(Vapir::TableCell), extra_for_contained.merge(:candidates => proc do |container|
    container_object=container.element_object
    object_collection_to_enumerable(container_object.rows).inject([]) do |candidates, row|
      candidates+object_collection_to_enumerable(row.cells).to_a
    end
  end))
end

#column_count(index = nil) ⇒ Object

returns the number of columns of the table, either on the row at the given index or (by default) on the first row. takes into account any defined colSpans. returns nil if the table has no rows. (if you want the number of cells - not taking into account colspans - use #cell_count on the row in question)



597
598
599
600
601
602
603
604
605
# File 'lib/vapir-common/elements/elements.rb', line 597

def column_count(index=nil)
  if index
    rows[index].column_count
  elsif row=rows.first
    row.column_count
  else
    nil
  end
end

#column_texts_at(column_index) ⇒ Object

Returns an array containing the text of the cell in the specified index in each row.



619
620
621
622
623
# File 'lib/vapir-common/elements/elements.rb', line 619

def column_texts_at(column_index)
  rows.map do |row|
    row.cells[column_index].text
  end
end

#each_rowObject Also known as: each

iterates through the rows in the table. Yields a TableRow object



554
555
556
557
558
# File 'lib/vapir-common/elements/elements.rb', line 554

def each_row
  rows.each do |row|
    yield row
  end
end

#row_countObject

Returns the number of rows inside the table. does not recurse through nested tables. same as (object).rows.length

if you want the row count including nested tables (which this brokenly used to return) use (object).table_rows.length



572
573
574
# File 'lib/vapir-common/elements/elements.rb', line 572

def row_count
  element_object.rows.length
end

#row_count_excluding_nested_tablesObject

Raises:

  • (NotImplementedError)


576
577
578
# File 'lib/vapir-common/elements/elements.rb', line 576

def row_count_excluding_nested_tables
  raise NotImplementedError, "the method \#row_count_excluding_nested_tables is gone. the \#row_count method now returns the number of rows in this #{self.class}. for the number of rows including nested tables, use [this object].table_rows.length"
end

#row_texts_at(row_index) ⇒ Object

Returns an array of the text of each cell in the row at the given index.



611
612
613
614
615
# File 'lib/vapir-common/elements/elements.rb', line 611

def row_texts_at(row_index)
  rows[row_index].cells.map do |cell|
    cell.text
  end
end

#to_aObject

Returns a 2 dimensional array of text contents of each row and column of the table or tbody.



549
550
551
# File 'lib/vapir-common/elements/elements.rb', line 549

def to_a
  rows.map{|row| row.cells.map{|cell| cell.text.strip}}
end