Class: GreenPepper::RuleForExampleFactory

Inherits:
ExampleFactory show all
Defined in:
lib/greenpepper/factory/ruleforexamplefactory.rb

Instance Method Summary collapse

Methods inherited from ExampleFactory

#last_table?

Instance Method Details

#create_example(table) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/greenpepper/factory/ruleforexamplefactory.rb', line 12

def create_example(table)
  return nil unless support? table

  output_header_regex = /\s*(.*)\?\s*\Z/

  # fixture name in cell 0,1
  example = RuleForExample.new table[0][1]

  # fixture argument in row 0 with symbol/value pair
  args_cells_size = table[0][2..-1].size
  2.step(args_cells_size, 2) do |i|
    example.add_fixture_argument table[0][i + 1]
  end

  # headers in row 1
  table[1].each do |header|
    example.add_header header
  end

  # data in next rows
  table[2..-1].each_with_index {|row, index| 
    example.add_row row
    if row.size != table[1].size
      message = "Missing at least one cell on line #{index+1}: Header has #{table[1].size} cells while current row has #{row.size}"
      raise GreenPepperMalformedTableError.new(message) 
    end
  }
  return example
end

#support?(table) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/greenpepper/factory/ruleforexamplefactory.rb', line 42

def support?(table)
  /^\s*rule\s*for\s*$/i === table[0][0]
end