Module: Verifly::DependentCallbacks

Extended by:
HasLogger
Includes:
Storage
Defined in:
lib/verifly/dependent_callbacks.rb,
lib/verifly/dependent_callbacks/invoker.rb,
lib/verifly/dependent_callbacks/service.rb,
lib/verifly/dependent_callbacks/storage.rb,
lib/verifly/dependent_callbacks/callback.rb,
lib/verifly/dependent_callbacks/callback_group.rb

Overview

DependentCallbacks interface is similar to ActiveSupport::Callbacks, but it has few differences

1) ‘extend` it, not `include`

2) Use ‘.callback_groups` do tefine callbacks instead of define_callbacks

3) Better define callbacks in separate module, wich should extend DependentCallbacks::Storage

4) Use merge_callbacks_from(Module) instead of including it

5) There is no run_callbacks method.

You can either use self.class.dependent_callbacks.invoke(group, *context) {} or use .export_callbacks_to(:active_support) / .export_callbacks_to(:wrap_method)

Defined Under Namespace

Modules: Storage Classes: Callback, CallbackGroup, Service

Instance Attribute Summary

Attributes included from HasLogger

#logger

Instance Method Summary collapse

Methods included from Storage

#callback_groups, #dependent_callbacks_service, #merge_callbacks_from

Instance Method Details

#export_callbacks_to(target, groups: nil) ⇒ Object

Exports callbacks to another callback system / something like that

Parameters:

  • target (:active_support, :wrap_method)

    Target selection.

    • :active_support exports each group to correspoding ActiveSupport callback (via set_callback)

    • :wrap_method defines / redefines methods, named same as each group. If method was defined, on it’s call callbacks would run around its previous defenition. If not, callbacks would run around nothing

  • groups ([Symbol]) (defaults to: nil)

    arra of groups to export. Defaults to all groups



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/verifly/dependent_callbacks.rb', line 48

def export_callbacks_to(target, groups: nil)
  (groups || dependent_callbacks_service.group_names).each do |group|
    case target
    when :active_support then _export_callback_group_to_active_support(group)
    when :action_controller then _export_callback_group_to_action_controller(group)
    when :wrap_method then _export_callback_group_to_method_wapper(group)
    else
      raise "#{target.inspect} export target unavailable. " \
            "available targets are :active_support, :action_controller, :wrap_method"
    end
  end
end

#inherited(child) ⇒ Object

Allows children to inherit callbacks from parent

Parameters:

  • child (Class)


31
32
33
34
35
36
37
# File 'lib/verifly/dependent_callbacks.rb', line 31

def inherited(child)
  super

  child.instance_exec(dependent_callbacks_service) do |service|
    @dependent_callbacks_service = Service.new(service)
  end
end