Class: Cucumber::RbSupport::RbTransform

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/rb_support/rb_transform.rb

Overview

A Ruby Transform holds a Regexp and a Proc, and is created by calling Transform in the <tt>support ruby files. See also RbDsl.

Example:

Transform /^(\d+) cucumbers$/ do |cucumbers_string|
  cucumbers_string.to_i
end

Defined Under Namespace

Classes: MissingProc

Instance Method Summary collapse

Constructor Details

#initialize(rb_language, pattern, proc) ⇒ RbTransform

Returns a new instance of RbTransform

Raises:



20
21
22
23
# File 'lib/cucumber/rb_support/rb_transform.rb', line 20

def initialize(rb_language, pattern, proc)
  raise MissingProc if proc.nil? || proc.arity < 1
  @rb_language, @regexp, @proc = rb_language, Regexp.new(pattern), proc
end

Instance Method Details

#invoke(arg) ⇒ Object



29
30
31
32
33
34
# File 'lib/cucumber/rb_support/rb_transform.rb', line 29

def invoke(arg)
  if matched = match(arg)
    args = matched.captures.empty? ? [arg] : matched.captures
    @rb_language.current_world.cucumber_instance_exec(true, @regexp.inspect, *args, &@proc)
  end
end

#match(arg) ⇒ Object



25
26
27
# File 'lib/cucumber/rb_support/rb_transform.rb', line 25

def match(arg)
  arg ? arg.match(@regexp) : nil
end

#to_sObject



36
37
38
# File 'lib/cucumber/rb_support/rb_transform.rb', line 36

def to_s
   convert_captures(strip_anchors(@regexp.source))
end