class Turnip::StepDefinition

Constants

ALTERNATIVE_WORD_REGEXP
OPTIONAL_WORD_REGEXP
PLACEHOLDER_REGEXP

Attributes

block[R]
called_from[R]
expression[R]
method_name[R]

Public Class Methods

new(expression, method_name=nil, called_from=nil, &block) click to toggle source
# File lib/turnip/step_definition.rb, line 15
def initialize(expression, method_name=nil, called_from=nil, &block)
  @expression = expression
  @method_name = method_name || expression
  @called_from = called_from
  @block = block
end

Public Instance Methods

match(description) click to toggle source
# File lib/turnip/step_definition.rb, line 26
def match(description)
  result = description.match(regexp)
  if result
    params = result.captures
    @placeholder_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
regexp() click to toggle source
# File lib/turnip/step_definition.rb, line 22
def regexp
  @regexp ||= compile_regexp
end

Protected Instance Methods

compile_regexp() click to toggle source
# File lib/turnip/step_definition.rb, line 43
def compile_regexp
  @placeholder_names = []
  regexp = Regexp.escape(expression)
  regexp.gsub!(PLACEHOLDER_REGEXP) do |_|
    @placeholder_names << "#{$1}"
    "(?<#{$1}>#{Placeholder.resolve($1.to_sym)})"
  end
  regexp.gsub!(OPTIONAL_WORD_REGEXP) do |_|
    [$1, $2, $3].compact.map { |m| "(?:#{m})?" }.join
  end
  regexp.gsub!(ALTERNATIVE_WORD_REGEXP) do |_|
    "(?:#{$1}#{$2.tr('/', '|')})"
  end
  Regexp.new("^#{regexp}$")
end