Module: Filters::ClassMethods

Defined in:
lib/filt.rb

Instance Method Summary collapse

Instance Method Details

#add_global_hook(method) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/filt.rb', line 10

def add_global_hook(method)
  alias_method "__#{method}", method
  define_method method do |*args,&block|
    @@before_hooks ||= {}
    @@after_hooks  ||= {}
    @@before_hooks.keys.each do |hook|
      unless (@@before_hooks[hook][:except]||[]).include?(method) || (@@before_hooks[hook][:only] && !@@before_hooks[hook][:only].include?(method))
        return unless send(hook)
      end
    end unless @@before_hooks[method] || @@after_hooks[method]
    returnable = send("__#{method}",*args,&block)
    @@after_hooks.keys.each do |hook|
      unless (@@after_hooks[hook][:except]||[]).include?(method) || (@@after_hooks[hook][:only] && !@@after_hooks[hook][:only].include?(method))
        return unless send(hook)
      end
    end unless @@before_hooks[method] || @@after_hooks[method]
    return returnable
  end
end

#after_filter(hook_method, options = {}) ⇒ Object



35
36
37
38
# File 'lib/filt.rb', line 35

def after_filter(hook_method,options={})
  options.keys.each{|k|options[k] = [options[k]].flatten}
  (@@after_hooks||={})[hook_method.to_sym] = options
end

#before_filter(hook_method, options = {}) ⇒ Object



30
31
32
33
# File 'lib/filt.rb', line 30

def before_filter(hook_method,options={})
  options.keys.each{|k|options[k] = [options[k]].flatten}
  (@@before_hooks||={})[hook_method.to_sym] = options
end

#method_added(method) ⇒ Object



40
41
42
# File 'lib/filt.rb', line 40

def method_added(method)
  add_global_hook(method) unless method.to_s[0,2] == "__" || instance_methods.include?("__#{method}".to_sym)
end