Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/cucumber/core_ext/instance_exec.rb

Defined Under Namespace

Modules: InstanceExecHelper

Instance Method Summary collapse

Instance Method Details

#cucumber_arity(block) ⇒ Object



25
26
27
28
# File 'lib/cucumber/core_ext/instance_exec.rb', line 25

def cucumber_arity(block)
  a = block.arity
  Cucumber::RUBY_1_9 ? a : (a == -1 ? 0 : a)
end

#cucumber_compatible_arity?(args, block) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/cucumber/core_ext/instance_exec.rb', line 30

def cucumber_compatible_arity?(args, block)
  a = cucumber_arity(block)
  return true if (a == -1) && Cucumber::RUBY_1_9
  a == args.length
end

#cucumber_instance_exec(check_arity, pseudo_method, *args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cucumber/core_ext/instance_exec.rb', line 9

def cucumber_instance_exec(check_arity, pseudo_method, *args, &block)
  cucumber_run_with_backtrace_filtering(pseudo_method) do
    if check_arity && !cucumber_compatible_arity?(args, block)
      instance_exec do
        s1 = cucumber_arity(block) == 1 ? "" : "s"
        s2 = args.length == 1 ? "" : "s"
        raise Cucumber::ArityMismatchError.new(
          "Your block takes #{cucumber_arity(block)} argument#{s1}, but the Regexp matched #{args.length} argument#{s2}."
        )
      end
    else
      instance_exec(*args, &block)
    end
  end
end

#cucumber_run_with_backtrace_filtering(pseudo_method) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/cucumber/core_ext/instance_exec.rb', line 36

def cucumber_run_with_backtrace_filtering(pseudo_method)
  begin
    yield
  rescue Exception => e
    instance_exec_invocation_line = "#{__FILE__}:#{__LINE__ - 2}:in `cucumber_run_with_backtrace_filtering'"
    Exception.cucumber_strip_backtrace!((e.backtrace || []), instance_exec_invocation_line, pseudo_method)
    raise e
  end
end

#instance_exec(*args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cucumber/core_ext/instance_exec.rb', line 50

def instance_exec(*args, &block)
  begin
    old_critical, Thread.critical = Thread.critical, true
    n = 0
    n += 1 while respond_to?(mname="__instance_exec#{n}")
    InstanceExecHelper.module_eval{ define_method(mname, &block) }
  ensure
    Thread.critical = old_critical
  end
  begin
    ret = send(mname, *args)
  ensure
    InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
  end
  ret
end