Module: Domainic::Command Abstract

Defined in:
lib/domainic/command.rb,
lib/domainic/command/result.rb,
lib/domainic/command/errors/error.rb,
lib/domainic/command/class_methods.rb,
lib/domainic/command/result/status.rb,
lib/domainic/command/context/behavior.rb,
lib/domainic/command/instance_methods.rb,
lib/domainic/command/result/error_set.rb,
lib/domainic/command/context/attribute.rb,
lib/domainic/command/context/attribute_set.rb,
lib/domainic/command/context/input_context.rb,
lib/domainic/command/context/output_context.rb,
lib/domainic/command/errors/execution_error.rb,
lib/domainic/command/context/runtime_context.rb

Overview

This module is abstract.

Including classes must implement an #execute method that defines the command's business logic. The #execute method has access to validated inputs via the #context accessor and should set any output values on the context before returning.

A module that implements the Command pattern, providing a structured way to encapsulate business operations. Commands are single-purpose objects that perform a specific action, validating their inputs and outputs while maintaining a consistent interface for error handling and result reporting.

Examples:

Basic command definition

class CreateUser
  include Domainic::Command

  argument :login, String, "The user's login", required: true
  argument :password, String, "The user's password", required: true

  output :user, User, "The created user", required: true
  output :created_at, Time, "When the user was created"

  def execute
    user = User.create!(login: context., password: context.password)
    context.user = user
    context.created_at = Time.current
  end
end

Using external context classes

class CreateUserInput < Domainic::Command::Context::InputContext
  argument :login, String, "The user's login", required: true
  argument :password, String, "The user's password", required: true
end

class CreateUserOutput < Domainic::Command::Context::OutputContext
  field :user, User, "The created user", required: true
  field :created_at, Time, "When the user was created"
end

class CreateUser
  include Domainic::Command

  accepts_arguments_matching CreateUserInput
  returns_output_matching CreateUserOutput

  def execute
    user = User.create!(login: context., password: context.password)
    context.user = user
    context.created_at = Time.current
  end
end

Command usage

# Successful execution
result = CreateUser.call(login: "[email protected]", password: "secret123")
result.successful? #=> true
result.user #=> #<User id: 1, login: "[email protected]">

# Failed execution
result = CreateUser.call(login: "invalid")
result.failure? #=> true
result.errors[:password] #=> ["is required"]

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, Context, InstanceMethods Classes: Error, ExecutionError, Result

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0



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

def self.included(base)
  super
  base.include(InstanceMethods)
  base.extend(ClassMethods)
end