Class: Domainic::Command::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/domainic/command/result.rb,
lib/domainic/command/result/status.rb,
lib/domainic/command/result/error_set.rb

Overview

A value object representing the outcome of a command execution. The Result class provides a consistent interface for handling both successful and failed command executions, including return data and error information.

Results are created through factory methods rather than direct instantiation, making the intent of the result clear:

Results use status codes that align with Unix exit codes, making them suitable for CLI applications:

  • 0 - Successful execution
  • 1 - Runtime failure
  • 2 - Input validation failure
  • 3 - Output validation failure

Examples:

Creating a success result

result = Result.success(value: 42)
result.successful? #=> true
result.value #=> 42

Creating a failure result

result = Result.failure_at_input(
  { name: "can't be blank" },
  context: { attempted_name: nil }
)
result.failure? #=> true
result.errors[:name] #=> ["can't be blank"]

CLI usage

def self.run
  result = MyCommand.call(args)
  puts result.errors.full_messages if result.failure?
  exit(result.status_code)
end

Author:

Since:

  • 0.1.0

Defined Under Namespace

Modules: STATUS Classes: ErrorSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, context: {}, errors: nil) ⇒ void

Creates a new result instance

Parameters:

  • status_code (Integer)

    The status code for the result

  • context (Hash) (defaults to: {})

    The data context for the result

  • errors (Object, nil) (defaults to: nil)

    Any errors that occurred

Raises:

  • (ArgumentError)

    If status_code is invalid or context is not a Hash

Since:

  • 0.1.0



117
118
119
120
121
# File 'lib/domainic/command/result.rb', line 117

def initialize(status_code, context: {}, errors: nil)
  initialize_status_code(status_code)
  initialize_data(context)
  @errors = ErrorSet.new(errors)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (private)

Delegate method calls to the data struct

Parameters:

  • method_name (String, Symbol)

    The method name to call

Returns:

  • (Object)

    The result of the method call

Since:

  • 0.1.0



176
177
178
179
180
# File 'lib/domainic/command/result.rb', line 176

def method_missing(method_name, ...)
  return super unless respond_to_missing?(method_name)

  data.public_send(method_name.to_sym)
end

Instance Attribute Details

#dataStruct (readonly)

The structured data returned by the command

Returns:

  • (Struct)

    A frozen struct containing the command's output data

Since:

  • 0.1.0



52
53
54
# File 'lib/domainic/command/result.rb', line 52

def data
  @data
end

#errorsErrorSet (readonly)

The errors that occurred during command execution

Returns:

  • (ErrorSet)

    The set of errors from the command

Since:

  • 0.1.0



57
58
59
# File 'lib/domainic/command/result.rb', line 57

def errors
  @errors
end

#status_codeInteger (readonly)

The status code indicating the result of the command execution

Returns:

  • (Integer)

    The status code (0 for success, non-zero for failures)

Since:

  • 0.1.0



62
63
64
# File 'lib/domainic/command/result.rb', line 62

def status_code
  @status_code
end

Class Method Details

.failure(errors, context = {}, status: STATUS::FAILED_AT_RUNTIME) ⇒ Result

Creates a new failure result with the given status

Parameters:

  • errors (Object)

    The errors that caused the failure

  • context (Hash) (defaults to: {})

    Optional context data for the failure

  • status (Integer) (defaults to: STATUS::FAILED_AT_RUNTIME)

    The status code for the failure (defaults to FAILED_AT_RUNTIME)

Returns:

  • (Result)

    A new failure result

Since:

  • 0.1.0



72
73
74
# File 'lib/domainic/command/result.rb', line 72

def self.failure(errors, context = {}, status: STATUS::FAILED_AT_RUNTIME)
  new(status, context:, errors:)
end

.failure_at_input(errors, context = {}) ⇒ Result

Creates a new input validation failure result

Parameters:

  • errors (Object)

    The validation errors

  • context (Hash) (defaults to: {})

    Optional context data for the failure

Returns:

  • (Result)

    A new input validation failure result

Since:

  • 0.1.0



83
84
85
# File 'lib/domainic/command/result.rb', line 83

def self.failure_at_input(errors, context = {})
  new(STATUS::FAILED_AT_INPUT, context:, errors:)
end

.failure_at_output(errors, context = {}) ⇒ Result

Creates a new output validation failure result

Parameters:

  • errors (Object)

    The validation errors

  • context (Hash) (defaults to: {})

    Optional context data for the failure

Returns:

  • (Result)

    A new output validation failure result

Since:

  • 0.1.0



94
95
96
# File 'lib/domainic/command/result.rb', line 94

def self.failure_at_output(errors, context = {})
  new(STATUS::FAILED_AT_OUTPUT, context:, errors:)
end

.success(context) ⇒ Result

Creates a new success result

Parameters:

  • context (Hash)

    The successful result data

Returns:

  • (Result)

    A new success result

Since:

  • 0.1.0



104
105
106
# File 'lib/domainic/command/result.rb', line 104

def self.success(context)
  new(STATUS::SUCCESS, context:)
end

Instance Method Details

#failure?Boolean Also known as: failed?

Indicates whether the command failed

Returns:

  • (Boolean)

    true if the command failed; false otherwise

Since:

  • 0.1.0



128
129
130
# File 'lib/domainic/command/result.rb', line 128

def failure?
  status_code != STATUS::SUCCESS
end

#successful?Boolean Also known as: success?

Indicates whether the command succeeded

Returns:

  • (Boolean)

    true if the command succeeded; false otherwise

Since:

  • 0.1.0



137
138
139
# File 'lib/domainic/command/result.rb', line 137

def successful?
  status_code == STATUS::SUCCESS
end