Module: ActiveFunctionCore::Plugins::Hooks::ClassMethods

Defined in:
lib/active_function_core/plugins/hooks.rb

Overview

Instance Method Summary collapse

Instance Method Details

#callback_optionsObject

Returns all setuped custom callback options for the class.



127
# File 'lib/active_function_core/plugins/hooks.rb', line 127

def callback_options = @__callback_options ||= 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.

Examples:

define_hooks_for :process, name: :action
before_action :set_first
after_action :set_last, if: :ok?

Parameters:

  • method (Symbol)

    the name of the callbackable method.

  • name (Symbol) (defaults to: method)

    alias for hooked method before_[name] & after_[name] methods.

Raises:

  • (ArgumentError)

    if hook for method already defined.

  • (ArgumentError)

    if method is not defined.



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, options = {}|
    set_callback(:before, name, target, options)
  end

  define_singleton_method(:"after_#{name}") do |target, options = {}|
    set_callback(:after, name, target, options)
  end

  define_method(method) do |*args, &block|
    self.class.hooks[name].run_callbacks(self) do
      super(*args, &block)
    end
  end
end

#hooksObject

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, callback_options.dup)
end

#set_callback(type, method_name, target, options = {}) ⇒ Object

Sets a callback for an existing hook’ed method.

Examples:

define_hooks_for :action
set_callback :before, :action, :set_first

Parameters:

  • type (Symbol)

    the type of callback, :before or :after

  • method_name (Symbol)

    the name of the callbackable method.

  • target (Symbol)

    the name of the callback method.

  • options (Hash) (defaults to: {})

    the options for the callback.

Options Hash (options):

  • :if (Symbol)

    the name of the booled method to call before executing the callback.

  • :unless (Symbol)

    the name of the booled method to call before executing the callback.

Raises:

  • (ArgumentError)

    if hook for provided method is not setuped via ::define_hooks_for.

  • (ArgumentError)

    if unsupported @options was passed.



183
184
185
186
187
188
# File 'lib/active_function_core/plugins/hooks.rb', line 183

def set_callback(type, method_name, target, options = {})
  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 (options.keys - callback_options.keys).any?

  hooks[method_name].add_callback(type:, target:, options:)
end

#set_callback_options(option) {|*attrs, context:| ... } ⇒ Object

Sets a custom callback option.

Examples:

set_callback_option only: ->(args, context:) { args.to_set === context.current_action }
define_hooks_for :action
before_action :set_first, only: :index

Parameters:

  • option (Hash{Symbol => Proc})

    The custom callback option as a single-value hash.

    • :name [Symbol] The name of the option.

    • :block [Proc] The block to call.

Yields:

  • (*attrs, context:)

    the block to call.

Yield Parameters:

  • attrs (*)

    the attributes passed to the option.

  • context (Object)

    the instance context (optional).

Yield Returns:

  • (Boolean)

    .



204
205
206
207
# File 'lib/active_function_core/plugins/hooks.rb', line 204

def set_callback_options(option)
  name, block            = option.first
  callback_options[name] = block
end