Class: WorkflowTemplate::Outcome::Handler
- Inherits:
-
Object
- Object
- WorkflowTemplate::Outcome::Handler
- Extended by:
- Forwardable
- Defined in:
- lib/workflow_template/outcome/handler.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#final_state ⇒ Object
readonly
Returns the value of attribute final_state.
-
#retval ⇒ Object
readonly
Returns the value of attribute retval.
Class Method Summary collapse
- .default?(status, **opts) ⇒ Boolean
- .handler_strategy(status) ⇒ Object
- .run_handler(status, statuses, final_state, binding: nil, &block) ⇒ Object
- .run_handler?(status, final_state, **opts) ⇒ Boolean
Instance Method Summary collapse
-
#handle_status(status, binding: nil, **opts, &block) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(final_state, statuses: Outcome::Statuses.new, retval: nil) ⇒ Handler
constructor
A new instance of Handler.
- #otherwise(binding: nil, &block) ⇒ Object
- #otherwise_unwrap(slice: nil, fetch: nil) ⇒ Object
Constructor Details
#initialize(final_state, statuses: Outcome::Statuses.new, retval: nil) ⇒ Handler
Returns a new instance of Handler.
18 19 20 21 22 23 |
# File 'lib/workflow_template/outcome/handler.rb', line 18 def initialize(final_state, statuses: Outcome::Statuses.new, retval: nil) @final_state = final_state @statuses = statuses @retval = retval freeze end |
Instance Attribute Details
#final_state ⇒ Object (readonly)
Returns the value of attribute final_state.
12 13 14 |
# File 'lib/workflow_template/outcome/handler.rb', line 12 def final_state @final_state end |
#retval ⇒ Object (readonly)
Returns the value of attribute retval.
12 13 14 |
# File 'lib/workflow_template/outcome/handler.rb', line 12 def retval @retval end |
Class Method Details
.default?(status, **opts) ⇒ Boolean
65 66 67 |
# File 'lib/workflow_template/outcome/handler.rb', line 65 def self.default?(status, **opts) handler_strategy(status).default?(**opts) end |
.handler_strategy(status) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/workflow_template/outcome/handler.rb', line 75 def self.handler_strategy(status) case status when :ok then Ok when :error then Error else raise WorkflowTemplate::Error, "Unexpected status '#{status}'" end end |
.run_handler(status, statuses, final_state, binding: nil, &block) ⇒ Object
59 60 61 62 63 |
# File 'lib/workflow_template/outcome/handler.rb', line 59 def self.run_handler(status, statuses, final_state, binding: nil, &block) statuses = statuses.handled!(status) retval = final_state.state_class.run_handler!(final_state, binding, &block) Handler.new(final_state, statuses: statuses, retval: retval) end |
.run_handler?(status, final_state, **opts) ⇒ Boolean
69 70 71 72 73 |
# File 'lib/workflow_template/outcome/handler.rb', line 69 def self.run_handler?(status, final_state, **opts) return false unless status == final_state.status handler_strategy(status).run_handler?(final_state, **opts) end |
Instance Method Details
#handle_status(status, binding: nil, **opts, &block) ⇒ Object
rubocop:disable Metrics/AbcSize
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/workflow_template/outcome/handler.rb', line 25 def handle_status(status, binding: nil, **opts, &block) # rubocop:disable Metrics/AbcSize status = status.to_sym raise WorkflowTemplate::Error, "Handler for '#{status}' unexpected" if @statuses[status].default? statuses = @statuses.on_default_detected(status, self.class.default?(status, **opts)) if unhandled?(final_state.status) && self.class.run_handler?(status, final_state, **opts) self.class.run_handler(status, statuses, final_state, binding: binding, &block) else Handler.new(final_state, statuses: statuses, retval: retval) end end |
#otherwise(binding: nil, &block) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/workflow_template/outcome/handler.rb', line 37 def otherwise(binding: nil, &block) statuses = @statuses.all_defaults! if unhandled?(final_state.status) self.class.run_handler(final_state.status, statuses, final_state, binding: binding, &block) else Handler.new(final_state, statuses: statuses, retval: retval) end end |
#otherwise_unwrap(slice: nil, fetch: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/workflow_template/outcome/handler.rb', line 46 def otherwise_unwrap(slice: nil, fetch: nil) statuses = @statuses.all_defaults! if unhandled?(final_state.status) Handler.new( final_state, statuses: statuses.handled!(final_state.status), retval: final_state.unwrap(slice: slice, fetch: fetch) ) else Handler.new(final_state, statuses: statuses, retval: retval) end end |