Module: N::Filtering::ClassMethods

Defined in:
lib/nitro/filters.rb

Constant Summary collapse

@@before_filters =

the filters to be evaluated before the method

[]
@@after_filters =

the filters to be evaluated after the method

[]

Instance Method Summary collapse

Instance Method Details

#after_filtersObject



50
51
52
# File 'lib/nitro/filters.rb', line 50

def after_filters
	@@after_filters
end

#append_after_filter(*filters, &block) ⇒ Object Also known as: after_filter



81
82
83
84
# File 'lib/nitro/filters.rb', line 81

def append_after_filter(*filters, &block)
	filters << block if block_given?
	@@after_filters = @@after_filters + filters
end

#append_arround_filter(*filters) ⇒ Object Also known as: filter

Install an arround Filter. A filter is a class that responds to the before and after methods. Both methods are appended to the respective lists.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/nitro/filters.rb', line 112

def append_arround_filter(*filters)
	filters = [filters].flatten
	
	for filter in filters
		if filter.respond_to?(:before)
			append_before_filter { |c| filter.before(c) }
		elsif filter.respond_to?(:filter)
			# consider 'filter' as synonym of before.
			append_before_filter { |c| filter.filter(c) }
		end
		append_after_filter { |c| filter.after(c) } if filter.respond_to?(:after)
	end
end

#append_before_filter(*filters, &block) ⇒ Object



63
64
65
66
# File 'lib/nitro/filters.rb', line 63

def append_before_filter(*filters, &block)
	filters << block if block_given?
	@@before_filters = @@before_filters + filters
end

#before_filtersObject



44
45
46
# File 'lib/nitro/filters.rb', line 44

def before_filters 
	@@before_filters
end

#gen_filters_call_code(filters) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nitro/filters.rb', line 131

def gen_filters_call_code(filters)
	code = ""

	filters.each do |filter| 
		if Symbol === filter
			code << "if #{filter} == false then return false end;"
		elsif filter.respond_to?('call') && (filter.arity == 1 || filter.arity == -1)
			code << 'if filter.call(self) == false then return false end;'
		elsif filter.is_a?(Module) and filter.instance_methods.size > 0
			class_eval "include #{filter};"
			meth = filter.instance_methods[0]
			code << "if #{meth} == false then return false end;"
		elsif filter.respond_to?('filter')
			code << "if #{filter}.filter(self) == false then return false end"
		else
			raise 'Valid filters are either a symbol or a proc/method.'
		end
	end
	
	return code
end

#prepend_after_filter(*filters, &block) ⇒ Object



74
75
76
77
# File 'lib/nitro/filters.rb', line 74

def prepend_after_filter(*filters, &block)
	filters << block if block_given?
	@@after_filters = filters + @@after_filters
end

#prepend_arround_filter(*filters) ⇒ Object

Install an arround Filter. A filter is a class that responds to the before and after methods. Both methods are prepended to the respective lists.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nitro/filters.rb', line 94

def prepend_arround_filter(*filters)
	filters = [filters].flatten
	
	for filter in filters
		if filter.respond_to?(:before)
			prepend_before_filter { |c| filter.before(c) }
		elsif filter.respond_to?(:filter)
			# consider 'filter' as synonym of before.
			prepend_before_filter { |c| filter.filter(c) }
		end
		prepend_after_filter { |c| filter.after(c) } if filter.respond_to?(:after)
	end
end

#prepend_before_filter(*filters, &block) ⇒ Object Also known as: before_filter



56
57
58
59
# File 'lib/nitro/filters.rb', line 56

def prepend_before_filter(*filters, &block)
	filters << block if block_given?
	@@before_filters = filters + @@before_filters
end