Class: Plutonium::Interaction::Outcome Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/interaction/outcome.rb

Overview

This class is abstract.

Subclass and override #and_then, #map, and #to_response to implement

Base class for interaction outcomes.

This class provides a common interface for both successful and failed outcomes of an interaction.

Direct Known Subclasses

Failure, Success

Defined Under Namespace

Classes: Failure, Success

Instance Method Summary collapse

Instance Method Details

#and_thenObject

This method is abstract.

Chains another operation to be executed if this outcome is successful.

Raises:

  • (NotImplementedError)

    if not implemented in subclass.



51
52
53
# File 'lib/plutonium/interaction/outcome.rb', line 51

def and_then
  raise NotImplementedError, "#{self.class} must implement #and_then"
end

#failure?Boolean

Checks if the outcome is a failure.

Returns:

  • (Boolean)

    true if the outcome is a Failure, false otherwise.



22
23
24
# File 'lib/plutonium/interaction/outcome.rb', line 22

def failure?
  is_a?(Failure)
end

#messagesObject



10
# File 'lib/plutonium/interaction/outcome.rb', line 10

def messages = @messages || []

#success?Boolean

Checks if the outcome is successful.

Returns:

  • (Boolean)

    true if the outcome is a Success, false otherwise.



15
16
17
# File 'lib/plutonium/interaction/outcome.rb', line 15

def success?
  is_a?(Success)
end

#to_responseObject

This method is abstract.

Converts the outcome to a response object.

Raises:

  • (NotImplementedError)

    if not implemented in subclass.



59
60
61
# File 'lib/plutonium/interaction/outcome.rb', line 59

def to_response
  raise NotImplementedError, "#{self.class} must implement #to_response"
end

#with_message(msg, type = :notice) ⇒ self

Adds a message to the outcome.

Parameters:

  • msg (String)

    The message to add.

  • type (Symbol) (defaults to: :notice)

    The type of the message (e.g., :notice, :alert).

Returns:

  • (self)


31
32
33
34
35
# File 'lib/plutonium/interaction/outcome.rb', line 31

def with_message(msg, type = :notice)
  @messages ||= []
  @messages << [msg, type]
  self
end

#with_response(response) ⇒ Object

This method is abstract.

Sets the response for the outcome.

Parameters:

Raises:

  • (NotImplementedError)

    if not implemented in subclass.



43
44
45
# File 'lib/plutonium/interaction/outcome.rb', line 43

def with_response(response)
  raise NotImplementedError, "#{self.class} must implement #with_response"
end