Sha256: e4cd0a11060989f76c40308954426135483994a39bb1c2c70b5fc8b5515005bb

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 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
      attr_accessor :name
      
      def call_in(obj, *args)
        obj.extend(mod)
        a = arity == -1 ? 0 : arity
        if self != StepMother::PENDING && args.length != a
          # We have to manually raise when the block has arity -1 (no pipes)
          raise ArityMismatchError.new("expected #{arity == -1 ? 0 : arity} block argument(s), got #{args.length}")
        else
          obj.__send__(meth, *args)
        end
      end

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

      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(/[\d\w]+@(.*):(.*)>/)[1..2]
        path = File.expand_path(path)
        pwd = Dir.pwd
        path = path[pwd.length+1..-1]        
        "#{path}:#{line}"
      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

2 entries across 2 versions & 2 rubygems

Version Path
elight-cucumber-0.1.9 lib/cucumber/core_ext/proc.rb
cucumber-0.1.8 lib/cucumber/core_ext/proc.rb