Domainic::Command
A robust implementation of the Command pattern for Ruby applications, providing type-safe, self-documenting business operations with standardized error handling and composable workflows.
Tired of scattered business logic and unclear error handling? Domainic::Command brings clarity to your domain operations by:
- Enforcing explicit input/output contracts with type validation
- Providing consistent error handling and status reporting
- Enabling self-documenting business operations
- Supporting composable command workflows
- Maintaining thread safety for concurrent operations
Quick Start
class CreateUser
include Domainic::Command
# Define expected inputs with validation
argument :login, String, "The user's login", required: true
argument :password, String, "The user's password", required: true
# Define expected outputs
output :user, User, "The created user", required: true
output :created_at, Time, "When the user was created"
def execute
user = User.create!(login: context.login, password: context.password)
context.user = user
context.created_at = Time.current
end
end
# Success case
result = CreateUser.call(login: "[email protected]", password: "secret123")
result.successful? # => true
result.user # => #<User id: 1, login: "[email protected]">
# Failure case
result = CreateUser.call(login: "invalid")
result.failure? # => true
result.errors # => { password: ["is required"] }
Installation
Add this line to your application's Gemfile:
gem 'domainic-command'
Or install it yourself as:
gem install domainic-command
Key Features
- Type-Safe Arguments: Define and validate input parameters with clear type constraints
- Explicit Outputs: Specify expected return values and their requirements
- Standardized Error Handling: Consistent error reporting with detailed failure information
- Thread Safety: Built-in thread safety for class definition and execution
- Command Composition: Build complex workflows by combining simpler commands
- Self-Documenting: Generate clear documentation from your command definitions
- Framework Agnostic: Use with any Ruby application (Rails, Sinatra, pure Ruby)
Documentation
For detailed usage instructions and examples, see USAGE.md.
Contributing
We welcome contributions! Please see our Contributing Guidelines for:
- Development setup and workflow
- Code style and documentation standards
- Testing requirements
- Pull request process
Before contributing, please review our Code of Conduct.
License
The gem is available as open source under the terms of the MIT License.