Sha256: e3b8ab9a02cdf7e32da860118bb03213cde08dcf2a4c278553493876669217ce

Contents?: true

Size: 1.43 KB

Versions: 35

Compression:

Stored size: 1.43 KB

Contents

module Cucumber
  module RbSupport
    # A Ruby Transform holds a Regexp and a Proc, and is created
    # by calling <tt>Transform in the <tt>support</tt> ruby files.
    # See also RbDsl.
    #
    # Example:
    #
    #   Transform /^(\d+) cucumbers$/ do |cucumbers_string|
    #     cucumbers_string.to_i
    #   end
    #
    class RbTransform
      class MissingProc < StandardError
        def message
          "Transforms must always have a proc with at least one argument"
        end
      end

      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

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

      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

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

    private
      def convert_captures(regexp_source)
        regexp_source.gsub(/(\()(?!\?:)/,'(?:')
      end

      def strip_captures(regexp_source)
        regexp_source.
          gsub(/(\()/, '').
          gsub(/(\))/, '')
      end

      def strip_anchors(regexp_source)
        regexp_source.
          gsub(/(^\^|\$$)/, '')
      end
    end
  end
end

Version data entries

35 entries across 33 versions & 2 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/cucumber-1.3.16/lib/cucumber/rb_support/rb_transform.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/cucumber-1.3.18/lib/cucumber/rb_support/rb_transform.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/cucumber-1.3.18/lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.20 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.19 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.rc.3 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.rc.2 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.rc.1 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.18 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.beta.5 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.beta.4 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.beta.3 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.17 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.beta.2 lib/cucumber/rb_support/rb_transform.rb
cucumber-2.0.0.beta.1 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.16 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.15 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.14 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.13 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.3.12 lib/cucumber/rb_support/rb_transform.rb