Sha256: 65cd7a10ec7b75c021bd733284e9109ce32513b029951c12e89773c94ecafb5b

Contents?: true

Size: 1.44 KB

Versions: 32

Compression:

Stored size: 1.44 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

32 entries across 30 versions & 8 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
candlepin-api-0.4.0 bundle/ruby/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
librarian-puppet-0.9.8 vendor/gems/ruby/1.9.1/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
casecumber-1.2.1.cb2 lib/cucumber/rb_support/rb_transform.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/cucumber-1.2.1/lib/cucumber/rb_support/rb_transform.rb
cucumber-1.2.1 lib/cucumber/rb_support/rb_transform.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
ftl-0.2.0 vendor/bundle/gems/cucumber-1.2.0/lib/cucumber/rb_support/rb_transform.rb
cucumber-1.2.0 lib/cucumber/rb_support/rb_transform.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
radiant-1.0.0 ruby-debug/ruby/1.8/gems/cucumber-1.1.9/lib/cucumber/rb_support/rb_transform.rb
cucumber-1.1.9 lib/cucumber/rb_support/rb_transform.rb