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.
Instance Method Summary collapse
-
#after_create(method = nil, &block) ⇒ Object
Add a *after create* callback.
-
#after_destroy(method = nil, &block) ⇒ Object
Add a *after destroy* callback.
-
#after_find(method = nil, &block) ⇒ Object
Add a *after find* callback.
-
#after_save(method = nil, &block) ⇒ Object
Add a *after save* callback.
-
#after_update(method = nil, &block) ⇒ Object
Add a *after update* callback.
-
#before_create(method = nil, &block) ⇒ Object
Add a *before create* callback.
-
#before_destroy(method = nil, &block) ⇒ Object
Add a *before destroy* callback.
-
#before_find(method = nil, &block) ⇒ Object
Do not add a *before find* callback.
-
#before_save(method = nil, &block) ⇒ Object
Add a *before save* callback.
-
#before_update(method = nil, &block) ⇒ Object
Add a *before update* callback.
- #hooks ⇒ Object
-
#wrap_in_hooks(resource, *hooks) {|resource, resource.class| ... } ⇒ Object
Wrap a block between “before” and “after” hooks.
Instance Method Details
#after_create(method = nil, &block) ⇒ Object
Add a *after create* callback. Triggered after a resource is created.
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.
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.
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.
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.
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.
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.
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.
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.
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.
32 |
# File 'lib/her/model/hooks.rb', line 32 def before_update(method=nil, &block); set_hook(:before, :update, method || block); end |
#hooks ⇒ Object
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
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 |