Module: GreenPepper::FreeText

Defined in:
lib/greenpepper/freetext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/greenpepper/freetext.rb', line 11

def self.included(base)
  # In ruby 1.8.x, class variables scopes are a little too permisive. 
  # This prevents us from reusing the same name for for all 
  # actions. So we define a variable with a unique name to avoid a variable 
  # clash.
  base.send :class_variable_set, "@@gp_actions_#{base}", []

  base.class_eval {
    def self.actions
      self.send :class_variable_get, "@@gp_actions_#{self}"
    end

    def self.add_action(action)
      actions << action
    end

    def self.GivenVanilla(regex, &block) 
      self.add_action GivenKeyword.new(regex, block, false) 
    end

    def self.Given(regex, &block) 
      self.add_action GivenKeyword.new(regex, block, true) 
    end

    def self.WhenVanilla(regex, &block) 
      self.add_action WhenKeyword.new(regex, block, false) 
    end

    def self.When(regex, &block) 
      self.add_action WhenKeyword.new(regex, block, true) 
    end

    def self.AskVanilla(regex, &block)
      self.add_action AskKeyword.new(regex, block, false)
    end

    def self.Ask(regex, &block)
      self.add_action AskKeyword.new(regex, block, true)
    end

    def self.CheckVanilla(regex, &block)
      self.add_action CheckKeyword.new(regex, block, false)
    end

    def self.Check(regex, &block)
      self.add_action CheckKeyword.new(regex, block, true)
    end

    def self.ThenVanilla(regex, &block)
      self.add_action ThenKeyword.new(regex, block, false)
    end

    def self.Then(regex, &block)
      self.add_action ThenKeyword.new(regex, block, true)
    end
  }
end

Instance Method Details

#call_method(keyword, args) ⇒ Object



83
84
85
86
87
88
# File 'lib/greenpepper/freetext.rb', line 83

def call_method(keyword, args)
  # Set the gp_current_method to be the block so we can call it
  # as an instance method and not as a class method.
  self.class.send :define_method, "gp_current_method", keyword.block
  keyword.result = gp_current_method(*args)
end

#greenpepper_map_action(action) ⇒ Object

Maps the specified action to the correct block of code and executes it.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/greenpepper/freetext.rb', line 70

def greenpepper_map_action(action)
  actions = self.class.send(:class_variable_get, "@@gp_actions_#{self.class}")
  actions.each{ |keyword| 
    if keyword.match?(action)
      args = keyword.args(action)

      call_method(keyword, args)
      return keyword
    end
  }
  raise GreenPepperUnknownActionError.new action
end