-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptain_hook.rb
195 lines (160 loc) · 5.98 KB
/
captain_hook.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true
require_relative "./captain_hook/configuration"
# Strong inspiration on https://github.com/collectiveidea/interactor/blob/master/lib/interactor/hooks.rb
#
# Hook module, responsible to add the before, after and around callbacks on the
# public methods of the class which requires it.
module CaptainHook
# Install Hook methods in the including class.
def self.included(base)
base.class_eval do
extend ClassMethods
end
end
class << self
attr_accessor :cache_key
end
self.cache_key = 0
# Class methods for the including class.
module ClassMethods
# Main hook configuartion entrypoint, DSL
# Examples:
# hook :before, hook: CookHook.new, include: [:cook], inject: [:policy_context]
# hook :before, hook: ErroringHook.new
# hook :around, hook: ServeHook.new
def hook(
kind,
hook:,
include: [],
inject: [],
exclude: [],
skip_when: nil,
param_builder: nil
)
CaptainHook.cache_key += 1
hooks[kind][hook] = Configuration.new(
hook: hook,
include: include,
inject: inject,
exclude: exclude,
skip_when: skip_when,
param_builder: param_builder
)
end
####
# Hooks logic part
####
def get_hooks(kind)
if @_captain_hook_cache_key != CaptainHook.cache_key
@_captain_hooks = {}
@_captain_hook_cache_key = CaptainHook.cache_key
end
@_captain_hooks[kind] ||=
ancestors.each_with_object({}) do |klass, hook_list|
next unless klass.respond_to?(:hooks)
klass.hooks[kind]&.each_value do |hook|
hook_list[hook.hook.class] ||= hook
end
end.values
end
def hooks
@hooks ||= { before: {}, after: {}, around: {} }
end
####
# Decorator pattern logic part
####
def overriden?(method)
overriden_methods.include? method
end
def method_excluded_by_all_hooks?(method)
get_hooks(:before).all? { |hook| hook.exclude.include?(method) } &&
get_hooks(:after).all? { |hook| hook.exclude.include?(method) } &&
get_hooks(:around).all? { |hook| hook.exclude.include?(method) }
end
def method_added(method_name)
unless !public_method_defined?(method_name) || overriden?(method_name) ||
method_name.to_s.end_with?("__without_hooks")
decorate_method!(method_name)
end
super
end
def overriden_methods
@overriden_methods ||= Set.new
end
def mark_as_overriden!(method)
overriden_methods << method
end
# Replaces the method with a decorated version of it
def decorate_method!(method_name)
mark_as_overriden!(method_name)
original_method_name = :"#{method_name}__without_hooks"
# Skip if the method is excluded by all hooks
return if method_excluded_by_all_hooks?(method_name)
alias_method original_method_name, method_name
define_method(method_name) do |*args, **kwargs|
hook_args = args
hook_kwargs = kwargs
around_body = lambda do
# Run before hooks
before_hook_result = run_hooks(method_name, self.class.get_hooks(:before), *hook_args, **hook_kwargs)
return before_hook_result if hook_error?(before_hook_result)
# Supporting any kind of method, without arguments, with positional
# or with named parameters. Or any combination of them.
result = if args.empty? && kwargs.empty?
send(original_method_name)
elsif args.empty?
send(original_method_name, **kwargs)
elsif kwargs.empty?
if args.length == 1 && args[0].is_a?(Hash)
send(original_method_name, **args[0])
else
send(original_method_name, *args)
end
else
send(original_method_name, *args, **kwargs)
end
# Run after hooks
run_hooks(method_name, self.class.get_hooks(:after), *hook_args, **hook_kwargs)
result
end
result = run_around_hooks(method_name, *hook_args, **hook_kwargs, &around_body)
result
end
end
end
def prepare_hook_params(method, hook_configuration, args, kwargs)
return hook_configuration.param_builder.call(self, method, args, kwargs) if hook_configuration.param_builder
[args, kwargs]
end
# Around hooks are a bit different from before and after hooks, as they
# need to be executed in a stack-like way.
def run_around_hooks(method, *args, **kwargs, &block)
self.class.get_hooks(:around).to_a.reverse.inject(block) do |chain, hook_configuration|
hook_args, hook_kwargs = prepare_hook_params(method, hook_configuration, args, kwargs)
next chain if hook_configuration.skip?(method, *hook_args, **hook_kwargs)
proc {
run_hook(method, hook_configuration, chain, *hook_args, **hook_kwargs)
}
end.call
end
# Runs non-around hooks for a given method.
def run_hooks(method, hooks, *args, **kwargs)
hooks.each do |hook_configuration|
hook_args, hook_kwargs = prepare_hook_params(method, hook_configuration, args, kwargs)
next if hook_configuration.skip?(method, *hook_args, **hook_kwargs)
body = run_hook(method, hook_configuration, -> {}, *hook_args, **hook_kwargs)
return body if hook_error?(body)
end
end
# Runs an specific hook based on its configuration
def run_hook(method, hook_configuration, chain, *args, **kwargs)
# puts "Running hook: #{hook_configuration.hook.class.name} with method: #{self.class.name}##{method}"
hook_configuration.hook.call(self, method, *args, **kwargs, &chain)
rescue ArgumentError => e
puts "Argument error running hook: #{hook_configuration.hook.class.name} with method: #{method}, args: #{args}, kwargs: #{kwargs}"
raise e
end
def hook_error?(result)
result.respond_to?(:error?) && result.error?
end
end