forked from owningrails/patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.rb
47 lines (40 loc) · 1.21 KB
/
filters.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require "active_support/all"
module Filters
def self.included(base)
base.extend ClassMethods
# Using Class#class_attribute from ActiveSupport here to make class attributes inheritance
# behave like instance attributes.
# See activesupport/core_ext/class/attribute.rb for more info.
base.class_attribute :before_filters
base.before_filters = []
base.class_attribute :after_filters
base.after_filters = []
base.class_attribute :around_filters
base.around_filters = []
end
module ClassMethods
def before_filter(method)
self.before_filters += [method]
end
def after_filter(method)
self.after_filters += [method]
end
def around_filter(method)
self.around_filters += [method]
end
end
def filter
process_proc = proc do
before_filters.each { |method| send(method) }
yield
after_filters.each { |method| send(method) }
end
# Wrapping each proc in a new one for each around filter.
around_filters.reverse.each do |method|
proc = process_proc
process_proc = proc { send(method, &proc) }
end
# Calling top level proc to start cascading effect.
process_proc.call
end
end