class Statefully::State::Failure
{Failure} is a failed {State}.
Attributes
Error stored in the current {State}
@return [StandardError] @api public @example
state = Statefully::State.create(key: 'val').fail(RuntimeError.new('Boom!')) state.error => #<RuntimeError: Boom!>
Public Class Methods
Constructor for the {Failure} object
@param values [Hash<Symbol, Object>] fields to be stored @param error [StandardError] error to be wrapped @param previous [State] previous state @api private
Statefully::State::new
# File lib/statefully/state.rb, line 409 def initialize(values, error, previous:) super(values, previous: previous) @error = error end
Public Instance Methods
Return a {Diff} between current and previous {State}
@return [Diff::Failed] @api public @example
state = Statefully::State.create(key: 'val').fail(RuntimeError.new('Boom!')) state.diff => #<Statefully::Diff::Failed error=#<RuntimeError: Boom!>>
# File lib/statefully/state.rb, line 422 def diff Diff::Failed.new(error).freeze end
Show the current {State} in a human-readable form
@return [String] @api public @example
Statefully::State.create.fail(RuntimeError.new('Boom!')) => #<Statefully::State::Failure error="#<RuntimeError: Boom!>">
# File lib/statefully/state.rb, line 468 def inspect _inspect_details(error: error.inspect) end
Resolve the current {State}
Resolving will return the current {State} if successful, but raise an error wrapped in a {State::Failure}. This is a convenience method inspired by monadic composition from functional languages.
@return [State] if the receiver is {#successful?} @raise [StandardError] if the receiver is {#failed?} @api public @example
Statefully::State.create(key: 'val').resolve => #<Statefully::State::Success key="val"> Statefully::State.create.fail(RuntimeError.new('Boom!')).resolve RuntimeError: Boom! [STACK TRACE]
# File lib/statefully/state.rb, line 457 def resolve raise error end
Check if the current {State} is successful
@return [Boolean] @api public @example
state = Statefully::State.create state.successful? => true state.fail(RuntimeError.new('Boom!')).successful? => false
# File lib/statefully/state.rb, line 437 def successful? false end