Module: ActiveFunctionCore::Plugins::Hooks::ClassMethods
- Defined in:
- lib/active_function_core/plugins/hooks.rb
Overview
DSL method for ActiveFunctionCore::Plugins::Hooks
Instance Method Summary collapse
-
#callback_options ⇒ Object
Returns all setuped custom callback options for the class.
-
#define_hooks_for(method, name: method) ⇒ Object
Setups hooks for provided method.
-
#hooks ⇒ Object
Returns all setuped hooks for the class.
-
#inherited(subclass) ⇒ Object
Inherited callback to ensure that callbacks are inherited from the base class.
-
#set_callback(type, method_name, target, options = {}) ⇒ Object
Sets a callback for an existing hook’ed method.
-
#set_callback_options(option) {|*attrs, context:| ... } ⇒ Object
Sets a custom callback option.
Instance Method Details
#callback_options ⇒ Object
Returns all setuped custom callback options for the class.
127 |
# File 'lib/active_function_core/plugins/hooks.rb', line 127 def = ||= Hook::DEFAULT_CALLBACK_OPTIONS.dup |
#define_hooks_for(method, name: method) ⇒ Object
Setups hooks for provided method. Redefines method providing callbacks calls around it. Defines before_[name] and after_[name] methods for setting callbacks.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_function_core/plugins/hooks.rb', line 148 def define_hooks_for(method, name: method) raise(ArgumentError, "Hook for #{method} are already defined") if hooks.key?(method) raise(ArgumentError, "Method #{method} is not defined") unless method_defined?(method) hooks[name] = Hook.new(name) define_singleton_method(:"before_#{name}") do |target, = {}| set_callback(:before, name, target, ) end define_singleton_method(:"after_#{name}") do |target, = {}| set_callback(:after, name, target, ) end define_method(method) do |*args, &block| self.class.hooks[name].run_callbacks(self) do super(*args, &block) end end end |
#hooks ⇒ Object
Returns all setuped hooks for the class.
124 |
# File 'lib/active_function_core/plugins/hooks.rb', line 124 def hooks = @__hooks ||= {} |
#inherited(subclass) ⇒ Object
Inherited callback to ensure that callbacks are inherited from the base class.
130 131 132 133 |
# File 'lib/active_function_core/plugins/hooks.rb', line 130 def inherited(subclass) subclass.instance_variable_set(:@__hooks, Marshal.load(Marshal.dump(hooks))) subclass.instance_variable_set(:@__callback_options, .dup) end |
#set_callback(type, method_name, target, options = {}) ⇒ Object
Sets a callback for an existing hook’ed method.
183 184 185 186 187 188 |
# File 'lib/active_function_core/plugins/hooks.rb', line 183 def set_callback(type, method_name, target, = {}) raise(ArgumentError, "Hook for :#{method_name} is not defined") unless hooks.key?(method_name) raise(ArgumentError, "Hook Callback accepts only #{callback_options.keys} options") if (.keys - .keys).any? hooks[method_name].add_callback(type:, target:, options:) end |
#set_callback_options(option) {|*attrs, context:| ... } ⇒ Object
Sets a custom callback option.
204 205 206 207 |
# File 'lib/active_function_core/plugins/hooks.rb', line 204 def (option) name, block = option.first [name] = block end |