Module: Fastr::Filter
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.execute_filter(filter, obj, *args) ⇒ Object
Executes a filter.
-
.get_filters(klass, type) ⇒ Array
Gets the filters for a class.
-
.get_filters_for_action(klass, type, action) ⇒ Array
Get all the filters for an action.
- .included(kls) ⇒ Object
-
.run_after_filters(controller, klass, action, response) ⇒ Array
Run the response through all after filters and return the new response.
-
.run_before_filters(controller, klass, action) ⇒ Hash
Run through all the before filters and maybe return a response.
- .run_filter?(filter, action) ⇒ Boolean
Instance Method Summary collapse
-
#filter_continue ⇒ Object
Continue through the filter chain.
-
#filter_stop(response) ⇒ Object
Halt the filter chain and return a response.
Methods included from Log
Class Method Details
.execute_filter(filter, obj, *args) ⇒ Object
Executes a filter.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fastr/filter.rb', line 45 def self.execute_filter(filter, obj, *args) if args.length == 0 state = nil else state = *args end filter[:methods].each do |method| if args.length == 0 state = obj.send(method) else state = obj.send(method, state) end end return state end |
.get_filters(klass, type) ⇒ Array
Gets the filters for a class.
78 79 80 81 82 83 84 85 |
# File 'lib/fastr/filter.rb', line 78 def self.get_filters(klass, type) filter_var = "@filters_#{type}".to_sym if klass.instance_variable_defined? filter_var return klass.instance_variable_get(filter_var) else return [] end end |
.get_filters_for_action(klass, type, action) ⇒ Array
Get all the filters for an action.
69 70 71 |
# File 'lib/fastr/filter.rb', line 69 def self.get_filters_for_action(klass, type, action) get_filters(klass, type).find_all { |f| run_filter?(f, action) } end |
.included(kls) ⇒ Object
5 6 7 |
# File 'lib/fastr/filter.rb', line 5 def self.included(kls) kls.extend(ClassMethods) end |
.run_after_filters(controller, klass, action, response) ⇒ Array
Run the response through all after filters and return the new response.
31 32 33 34 35 36 37 |
# File 'lib/fastr/filter.rb', line 31 def self.run_after_filters(controller, klass, action, response) new_response = response get_filters_for_action(klass, :after, action).each do |filter| new_response = execute_filter(filter, controller, new_response) end new_response end |
.run_before_filters(controller, klass, action) ⇒ Hash
Run through all the before filters and maybe return a response.
16 17 18 19 20 21 22 |
# File 'lib/fastr/filter.rb', line 16 def self.run_before_filters(controller, klass, action) new_response = nil get_filters_for_action(klass, :before, action).each do |filter| new_response = execute_filter(filter, controller) end new_response end |
.run_filter?(filter, action) ⇒ Boolean
89 90 91 92 93 94 |
# File 'lib/fastr/filter.rb', line 89 def self.run_filter?(filter, action) return true if filter.has_key? :all and filter[:all] == true return true if filter.has_key? :only and filter[:only].include? action return true if filter.has_key? :except and not filter[:except].include? action return false end |
Instance Method Details
#filter_continue ⇒ Object
Continue through the filter chain
103 104 105 |
# File 'lib/fastr/filter.rb', line 103 def filter_continue nil end |
#filter_stop(response) ⇒ Object
Halt the filter chain and return a response.
98 99 100 |
# File 'lib/fastr/filter.rb', line 98 def filter_stop(response) response end |