Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber-table.rb

Instance Method Summary collapse

Instance Method Details

#to_table(binding = nil) ⇒ Object



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

def to_table binding = nil
  table = split("\n").reject {|l| l.strip.empty? }.each {|l|
      raise CucumberTable::TableFormatException.new('Invalid table format') unless /^\s*\|.+\|\s*$/ =~ l
    }.map { |s| s.gsub(/\|(.+)\|/, '\1').split('|').map(&:strip) }
  unless table.map(&:size).uniq.size == 1 # All cols has the same count of cells
    raise CucumberTable::TableFormatException.new('Invalid table format')
  end
  table = table.map {|row| row.map {|cell| ERB.new(cell).result(binding) }} if binding
  CucumberTable::Table.new table
end