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
|
# File 'lib/deprecate_soft/method_wrapper.rb', line 5
def self.wrap_method(context, method_name, message, is_class_method:)
hidden_method_name = "#{DeprecateSoft.prefix}#{method_name}_#{DeprecateSoft.suffix}"
if is_class_method
target = context.singleton_class
return if target.method_defined?(hidden_method_name) || target.private_method_defined?(hidden_method_name)
original_method = context.method(method_name)
target.define_method(hidden_method_name, original_method)
target.define_method(method_name) do |*args, &block|
klass_name = self.class.name || self.class.to_s
full_name = "#{klass_name}.#{method_name}"
begin
DeprecateSoft.before_hook&.call(full_name, message, args: args)
rescue StandardError => e
warn "DeprecateSoft.before_hook error: #{e.class} - #{e.message}"
end
result = send(hidden_method_name, *args, &block)
begin
DeprecateSoft.after_hook&.call(full_name, message, result: result)
rescue StandardError => e
warn "DeprecateSoft.after_hook error: #{e.class} - #{e.message}"
end
result
end
else
return if context.method_defined?(hidden_method_name) || context.private_method_defined?(hidden_method_name)
original_method = context.instance_method(method_name)
context.define_method(hidden_method_name, original_method)
context.define_method(method_name) do |*args, &block|
klass_name = self.class.name || self.to_s
full_name = "#{klass_name}.#{method_name}"
begin
DeprecateSoft.before_hook&.call(full_name, message, args: args)
rescue StandardError => e
warn "DeprecateSoft.before_hook error: #{e.class} - #{e.message}"
end
result = send(hidden_method_name, *args, &block)
begin
DeprecateSoft.after_hook&.call(full_name, message, result: result)
rescue StandardError => e
warn "DeprecateSoft.after_hook error: #{e.class} - #{e.message}"
end
result
end
end
end
|