Sha256: d2bc53b396c95910c4ae7bd425ac6f1bdfba17a418b6a0f45733d40acbdb7bf0

Contents?: true

Size: 1.33 KB

Versions: 13

Compression:

Stored size: 1.33 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
        strip_captures(strip_anchors(@regexp.source))
      end
    
    private

      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

13 entries across 13 versions & 3 rubygems

Version Path
casecumber-1.0.2.1 lib/cucumber/rb_support/rb_transform.rb
js-log-cucumber-1.0.2 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.0.3 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.0.2 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.0.1 lib/cucumber/rb_support/rb_transform.rb
cucumber-1.0.0 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.8.6 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.7 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.6 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.5 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.3 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.2 lib/cucumber/rb_support/rb_transform.rb
cucumber-0.10.1 lib/cucumber/rb_support/rb_transform.rb