Class: RR::Injections::MethodMissingInjection

Inherits:
Injection
  • Object
show all
Defined in:
lib/rr/injections/method_missing_injection.rb

Instance Attribute Summary

Attributes inherited from Injection

#subject

Instance Method Summary collapse

Methods inherited from Injection

#subject_has_method_defined?, #subject_has_original_method?

Methods included from Space::Reader

#space

Constructor Details

#initialize(subject) ⇒ MethodMissingInjection

Returns a new instance of MethodMissingInjection.



4
5
6
7
# File 'lib/rr/injections/method_missing_injection.rb', line 4

def initialize(subject)
  @subject = subject
  @placeholder_method_defined = false
end

Instance Method Details

#bindObject



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

def bind
  unless subject.respond_to?(original_method_alias_name)
    unless subject.respond_to?(:method_missing)
      @placeholder_method_defined = true
      subject_class.class_eval do
        def method_missing(method_name, *args, &block)
          super
        end
      end
    end
    subject_class.__send__(:alias_method, original_method_alias_name, :method_missing)
    bind_method
  end
  self
end

#dispatch_method(method_name, args, block) ⇒ Object



39
40
41
# File 'lib/rr/injections/method_missing_injection.rb', line 39

def dispatch_method(method_name, args, block)
  MethodDispatches::MethodMissingDispatch.new(subject, method_name, args, block).call
end

#resetObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rr/injections/method_missing_injection.rb', line 25

def reset
  if subject_has_method_defined?(original_method_alias_name)
    memoized_original_method_alias_name = original_method_alias_name
    placeholder_method_defined = @placeholder_method_defined
    subject_class.class_eval do
      remove_method :method_missing
      unless placeholder_method_defined
        alias_method :method_missing, memoized_original_method_alias_name
      end
      remove_method memoized_original_method_alias_name
    end
  end
end