Module: Her::Model::Hooks

Defined in:
lib/her/model/hooks.rb

Overview

Her supports hooks/callbacks that are triggered whenever resources are created, updated or destroyed.

Examples:

Defining a hook with a block

class User
  include Her::Model
  before_save { |resource| resource.internal_id = 42 }
end

Defining a hook with a method name

class User
  include Her::Model
  before_save :set_internal_id

  private
  def set_internal_id
    self.internal_id = 42
  end
end

Instance Method Summary collapse

Instance Method Details

#after_create(method = nil, &block) ⇒ Object

Add a *after create* callback. Triggered after a resource is created.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



47
# File 'lib/her/model/hooks.rb', line 47

def after_create(method=nil, &block); set_hook(:after, :create, method || block); end

#after_destroy(method = nil, &block) ⇒ Object

Add a *after destroy* callback. Triggered after a resource is destroyed.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



55
# File 'lib/her/model/hooks.rb', line 55

def after_destroy(method=nil, &block); set_hook(:after, :destroy, method || block); end

#after_find(method = nil, &block) ⇒ Object

Add a *after find* callback. Triggered after a resource is found.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



59
# File 'lib/her/model/hooks.rb', line 59

def after_find(method=nil, &block); set_hook(:after, :find, method || block); end

#after_save(method = nil, &block) ⇒ Object

Add a *after save* callback. Triggered after a resource is created or updated.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



43
# File 'lib/her/model/hooks.rb', line 43

def after_save(method=nil, &block); set_hook(:after, :save, method || block); end

#after_update(method = nil, &block) ⇒ Object

Add a *after update* callback. Triggered after a resource is updated.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



51
# File 'lib/her/model/hooks.rb', line 51

def after_update(method=nil, &block); set_hook(:after, :update, method || block); end

#before_create(method = nil, &block) ⇒ Object

Add a *before create* callback. Triggered before a resource is created.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



28
# File 'lib/her/model/hooks.rb', line 28

def before_create(method=nil, &block); set_hook(:before, :create, method || block); end

#before_destroy(method = nil, &block) ⇒ Object

Add a *before destroy* callback. Triggered before a resource is destroyed.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



36
# File 'lib/her/model/hooks.rb', line 36

def before_destroy(method=nil, &block); set_hook(:before, :destroy, method || block); end

#before_find(method = nil, &block) ⇒ Object

Do not add a *before find* callback. Only *after find* is supported.

Raises:

  • (NoMethodError)


39
# File 'lib/her/model/hooks.rb', line 39

def before_find(method=nil, &block); raise NoMethodError, "undefined method `before_find' for #{self}"; end

#before_save(method = nil, &block) ⇒ Object

Add a *before save* callback. Triggered before a resource is created or updated.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



24
# File 'lib/her/model/hooks.rb', line 24

def before_save(method=nil, &block); set_hook(:before, :save, method || block); end

#before_update(method = nil, &block) ⇒ Object

Add a *before update* callback. Triggered before a resource is updated.

Parameters:

  • method (Symbol, &block) (defaults to: nil)

    A method or a block to be called



32
# File 'lib/her/model/hooks.rb', line 32

def before_update(method=nil, &block); set_hook(:before, :update, method || block); end

#hooksObject



70
71
72
73
74
75
76
77
78
# File 'lib/her/model/hooks.rb', line 70

def hooks
  @her_hooks ||= begin
    if superclass.respond_to?(:hooks)
      superclass.hooks.dup
    else
      {}
    end
  end
end

#wrap_in_hooks(resource, *hooks) {|resource, resource.class| ... } ⇒ Object

Wrap a block between “before” and “after” hooks

Yields:

  • (resource, resource.class)


63
64
65
66
67
# File 'lib/her/model/hooks.rb', line 63

def wrap_in_hooks(resource, *hooks)
  perform_before_hooks(resource, *hooks)
  yield(resource, resource.class) if block_given?
  perform_after_hooks(resource, *hooks.reverse)
end