module Sequel::Plugins::InstanceHooks::InstanceMethods

Public Instance Methods

after_destroy() click to toggle source

Run after destroy instance hooks.

Calls superclass method
# File lib/sequel/plugins/instance_hooks.rb, line 65
def after_destroy
  super
  return unless @instance_hooks
  if ad = @instance_hooks[:after_destroy_commit]
    db.after_commit{ad.each(&:call)}
  end
  run_after_instance_hooks(:after_destroy)
  @instance_hooks.delete(:after_destroy)
  @instance_hooks.delete(:before_destroy)
  @instance_hooks.delete(:after_destroy_commit)
  @instance_hooks.delete(:after_destroy_rollback)
end
after_save() click to toggle source

Run after save instance hooks.

Calls superclass method
# File lib/sequel/plugins/instance_hooks.rb, line 86
def after_save
  super
  return unless @instance_hooks
  if (ac = @instance_hooks[:after_commit])
    db.after_commit{ac.each(&:call)}
  end
  run_after_instance_hooks(:after_save)
  @instance_hooks.delete(:after_save)
  @instance_hooks.delete(:before_save)
  @instance_hooks.delete(:after_validation)
  @instance_hooks.delete(:before_validation)
  @instance_hooks.delete(:after_commit)
  @instance_hooks.delete(:after_rollback)
end
after_validation() click to toggle source

Run after validation instance hooks.

Calls superclass method
# File lib/sequel/plugins/instance_hooks.rb, line 79
def after_validation
  super
  return unless @instance_hooks
  run_after_instance_hooks(:after_validation)
end
before_destroy() click to toggle source

Run #before_destroy instance hooks.

Calls superclass method
# File lib/sequel/plugins/instance_hooks.rb, line 102
def before_destroy
  return super unless @instance_hooks
  if adr = @instance_hooks[:after_destroy_rollback]
    db.after_rollback{adr.each(&:call)}
  end
  run_before_instance_hooks(:before_destroy) == false ? false : super
end
before_save() click to toggle source

Run #before_save instance hooks.

Calls superclass method
# File lib/sequel/plugins/instance_hooks.rb, line 111
def before_save
  return super unless @instance_hooks
  if ar = @instance_hooks[:after_rollback]
    db.after_rollback{ar.each(&:call)}
  end
  run_before_instance_hooks(:before_save) == false ? false : super
end

Private Instance Methods

add_instance_hook(hook, &block) click to toggle source

Add the block as an instance level hook. For before hooks, add it to the beginning of the instance hook's array. For after hooks, add it to the end.

# File lib/sequel/plugins/instance_hooks.rb, line 124
def add_instance_hook(hook, &block)
  instance_hooks(hook).send(BEFORE_HOOKS.include?(hook) ? :unshift : :push, block)
end
instance_hooks(hook) click to toggle source

An array of instance level hook blocks for the given hook type.

# File lib/sequel/plugins/instance_hooks.rb, line 129
def instance_hooks(hook)
  @instance_hooks ||= {}
  @instance_hooks[hook] ||= []
end
run_after_instance_hooks(hook) click to toggle source

Run all hook blocks of the given hook type.

# File lib/sequel/plugins/instance_hooks.rb, line 135
def run_after_instance_hooks(hook)
  instance_hooks(hook).each(&:call)
end
run_before_instance_hooks(hook) click to toggle source

Run all hook blocks of the given hook type. If a hook block returns false, immediately return false without running the remaining blocks.

# File lib/sequel/plugins/instance_hooks.rb, line 141
def run_before_instance_hooks(hook)
  instance_hooks(hook).each do |b|
    if b.call == false
      Sequel::Deprecation.deprecate("Having #{hook} instance hook block return false to stop evaluation of further #{hook} instance hook blocks", "Instead, call cancel_action inside #{hook} instance hook block")
      return false
    end
  end
end