Module: Roda::RodaPlugins::Hooks::ClassMethods
- Defined in:
- lib/roda/plugins/hooks.rb
Instance Method Summary collapse
-
#after(&block) ⇒ Object
Add an after hook.
-
#before(&block) ⇒ Object
Add a before hook.
-
#inherited(subclass) ⇒ Object
Copy the before and after hooks into the subclasses when inheriting.
Instance Method Details
#after(&block) ⇒ Object
Add an after hook. If there is already an after hook defined, use a proc that instance_execs the existing after proc and then instance_execs the given after proc, so that the given after proc always executes after the previous one.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/roda/plugins/hooks.rb', line 44 def after(&block) if block @after = if b = @after @after = proc do |res| instance_exec(res, &b) instance_exec(res, &block) end else block end end @after end |
#before(&block) ⇒ Object
Add a before hook. If there is already a before hook defined, use a proc that instance_execs the give before proc and then instance_execs the existing before proc, so that the given before proc always executes before the previous one.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/roda/plugins/hooks.rb', line 62 def before(&block) if block @before = if b = @before @before = proc do instance_exec(&block) instance_exec(&b) end else block end end @before end |
#inherited(subclass) ⇒ Object
Copy the before and after hooks into the subclasses when inheriting
78 79 80 81 82 |
# File 'lib/roda/plugins/hooks.rb', line 78 def inherited(subclass) super subclass.instance_variable_set(:@before, @before) subclass.instance_variable_set(:@after, @after) end |