Class: Statefully::State::Failure

Inherits:
Statefully::State show all
Defined in:
lib/statefully/state.rb

Overview

Failure is a failed Statefully::State.

Instance Attribute Summary collapse

Attributes inherited from Statefully::State

#previous

Instance Method Summary collapse

Methods inherited from Statefully::State

create, #each, #failed?, #fetch, #finished?, #history, #key?, #keys, #none?

Constructor Details

#initialize(values, error, previous:) ⇒ Failure

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Constructor for the Statefully::State::Failure object


385
386
387
388
# File 'lib/statefully/state.rb', line 385

def initialize(values, error, previous:)
  super(values, previous: previous)
  @error = error
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Statefully::State

Instance Attribute Details

#errorStandardError (readonly)

Error stored in the current Statefully::State

Examples:

state = Statefully::State.create(key: 'val').fail(RuntimeError.new('Boom!'))
state.error
=> #<RuntimeError: Boom!>

377
378
379
# File 'lib/statefully/state.rb', line 377

def error
  @error
end

Instance Method Details

#diffDiff::Failed

Return a Diff between current and previous Statefully::State

Examples:

state = Statefully::State.create(key: 'val').fail(RuntimeError.new('Boom!'))
state.diff
=> #<Statefully::Diff::Failed error=#<RuntimeError: Boom!>>

398
399
400
# File 'lib/statefully/state.rb', line 398

def diff
  Diff::Failed.new(error).freeze
end

#inspectString

Show the current Statefully::State in a human-readable form

Examples:

Statefully::State.create.fail(RuntimeError.new('Boom!'))
=> #<Statefully::State::Failure error="#<RuntimeError: Boom!>">

444
445
446
# File 'lib/statefully/state.rb', line 444

def inspect
  _inspect_details(error: error.inspect)
end

#resolveState

Resolve the current Statefully::State

Resolving will return the current Statefully::State if successful, but raise an error wrapped in a Statefully::State::Failure. This is a convenience method inspired by monadic composition from functional languages.

Examples:

Statefully::State.create(key: 'val').resolve
=> #<Statefully::State::Success key="val">

Statefully::State.create.fail(RuntimeError.new('Boom!')).resolve
RuntimeError: Boom!
        [STACK TRACE]

Raises:


433
434
435
# File 'lib/statefully/state.rb', line 433

def resolve
  raise error
end

#successful?Boolean

Check if the current Statefully::State is successful

Examples:

state = Statefully::State.create
state.successful?
=> true

state.fail(RuntimeError.new('Boom!')).successful?
=> false

413
414
415
# File 'lib/statefully/state.rb', line 413

def successful?
  false
end