Class: Turnip::StepDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/turnip/step_definition.rb

Defined Under Namespace

Classes: Ambiguous, Match, Pending

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, &block) ⇒ StepDefinition

Returns a new instance of StepDefinition.



32
33
34
35
# File 'lib/turnip/step_definition.rb', line 32

def initialize(expression, &block)
  @expression = expression
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



10
11
12
# File 'lib/turnip/step_definition.rb', line 10

def block
  @block
end

#expressionObject (readonly)

Returns the value of attribute expression.



10
11
12
# File 'lib/turnip/step_definition.rb', line 10

def expression
  @expression
end

Class Method Details

.execute(context, available_steps, step) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/turnip/step_definition.rb', line 13

def execute(context, available_steps, step)
  match = find(available_steps, step.description)
  params = match.params
  params << step.extra_arg if step.extra_arg
  context.instance_exec(*params, &match.block)
rescue Pending
  context.pending "the step '#{step.description}' is not implemented"
end

.find(available_steps, description) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
# File 'lib/turnip/step_definition.rb', line 22

def find(available_steps, description)
  found = available_steps.map do |step|
    step.match(description)
  end.compact
  raise Pending, description if found.length == 0
  raise Ambiguous, description if found.length > 1
  found[0]
end

Instance Method Details

#match(description) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/turnip/step_definition.rb', line 41

def match(description)
  result = description.match(regexp)
  if result
    params = result.captures
    result.names.each_with_index do |name, index|
      params[index] = Turnip::Placeholder.apply(name.to_sym, params[index])
    end
    Match.new(self, params, block)
  end
end

#regexpObject



37
38
39
# File 'lib/turnip/step_definition.rb', line 37

def regexp
  @regexp ||= compile_regexp
end