Sha256: 41ec12f152079855f0a099931fe380054dae82815e4b9f6e5b7c3df7b1162e03

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Spec
  module Story
    class Step
      PARAM_PATTERN = /(\$\w*)/
      PARAM_OR_GROUP_PATTERN = /(\$\w*)|\(.*?\)/
      
      attr_reader :name
      def initialize(name, &block)
        @name = name
        assign_expression(name)
        init_module(name, &block)
      end

      def perform(instance, *args)
        instance.extend(@mod)
        instance.__send__(sanitize(@name), *args)
      end

      def init_module(name, &block)
        sanitized_name = sanitize(name)
        @mod = Module.new do
          define_method(sanitized_name, &block)
        end
      end
      
      def sanitize(a_string_or_regexp)
        return a_string_or_regexp.source if Regexp == a_string_or_regexp
        a_string_or_regexp.to_s
      end
      

      def matches?(name)
        !(matches = name.match(@expression)).nil?
      end
            
      def parse_args(name)
        name.match(@expression)[1..-1]
      end

      private
      
        def assign_expression(name)
          expression = name.dup
          if String === expression
            expression.gsub! '(', '\('
            expression.gsub! ')', '\)'
            while expression =~ PARAM_PATTERN
              expression.gsub!($1, "(.*?)")
            end
          end
          @expression = Regexp.new("^#{expression}$")
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-1.1.2 lib/spec/story/step.rb