CMDx
CMDx
is a Ruby framework for building maintainable, observable business logic through composable command objects. Design robust workflows with automatic parameter validation, structured error handling, comprehensive logging, and intelligent execution flow control that scales from simple tasks to complex multi-step processes.
Installation
Prerequisites: This gem requires Ruby >= 3.1
to be installed.
Add this line to your application's Gemfile:
gem 'cmdx'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cmdx
Quick Example
# Setup task
class SendWelcomeEmailTask < CMDx::Task
use :middleware, CMDx::Middlewares::Timeout, seconds: 5
on_success :track_email_delivery!
required :user_id, type: :integer, numeric: { min: 1 }
optional :template, type: :string, default: "welcome"
def call
if user.nil?
fail!(reason: "User not found", code: 404)
elsif user.unconfirmed?
skip!(reason: "Email not verified")
else
response = UserMailer.welcome(user, template).deliver_now
context.message_id = response.message_id
end
end
private
def user
@user ||= User.find_by(id: user_id)
end
def track_email_delivery!
user.touch(:welcomed_at)
end
end
# Execute task
result = SendWelcomeEmailTask.call(user_id: 123, template: "premium_welcome")
# Handle result
if result.success?
puts "Welcome email sent <message_id: #{result.context.message_id}>"
elsif result.skipped?
puts "Skipped: #{result.metadata[:reason]}"
elsif result.failed?
puts "Failed: #{result.metadata[:reason]} with code: #{result.metadata[:code]}"
end
Table of contents
- Getting Started
- Configuration
- Basics
- Interruptions
- Parameters
- Outcomes
- Callbacks
- Middlewares
- Workflows
- Logging
- Internationalization (i18n)
- Testing
- Deprecation
- AI Prompts
- Tips & Tricks
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/drexed/cmdx. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the CMDx project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.