Module: ActiveService::Hooks::ClassMethods
- Defined in:
- lib/active_service/hooks.rb
Instance Method Summary collapse
- #add_hook(type, action, *args, &block) ⇒ Object
- #after(action, *args, &block) ⇒ Object
- #around(action, *args, &block) ⇒ Object
- #before(action, *args, &block) ⇒ Object
- #extract_options!(*args) ⇒ Object
- #run_after_hooks(obj, action) ⇒ Object
- #run_around_hooks(obj, action, &block) ⇒ Object
- #run_before_hooks(obj, action) ⇒ Object
- #run_hook(hook, obj, *args) ⇒ Object
Instance Method Details
#add_hook(type, action, *args, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/active_service/hooks.rb', line 23 def add_hook(type, action, *args, &block) = *args if args.first.is_a? Symbol block = lambda { |*ops| send(args.first, *ops) } end send("#{type}_hooks",action).push(block) end |
#after(action, *args, &block) ⇒ Object
39 40 41 |
# File 'lib/active_service/hooks.rb', line 39 def after(action, *args, &block) add_hook(:after, action, *args, &block) end |
#around(action, *args, &block) ⇒ Object
43 44 45 |
# File 'lib/active_service/hooks.rb', line 43 def around(action, *args, &block) add_hook(:around, action, *args, &block) end |
#before(action, *args, &block) ⇒ Object
35 36 37 |
# File 'lib/active_service/hooks.rb', line 35 def before(action, *args, &block) add_hook(:before, action, *args, &block) end |
#extract_options!(*args) ⇒ Object
65 66 67 |
# File 'lib/active_service/hooks.rb', line 65 def (*args) args.last.is_a?(::Hash) ? args.pop : {} end |
#run_after_hooks(obj, action) ⇒ Object
51 52 53 |
# File 'lib/active_service/hooks.rb', line 51 def run_after_hooks(obj, action) after_hooks(action).each { |h| run_hook(h, obj, action) } end |
#run_around_hooks(obj, action, &block) ⇒ Object
55 56 57 58 59 |
# File 'lib/active_service/hooks.rb', line 55 def run_around_hooks(obj, action, &block) around_hooks(action).inject(block) { |chain, hook| proc { run_hook(hook, obj, chain) } }.call end |
#run_before_hooks(obj, action) ⇒ Object
47 48 49 |
# File 'lib/active_service/hooks.rb', line 47 def run_before_hooks(obj, action) before_hooks(action).each { |h| run_hook(h, obj, action) } end |
#run_hook(hook, obj, *args) ⇒ Object
61 62 63 |
# File 'lib/active_service/hooks.rb', line 61 def run_hook(hook, obj, *args) obj.instance_exec(*args, &hook) end |