Class: WebUnit::Table

Inherits:
HtmlElem show all
Defined in:
lib/webunit/table.rb

Instance Attribute Summary collapse

Attributes inherited from HtmlElem

#array, #attrs, #children, #data, #name, #tag

Instance Method Summary collapse

Methods inherited from HtmlElem

#append, #extract, #find, #has?, #inspect, #print, #readlink, #search

Constructor Details

#initialize(ah) ⇒ Table

Returns a new instance of Table.



13
14
15
16
# File 'lib/webunit/table.rb', line 13

def initialize( ah )
  super( 'table', ah )
  @rows = []
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/webunit/table.rb', line 11

def rows
  @rows
end

Instance Method Details

#add_cell(cell) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webunit/table.rb', line 22

def add_cell( cell )
  p = @rows.last.size + 1
  if @rows.size > 1 && @rows[-2].at(p) && @rows[-2].at(p).rs > 1 then
    @rows.last.add( @rows[-2].at(p) )
    @rows[-2].at(p).rs -= 1
  end
  @rows.last.add( cell )
  while cell.cs > 1 do
    @rows.last.add( cell )
    cell.cs -= 1
  end
end

#add_row(ah) ⇒ Object



18
19
20
# File 'lib/webunit/table.rb', line 18

def add_row( ah )
  @rows.push( TableRow::new( ah ) )
end

#at(r, c = nil) ⇒ Object

— Table#at(r,c=nil)

return a TableCell, when two arguments(row,col) specified.
return a TableRow, when a argument(row) specified.
r and c is both beginning from 1.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/webunit/table.rb', line 42

def at( r, c=nil )
  if r < 1
    raise ArgumentError
  end
  if c == nil
    if @rows == [] || @rows.size < r
      raise
    end
    @rows[r-1]
  else
    if c < 1
      raise ArgumentError
    end
    if @rows == [] || @rows.size < r || @rows[r-1].size < c
      NilTableCell::new
    else
      @rows[r-1].at(c)
    end
  end
end

#col_index(data, row = 1) ⇒ Object

— Table#col_index( data, row=1 )



78
79
80
81
82
83
84
# File 'lib/webunit/table.rb', line 78

def col_index( data, row=1 )
  col = nil
  (1..(self.at(row).size)).each do |col|
    break unless self.at(row,col).search( data ) == []
  end
  col
end

#row_index(data, col = 1) ⇒ Object

— Table#row_index( data, col=1 )



67
68
69
70
71
72
73
# File 'lib/webunit/table.rb', line 67

def row_index( data, col=1 )
  row = nil
  (1..@rows.size).each do |row|
    break unless self.at(row,col).search( data ) == []
  end
  row
end