Sha256: cf79d4692534f286244208b1a27f2397c4a75d453dd9886745085449403f886f

Contents?: true

Size: 1.67 KB

Versions: 30

Compression:

Stored size: 1.67 KB

Contents

module Cucumber
  class ArityMismatchError < StandardError
  end

  module CoreExt
    # Proc extension that allows a proc to be called in the context of any object.
    # Also makes it possible to tack a name onto a Proc.
    module CallIn
      PROC_PATTERN = /[\d\w]+@(.*):(.*)>/
      
      if Proc.new{}.to_s =~ PROC_PATTERN
        def to_backtrace_line
          "#{file_colon_line}:in `#{name}'"
        end

        def to_comment_line
          "# #{file_colon_line}"
        end

        def file_colon_line
          path, line = *to_s.match(PROC_PATTERN)[1..2]
          path = File.expand_path(path)
          pwd = Dir.pwd
          path = path[pwd.length+1..-1]
          "#{path}:#{line}"
        end
      else
        # This Ruby implementation doesn't implement Proc#to_s correctly
        STDERR.puts "*** THIS RUBY IMPLEMENTATION DOESN'T REPORT FILE AND LINE FOR PROCS ***"
        
        def to_backtrace_line
          nil
        end

        def to_comment_line
          ""
        end
      end
      
      attr_accessor :name
      
      def call_in(obj, *args)
        obj.extend(mod)
        if self != StepMother::PENDING && args.length != arity2
          # We have to manually raise when the block has arity -1 (no pipes)
          raise ArityMismatchError.new("expected #{arity2} block argument(s), got #{args.length}")
        else
          obj.__send__(meth, *args)
        end
      end

      def arity2
        arity == -1 ? 0 : arity
      end

      def meth
        @meth ||= "__cucumber_#{object_id}"
      end

      def mod
        p = self
        m = meth
        @mod ||= Module.new do
          define_method(m, &p)
        end
      end
    end 
  end
end

Version data entries

30 entries across 30 versions & 6 rubygems

Version Path
aslakhellesoy-cucumber-0.1.10 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.11 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.12 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.13.2 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.13.3 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.13 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.14.1 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.14.2 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.15 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.16.1 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.16.2 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.16.3 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.16.4 lib/cucumber/core_ext/proc.rb
aslakhellesoy-cucumber-0.1.16 lib/cucumber/core_ext/proc.rb
bts-cucumber-0.1.13.1 lib/cucumber/core_ext/proc.rb
bts-cucumber-0.1.13.2 lib/cucumber/core_ext/proc.rb
bts-cucumber-0.1.13.3 lib/cucumber/core_ext/proc.rb
jeffrafter-cucumber-0.1.10 lib/cucumber/core_ext/proc.rb
jeffrafter-cucumber-0.1.12 lib/cucumber/core_ext/proc.rb
kosmas58-cucumber-0.1.16.5 lib/cucumber/core_ext/proc.rb