Class: GreenPepper::RuleForExample

Inherits:
ExampleWithFixture show all
Defined in:
lib/greenpepper/example/ruleforexample.rb

Constant Summary collapse

FIRST_DATA_ROW =
2

Constants inherited from ExampleWithFixture

ExampleWithFixture::EXAMPLE_NAME_COLUMN, ExampleWithFixture::EXAMPLE_NAME_ROW, ExampleWithFixture::FIXTURE_NAME_COLUMN, ExampleWithFixture::FIXTURE_NAME_ROW, ExampleWithFixture::HEADER_ROW

Instance Attribute Summary collapse

Attributes inherited from ExampleWithFixture

#fixture_arguments, #fixture_name, #headers

Instance Method Summary collapse

Methods inherited from ExampleWithFixture

#add_fixture_argument, #execute

Constructor Details

#initialize(fixture_name, name_resolver = NameResolver) ⇒ RuleForExample

Returns a new instance of RuleForExample.



20
21
22
23
24
# File 'lib/greenpepper/example/ruleforexample.rb', line 20

def initialize(fixture_name, name_resolver = NameResolver)
  super fixture_name
  @values = Array.new
  @name_resolver = name_resolver
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



16
17
18
# File 'lib/greenpepper/example/ruleforexample.rb', line 16

def values
  @values
end

Instance Method Details

#add_header(name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/greenpepper/example/ruleforexample.rb', line 26

def add_header(name)
  output_header_regex = /\s*(.*)\?\s*\Z/
  if output_header_regex === name 
    add_output_header($1)
  else
    add_input_header(name)
  end
end

#add_input_header(name) ⇒ Object



35
36
37
38
39
# File 'lib/greenpepper/example/ruleforexample.rb', line 35

def add_input_header(name)
  header = ExampleInputHeader.new(name, @name_resolver)
  @headers.push header
  header
end

#add_output_header(name) ⇒ Object



41
42
43
44
45
# File 'lib/greenpepper/example/ruleforexample.rb', line 41

def add_output_header(name)
  header = RuleForOutputHeader.new(name, @name_resolver)
  @headers.push header 
  header
end

#add_row(values_array) ⇒ Object



47
48
49
# File 'lib/greenpepper/example/ruleforexample.rb', line 47

def add_row(values_array)
  @values.push values_array
end

#do_execute(results, fixture_class) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/greenpepper/example/ruleforexample.rb', line 51

def do_execute(results, fixture_class)
  @values.each_index{ |row_index|
    # We instantiate a fixture every row to reset the object
    fixture = instantiate_fixture fixture_class

    headers.each_index{ |column_index|
      header = headers[column_index]
      string_value = @values[row_index][column_index]
      value = TypeConverter.instance.auto_convert_string(
        fixture, header.name, string_value)

      #The output header needs to keep the value of an empty string
      #instead of nil because it needs to differentiate the nil value
      #and an ignored cell.
      if header.class == RuleForOutputHeader &&
        (string_value == '')
        value = ''
      end
      result = header.execute( value, fixture, string_value)
      results.add row_index + FIRST_DATA_ROW, column_index,
        result unless result == nil
    }
  }
  results
end