module ServiceOperation::Hooks

Simple hooks mechanism with inheritance.

Public Class Methods

included(base) click to toggle source
# File lib/service_operation/hooks.rb, line 6
def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Private Instance Methods

run_after_hooks() click to toggle source
# File lib/service_operation/hooks.rb, line 82
def run_after_hooks
  run_hooks(self.class.after_hooks)
end
run_around_hooks(&block) click to toggle source
# File lib/service_operation/hooks.rb, line 72
def run_around_hooks(&block)
  self.class.around_hooks.reverse.inject(block) do |chain, hook|
    proc { run_hook(hook, chain) }
  end.call
end
run_before_hooks() click to toggle source
# File lib/service_operation/hooks.rb, line 78
def run_before_hooks
  run_hooks(self.class.before_hooks)
end
run_hook(hook, *args) click to toggle source

@param [Symbol, Proc] - name of a method defined in the operation or a block

# File lib/service_operation/hooks.rb, line 91
def run_hook(hook, *args)
  return if hook.class.name =~ /Delayed::Backend/ # prevent a clash with delayed_job gem.

  hook.is_a?(Symbol) ? send(hook, *args) : instance_exec(*args, &hook)
end
run_hooks(hooks) click to toggle source
# File lib/service_operation/hooks.rb, line 86
def run_hooks(hooks)
  hooks.each { |hook| run_hook(hook) }
end
with_hooks() { || ... } click to toggle source
# File lib/service_operation/hooks.rb, line 64
def with_hooks
  run_around_hooks do
    run_before_hooks
    yield
    run_after_hooks
  end
end