Class: CMDx::Result
- Inherits:
-
Object
- Object
- CMDx::Result
- Extended by:
- Forwardable
- Defined in:
- lib/cmdx/result.rb
Overview
Represents the execution result of a CMDx task, tracking state transitions, status changes, and providing methods for handling different outcomes.
The Result class manages the lifecycle of task execution from initialization through completion or interruption, offering a fluent interface for status checking and conditional handling.
Constant Summary collapse
- STATES =
[ INITIALIZED = "initialized", # Initial state before execution EXECUTING = "executing", # Currently executing task logic COMPLETE = "complete", # Successfully completed execution INTERRUPTED = "interrupted" # Execution was halted due to failure ].freeze
- STATUSES =
[ SUCCESS = "success", # Task completed successfully SKIPPED = "skipped", # Task was skipped intentionally FAILED = "failed" # Task failed due to error or validation ].freeze
Instance Attribute Summary collapse
-
#cause ⇒ Exception?
readonly
Returns the exception that caused the interruption.
-
#metadata ⇒ Hash{Symbol => Object}
readonly
Returns additional metadata about the result.
-
#reason ⇒ String?
readonly
Returns the reason for interruption (skip or failure).
-
#retries ⇒ Integer
Returns the number of retries attempted.
-
#rolled_back ⇒ Boolean
Returns whether the result has been rolled back.
-
#state ⇒ String
readonly
Returns the current execution state of the result.
-
#status ⇒ String
readonly
Returns the execution status of the result.
-
#task ⇒ CMDx::Task
readonly
Returns the task instance associated with this result.
Instance Method Summary collapse
-
#bad? ⇒ Boolean
Whether the task execution was unsuccessful (not success).
-
#caused_failure ⇒ CMDx::Result?
The result that caused this failure, or nil.
-
#caused_failure? ⇒ Boolean
Whether this result caused the failure.
- #complete! ⇒ Object
-
#deconstruct ⇒ Array
Array containing state, status, reason, cause, and metadata.
-
#deconstruct_keys ⇒ Hash
Hash with key-value pairs for pattern matching.
-
#executed! ⇒ self
Returns self for method chaining.
-
#executed? ⇒ Boolean
Whether the task has been executed (complete or interrupted).
- #executing! ⇒ Object
- #fail!(reason = nil, halt: true, cause: nil, **metadata) ⇒ Object
-
#good? ⇒ Boolean
(also: #ok?)
Whether the task execution was successful (not failed).
- #halt! ⇒ Object
-
#index ⇒ Integer
Index of this result in the chain.
-
#initialize(task) ⇒ CMDx::Result
constructor
A new result instance for the task.
- #interrupt! ⇒ Object
-
#on(*states_or_statuses) {|self| ... } ⇒ self
Returns self for method chaining.
-
#outcome ⇒ String
The outcome of the task execution.
-
#retried? ⇒ Boolean
Whether the result has been retried.
-
#rolled_back? ⇒ Boolean
Whether the result has been rolled back.
- #skip!(reason = nil, halt: true, cause: nil, **metadata) ⇒ Object
-
#threw_failure ⇒ CMDx::Result?
The result that threw this failure, or nil.
-
#threw_failure? ⇒ Boolean
Whether this result threw the failure.
- #throw!(result, halt: true, cause: nil, **metadata) ⇒ Object
-
#thrown_failure? ⇒ Boolean
Whether this result is a thrown failure.
-
#to_h ⇒ Hash
Hash representation of the result.
-
#to_s ⇒ String
String representation of the result.
Constructor Details
#initialize(task) ⇒ CMDx::Result
Returns A new result instance for the task.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cmdx/result.rb', line 132 def initialize(task) raise TypeError, "must be a CMDx::Task" unless task.is_a?(CMDx::Task) @task = task @state = INITIALIZED @status = SUCCESS @metadata = {} @reason = nil @cause = nil @retries = 0 @rolled_back = false end |
Instance Attribute Details
#cause ⇒ Exception? (readonly)
Returns the exception that caused the interruption.
96 97 98 |
# File 'lib/cmdx/result.rb', line 96 def cause @cause end |
#metadata ⇒ Hash{Symbol => Object} (readonly)
Returns additional metadata about the result.
76 77 78 |
# File 'lib/cmdx/result.rb', line 76 def @metadata end |
#reason ⇒ String? (readonly)
Returns the reason for interruption (skip or failure).
86 87 88 |
# File 'lib/cmdx/result.rb', line 86 def reason @reason end |
#retries ⇒ Integer
Returns the number of retries attempted.
106 107 108 |
# File 'lib/cmdx/result.rb', line 106 def retries @retries end |
#rolled_back ⇒ Boolean
Returns whether the result has been rolled back.
116 117 118 |
# File 'lib/cmdx/result.rb', line 116 def rolled_back @rolled_back end |
#state ⇒ String (readonly)
Returns the current execution state of the result.
56 57 58 |
# File 'lib/cmdx/result.rb', line 56 def state @state end |
#status ⇒ String (readonly)
Returns the execution status of the result.
66 67 68 |
# File 'lib/cmdx/result.rb', line 66 def status @status end |
#task ⇒ CMDx::Task (readonly)
Returns the task instance associated with this result.
46 47 48 |
# File 'lib/cmdx/result.rb', line 46 def task @task end |
Instance Method Details
#bad? ⇒ Boolean
Returns Whether the task execution was unsuccessful (not success).
246 247 248 |
# File 'lib/cmdx/result.rb', line 246 def bad? !success? end |
#caused_failure ⇒ CMDx::Result?
Returns The result that caused this failure, or nil.
383 384 385 386 387 |
# File 'lib/cmdx/result.rb', line 383 def caused_failure return unless failed? chain.results.reverse.find(&:failed?) end |
#caused_failure? ⇒ Boolean
Returns Whether this result caused the failure.
397 398 399 400 401 |
# File 'lib/cmdx/result.rb', line 397 def caused_failure? return false unless failed? caused_failure == self end |
#complete! ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/cmdx/result.rb', line 196 def complete! return if complete? raise "can only transition to #{COMPLETE} from #{EXECUTING}" unless executing? @state = COMPLETE end |
#deconstruct ⇒ Array
Returns Array containing state, status, reason, cause, and metadata.
534 535 536 |
# File 'lib/cmdx/result.rb', line 534 def deconstruct(*) [state, status, reason, cause, ] end |
#deconstruct_keys ⇒ Hash
Returns Hash with key-value pairs for pattern matching.
549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/cmdx/result.rb', line 549 def deconstruct_keys(*) { state: state, status: status, reason: reason, cause: cause, metadata: , outcome: outcome, executed: executed?, good: good?, bad: bad? } end |
#executed! ⇒ self
Returns self for method chaining
162 163 164 |
# File 'lib/cmdx/result.rb', line 162 def executed! success? ? complete! : interrupt! end |
#executed? ⇒ Boolean
Returns Whether the task has been executed (complete or interrupted).
172 173 174 |
# File 'lib/cmdx/result.rb', line 172 def executed? complete? || interrupted? end |
#executing! ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/cmdx/result.rb', line 182 def executing! return if executing? raise "can only transition to #{EXECUTING} from #{INITIALIZED}" unless initialized? @state = EXECUTING end |
#fail!(reason = nil, halt: true, cause: nil, **metadata) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/cmdx/result.rb', line 308 def fail!(reason = nil, halt: true, cause: nil, **) return if failed? raise "can only transition to #{FAILED} from #{SUCCESS}" unless success? @state = INTERRUPTED @status = FAILED @reason = reason || Locale.t("cmdx.faults.unspecified") @cause = cause @metadata = halt! if halt end |
#good? ⇒ Boolean Also known as: ok?
Returns Whether the task execution was successful (not failed).
235 236 237 |
# File 'lib/cmdx/result.rb', line 235 def good? !failed? end |
#halt! ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/cmdx/result.rb', line 329 def halt! return if success? klass = skipped? ? SkipFault : FailFault fault = klass.new(self) # Strip the first two frames (this method and the delegator) frames = caller_locations(3..-1) unless frames.empty? frames = frames.map(&:to_s) if (cleaner = task.class.settings[:backtrace_cleaner]) cleaner.call(frames) end fault.set_backtrace(frames) end raise(fault) end |
#index ⇒ Integer
Returns Index of this result in the chain.
471 472 473 |
# File 'lib/cmdx/result.rb', line 471 def index chain.index(self) end |
#interrupt! ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/cmdx/result.rb', line 210 def interrupt! return if interrupted? raise "cannot transition to #{INTERRUPTED} from #{COMPLETE}" if complete? @state = INTERRUPTED end |
#on(*states_or_statuses) {|self| ... } ⇒ self
Returns self for method chaining
261 262 263 264 265 266 |
# File 'lib/cmdx/result.rb', line 261 def on(*states_or_statuses, &) raise ArgumentError, "block required" unless block_given? yield(self) if states_or_statuses.any? { |s| send(:"#{s}?") } self end |
#outcome ⇒ String
Returns The outcome of the task execution.
481 482 483 |
# File 'lib/cmdx/result.rb', line 481 def outcome initialized? || thrown_failure? ? state : status end |
#retried? ⇒ Boolean
Returns Whether the result has been retried.
450 451 452 |
# File 'lib/cmdx/result.rb', line 450 def retried? retries.positive? end |
#rolled_back? ⇒ Boolean
Returns Whether the result has been rolled back.
460 461 462 |
# File 'lib/cmdx/result.rb', line 460 def rolled_back? !!@rolled_back end |
#skip!(reason = nil, halt: true, cause: nil, **metadata) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/cmdx/result.rb', line 281 def skip!(reason = nil, halt: true, cause: nil, **) return if skipped? raise "can only transition to #{SKIPPED} from #{SUCCESS}" unless success? @state = INTERRUPTED @status = SKIPPED @reason = reason || Locale.t("cmdx.faults.unspecified") @cause = cause @metadata = halt! if halt end |
#threw_failure ⇒ CMDx::Result?
Returns The result that threw this failure, or nil.
410 411 412 413 414 415 416 |
# File 'lib/cmdx/result.rb', line 410 def threw_failure return unless failed? current = index results = chain.results.select(&:failed?) results.find { |r| r.index > current } || results.last end |
#threw_failure? ⇒ Boolean
Returns Whether this result threw the failure.
426 427 428 429 430 |
# File 'lib/cmdx/result.rb', line 426 def threw_failure? return false unless failed? threw_failure == self end |
#throw!(result, halt: true, cause: nil, **metadata) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/cmdx/result.rb', line 364 def throw!(result, halt: true, cause: nil, **) raise TypeError, "must be a CMDx::Result" unless result.is_a?(Result) @state = result.state @status = result.status @reason = result.reason @cause = cause || result.cause @metadata = result..merge() halt! if halt end |
#thrown_failure? ⇒ Boolean
Returns Whether this result is a thrown failure.
440 441 442 |
# File 'lib/cmdx/result.rb', line 440 def thrown_failure? failed? && !caused_failure? end |
#to_h ⇒ Hash
Returns Hash representation of the result.
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/cmdx/result.rb', line 492 def to_h task.to_h.merge!( state:, status:, outcome:, metadata: ).tap do |hash| if interrupted? hash[:reason] = reason hash[:cause] = cause hash[:rolled_back] = rolled_back? end if failed? STRIP_FAILURE.call(hash, self, :threw_failure) STRIP_FAILURE.call(hash, self, :caused_failure) end end end |
#to_s ⇒ String
Returns String representation of the result.
518 519 520 521 522 523 524 525 |
# File 'lib/cmdx/result.rb', line 518 def to_s Utils::Format.to_str(to_h) do |key, value| case key when /failure/ then "#{key}=<[#{value[:index]}] #{value[:class]}: #{value[:id]}>" else "#{key}=#{value.inspect}" end end end |