81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/extend_it/callbacks.rb', line 81
def define_callbacks(*names, callbacks: [:before, :after])
callbacks = [:before, :after] unless callbacks.is_a?(Array)
callbacks = callbacks.ensure_array(values: CALLBACKS)
names = names.ensure_array(:flatten, :ensure_symbol, :compact, :uniq)
names.each do |name|
callbacks.each do |cb|
cb_name = "#{cb}_#{name}".to_sym
var_name = "@#{cb_name}".to_sym
define_singleton_method cb_name do |&block|
return if block.nil?
arr =
if instance_variable_defined?(var_name)
instance_variable_get(var_name)
else
instance_variable_set(var_name, [])
end
arr << block
end
end
end
end
|