File: method_dispatch.rb

package info (click to toggle)
ruby-rr 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,424 kB
  • sloc: ruby: 11,405; makefile: 7
file content (71 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module RR
  module MethodDispatches
    class MethodDispatch < BaseMethodDispatch
      attr_reader :double_injection, :subject

      def initialize(double_injection, subject, args, kwargs, block)
        @double_injection = double_injection
        @subject = subject
        @args = args
        @kwargs = kwargs
        @block = block
        @double = find_double_to_attempt
      end

      def call
        space.record_call(subject, method_name, args, kwargs, block)
        if double
          double.method_call(args, kwargs)
          call_yields
          return_value_1 = call_implementation
          return_value_2 = extract_subject_from_return_value(return_value_1)
          if after_call_proc
            extract_subject_from_return_value(after_call_proc.call(return_value_2))
          else
            return_value_2
          end
        else
          double_not_found_error
        end
      end

      def call_original_method
        if subject_has_original_method?
          if KeywordArguments.fully_supported?
            subject.__send__(original_method_alias_name, *args, **kwargs, &block)
          else
            subject.__send__(original_method_alias_name, *args, &block)
          end
        elsif subject_has_original_method_missing?
          call_original_method_missing
        else
          if KeywordArguments.fully_supported?
            subject.__send__(:method_missing, method_name, *args, **kwargs, &block)
          else
            subject.__send__(:method_missing, method_name, *args, &block)
          end
        end
      end

    protected
      def call_implementation
        if implementation_is_original_method?
          call_original_method
        else
          if implementation
            if KeywordArguments.fully_supported?
              implementation.call(*args, **kwargs, &block)
            else
              implementation.call(*args, &block)
            end
          else
            nil
          end
        end
      end

      def_delegators :definition, :implementation
      def_delegators :double_injection, :subject_has_original_method?, :subject_has_original_method_missing?, :method_name, :original_method_alias_name
    end
  end
end