Module: Roda::RodaPlugins::Hooks::ClassMethods

Defined in:
lib/roda/plugins/hooks.rb

Instance Method Summary collapse

Instance Method Details

#after(&block) ⇒ Object

Add an after hook.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/roda/plugins/hooks.rb', line 51

def after(&block)
  opts[:after_hooks] << define_roda_method("after_hook", 1, &block)
  if opts[:after_hooks].length == 1
    class_eval("alias _roda_after_80__hooks #{opts[:after_hooks].first}", __FILE__, __LINE__)
  else
    class_eval("def _roda_after_80__hooks(res) #{opts[:after_hooks].map{|m| "#{m}(res)"}.join(';')} end", __FILE__, __LINE__)
  end
  private :_roda_after_80__hooks
  def_roda_after
  nil
end

#before(&block) ⇒ Object

Add a before hook.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roda/plugins/hooks.rb', line 64

def before(&block)
  opts[:before_hooks].unshift(define_roda_method("before_hook", 0, &block))
  if opts[:before_hooks].length == 1
    class_eval("alias _roda_before_10__hooks #{opts[:before_hooks].first}", __FILE__, __LINE__)
  else
    class_eval("def _roda_before_10__hooks; #{opts[:before_hooks].join(';')} end", __FILE__, __LINE__)
  end
  private :_roda_before_10__hooks
  def_roda_before
  nil
end

#freezeObject

Freeze the array of hook methods when freezing the app.



43
44
45
46
47
48
# File 'lib/roda/plugins/hooks.rb', line 43

def freeze
  opts[:after_hooks].freeze
  opts[:before_hooks].freeze

  super
end