Class: Praxis::RequestStages::RequestStage
- Extended by:
- Forwardable
- Defined in:
- lib/praxis/request_stages/request_stage.rb
Overview
Special Stage what will hijack the run and execute methods to: 1- Run specific controller callbacks (in addition to any normal callbacks) 2- Shortcut the controller callback chain if any returns a Response object
Direct Known Subclasses
Action, LoadRequest, Response, Validate, ValidateParamsAndHeaders, ValidatePayload
Instance Attribute Summary
Attributes inherited from Stage
#after_callbacks, #before_callbacks, #context, #name, #stages
Instance Method Summary collapse
- #execute ⇒ Object
- #execute_controller_callbacks(callbacks) ⇒ Object
- #execute_with_around ⇒ Object
- #path ⇒ Object
- #run ⇒ Object
Methods inherited from Stage
#after, #application, #before, #callback_args, #execute_callbacks, #initialize, #setup!, #setup_deferred_callbacks!
Constructor Details
This class inherits a constructor from Praxis::Stage
Instance Method Details
#execute ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/praxis/request_stages/request_stage.rb', line 74 def execute raise NotImplementedError, 'Subclass must implement Stage#execute' unless @stages.any? @stages.each do |stage| shortcut = stage.run return shortcut if shortcut && shortcut.kind_of?(Praxis::Response) end nil end |
#execute_controller_callbacks(callbacks) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/praxis/request_stages/request_stage.rb', line 17 def execute_controller_callbacks(callbacks) if callbacks.has_key?(path) callbacks[path].each do |(conditions, block)| if conditions.has_key?(:actions) next unless conditions[:actions].include? action.name end result = block.call(controller) return result if result && result.kind_of?(Praxis::Response) end end nil end |
#execute_with_around ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/praxis/request_stages/request_stage.rb', line 48 def execute_with_around cb = controller.class.around_callbacks[ path ] if cb == nil || cb.empty? execute else inner_proc = proc { execute } applicable = cb.select do |(conditions, handler)| if conditions.has_key?(:actions) (conditions[:actions].include? action.name) ? true : false else true end end chain = applicable.reverse.inject(inner_proc) do |blk, (conditions, handler)| if blk proc{ handler.call(controller,blk) } else proc{ handler.call } end end chain.call end end |
#path ⇒ Object
13 14 15 |
# File 'lib/praxis/request_stages/request_stage.rb', line 13 def path [name] end |
#run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/praxis/request_stages/request_stage.rb', line 30 def run setup! setup_deferred_callbacks! execute_callbacks(self.before_callbacks) # Shortcut lifecycle if filters return a response (non-nil but non-response-class response is ignored) r = execute_controller_callbacks(controller.class.before_callbacks) return r if r result = execute_with_around # Still allow the after callbacks to shortcut it if necessary. r = execute_controller_callbacks(controller.class.after_callbacks) return r if r execute_callbacks(self.after_callbacks) result end |