-
Notifications
You must be signed in to change notification settings - Fork 58
/
method_missing_injection.rb
68 lines (62 loc) · 2.14 KB
/
method_missing_injection.rb
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
module RR
module Injections
class MethodMissingInjection < Injection
extend(Module.new do
def find_or_create(subject_class)
instances[subject_class] ||= begin
new(subject_class).bind
end
end
def exists?(subject)
instances.include?(subject)
end
end)
include ClassInstanceMethodDefined
attr_reader :subject_class
def initialize(subject_class)
@subject_class = subject_class
@placeholder_method_defined = false
end
def bind
unless class_instance_method_defined(subject_class, original_method_alias_name)
unless class_instance_method_defined(subject_class, :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
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
protected
def bind_method
subject_class_object_id = subject_class.object_id
subject_class.class_eval((<<-METHOD), __FILE__, __LINE__ + 1)
def method_missing(method_name, *args, &block)
MethodDispatches::MethodMissingDispatch.new(self, ObjectSpace._id2ref(#{subject_class_object_id}), method_name, args, block).call
end
METHOD
end
def original_method_alias_name
MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name
end
end
end
end