Module: EventHooks
- Defined in:
- lib/event_hooks/errors.rb,
lib/event_hooks/version.rb,
lib/event_hooks/event_hooks.rb
Defined Under Namespace
Modules: ClassMethods Classes: DoubleHook
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#hook_before(event, hook) ⇒ Object
prepend the hook to the event event won’t run unless hook returns a true-ish value.
Class Method Details
.included(base) ⇒ Object
:nodoc:
4 5 6 7 |
# File 'lib/event_hooks/event_hooks.rb', line 4 def self.included(base) #:nodoc: #base.extend EventHooks::ClassMethods super end |
Instance Method Details
#hook_before(event, hook) ⇒ Object
prepend the hook to the event event won’t run unless hook returns a true-ish value
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/event_hooks/event_hooks.rb', line 11 def hook_before(event, hook) raise EventHooks::DoubleHook.new if instance_methods.include?("#{event}_without_before_hook".to_sym) alias_method "#{event}_without_before_hook".to_sym, event define_method "#{event}_with_before_hook".to_sym do |*args| send(hook) && send("#{event}_without_before_hook".to_sym, *args) end alias_method event, "#{event}_with_before_hook".to_sym end |