class Cucumber::RbSupport::RbStepDefinition

A Ruby Step Definition holds a Regexp and a Proc, and is created by calling Given, When or Then in the step_definitions ruby files. See also RbDsl.

Example:

Given /I have (\d+) cucumbers in my belly/ do
  # some code here
end

Public Class Methods

new(rb_language, pattern, proc_or_sym, options) click to toggle source
Calls superclass method
# File lib/cucumber/rb_support/rb_step_definition.rb, line 26
def new(rb_language, pattern, proc_or_sym, options)
  raise MissingProc if proc_or_sym.nil?
  super rb_language, parse_pattern(pattern), create_proc(proc_or_sym, options)
end
new(rb_language, regexp, proc) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 72
def initialize(rb_language, regexp, proc)
  @rb_language, @regexp, @proc = rb_language, regexp, proc
  @rb_language.available_step_definition(regexp_source, location)
end

Private Class Methods

create_proc(proc_or_sym, options) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 41
def create_proc(proc_or_sym, options)
  return proc_or_sym if proc_or_sym.is_a?(Proc)
  raise ArgumentError unless proc_or_sym.is_a?(Symbol)
  message = proc_or_sym
  target_proc = parse_target_proc_from(options)
  patch_location_onto lambda { |*args|
    target = instance_exec(&target_proc)
    target.send(message, *args)
  }
end
parse_pattern(pattern) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 33
def parse_pattern(pattern)
  return pattern if pattern.is_a?(Regexp)
  raise ArgumentError unless pattern.is_a?(String)
  p = Regexp.escape(pattern)
  p = p.gsub(/\\$\w+/, '(.*)') # Replace $var with (.*)
  Regexp.new("^#{p}$")
end
parse_target_proc_from(options) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 58
def parse_target_proc_from(options)
  return lambda { self } unless options.key?(:on)
  target = options[:on]
  case target
  when Proc
    target
  when Symbol
    lambda { self.send(target) }
  else
    lambda { raise ArgumentError, "Target must be a symbol or a proc" }
  end
end
patch_location_onto(block) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 52
def patch_location_onto(block)
  location = Core::Ast::Location.of_caller(5)
  block.define_singleton_method(:source_location) { [location.file, location.line] }
  block
end

Public Instance Methods

==(step_definition) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 89
def ==(step_definition)
  regexp_source == step_definition.regexp_source
end
arguments_from(step_name) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 93
def arguments_from(step_name)
  args = RegexpArgumentMatcher.arguments_from(@regexp, step_name)
  @rb_language.invoked_step_definition(regexp_source, location) if args
  args
end
backtrace_line() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 109
def backtrace_line
  "#{location.to_s}:in `#{regexp_source}'"
end
file() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 126
def file
  @file ||= location.file
end
file_colon_line() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 113
def file_colon_line
  case @proc
  when Proc
    location.to_s
  when Symbol
    ":#{@proc}"
  end
end
invoke(args) click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 99
def invoke(args)
  begin
    args = @rb_language.execute_transforms(args)
    @rb_language.current_world.cucumber_instance_exec(true, regexp_source, *args, &@proc)
  rescue Cucumber::ArityMismatchError => e
    e.backtrace.unshift(self.backtrace_line)
    raise e
  end
end
location() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 122
def location
  @location ||= Cucumber::Core::Ast::Location.from_source_location(*@proc.source_location)
end
regexp_source() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 77
def regexp_source
  @regexp.inspect
end
to_hash() click to toggle source
# File lib/cucumber/rb_support/rb_step_definition.rb, line 81
def to_hash
  flags = ''
  flags += 'm' if (@regexp.options & Regexp::MULTILINE) != 0
  flags += 'i' if (@regexp.options & Regexp::IGNORECASE) != 0
  flags += 'x' if (@regexp.options & Regexp::EXTENDED) != 0
  {'source' => @regexp.source, 'flags' => flags}
end