Class: Table

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/capybara-table.rb,
lib/capybara-table/version.rb

Constant Summary collapse

VERSION =

Current Allotment verion

"0.1.0".freeze
DATE =

Date version created

"2014-02-25".freeze

Instance Method Summary collapse

Constructor Details

#initialize(element = "table") ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
# File 'lib/capybara-table.rb', line 8

def initialize element = "table"
  @element     = element
  @headers     = Hashie::Mash.new
  @row_headers = Hashie::Mash.new

  parse_body
  parse_headers
  parse_row_headers
end

Instance Method Details

#[](number) ⇒ Object



70
71
72
# File 'lib/capybara-table.rb', line 70

def [] number
  @table_body.map {|r| r[number]}
end

#column(number) ⇒ Object



58
59
60
# File 'lib/capybara-table.rb', line 58

def column number
  @headers.inject([]) { |sum,h| h[1].include?(number) ? sum.push(h[0]) : sum} + @table_body.map {|r| r[number]}
end

#get_cells(row_name, columns_array) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capybara-table.rb', line 40

def get_cells row_name, columns_array
  columns = columns_array.map{|c| @headers[c.downcase] ? @headers[c.downcase] : [] }.inject(:&)
  row = @row_headers[row_name.downcase]
  raise "Could not find row: %s in row headers: %s" % [row_name.downcase, @row_headers] if row.nil?

  temp = []
  columns.each do |column_index|
    temp.push @table_body[row][column_index]
  end

  raise "Unable to find any matching data with row: %s and columns: %s" % [row_name.to_s, columns_array.to_s] if temp.empty?
  temp.length == 1 ? temp.first : temp
end

#heightObject



66
67
68
# File 'lib/capybara-table.rb', line 66

def height
  column(0).length
end

#parse_bodyObject



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

def parse_body
  @table_body = all("#{@element} > tbody > tr").map{ |h| h.all("td").map{| e| e.text.downcase } }
end

#parse_headersObject



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

def parse_headers
  all("#{@element} > thead > tr").map{ |h| h.all "th" }.each do | header_row |
    column_index = 0
    header_row.each do | header |
      @headers[header.text.downcase] ||= []
      (header["colspan"] ? header["colspan"].to_i : 1).times do
        @headers[header.text.downcase].push column_index

        column_index += 1
      end
    end
  end
end

#parse_row_headersObject



36
37
38
# File 'lib/capybara-table.rb', line 36

def parse_row_headers
  all("#{@element} > tbody > tr").each_with_index{ |r,i| @row_headers[r.find("td:first-child").text.downcase] = i }
end

#row(number) ⇒ Object



54
55
56
# File 'lib/capybara-table.rb', line 54

def row number
  @table_body[number]
end

#widthObject



62
63
64
# File 'lib/capybara-table.rb', line 62

def width
  row(0).length
end