Command
Command adds the Command Design Pattern to any Class
.
This was based on Hanami::Interactor
, and started off as adding a direct call
on the singleton class, before that
was added to Hanami's. After working with different interactors and command-style gems, including ways to organize
units for execution and without depending on other utility classes, command
was born.
Because command
has been taken on rubygems (but not updated since 2013), and
commando
has been taken (but not updated since 2009) and the Dutch opdracht
is
probably not pronounceable by most people using this, I've decided to register this on the French
commande
.
However, if you are using this directly from GitHub, you can continue using it as is, without renaming, as long as you
change the Gemfile line to require: 'command'
.
# Gemfile
gem 'commande', require: 'command'
Installation
Add this line to your application's Gemfile:
gem 'commande'
or alternatively if you would like to refer to commande as Command
:
gem 'commande', require: 'command'
And then execute:
$ bundle
Or install it yourself as:
$ gem install commande
Usage
There are examples in the code and the tests. Here is a crude and basic example:
class FetchSecondInput
include Commande
output :fetched
def call(*args)
# always define call
self.fetched = args.second
end
def valid?(*args)
args.length == 2
end
private
attr_accessor :fetched
end
result = FetchSecondInput.call(42, 'gem')
result.successful? # => true
result.fetched # => 'gem'
result = FetchSecondInput.call(42, 'gem', 'three is a crowd')
result.successful? # => false
result.fetched # => nil
Chaining
If you opt-in to chaining require 'commande/chain'
, you can create a call chain that chains multiple Commande
. The
only limitation is that you can only use named arguments. This is to ensure breakage on unexpected output, name
mismatches and collisions.
require 'command'
require 'command/chain'
class StartCommand
include Commande
output :foo, :baz
def valid?(test:)
error! 'test must at least be 3' if test < 3
true
end
def call(test:)
self.foo = 'foo' * test
self.not_an_output = 'not_an_output'
end
private
attr_accessor :foo, :baz, :not_an_output
end
class SecondCommand
include Commande
output :result
def call(foo:, **_opts)
self.result = foo
end
private
attr_accessor :result
end
chained = Chain.new(StartCommand, SecondCommand).call(test: 3)
# => result is successful, and output matches `SecondCommand`
chained.chain_result.result
# => 'foofoofoo'
boomed = Chain.new(StartCommand, SecondCommand).call(test: 2)
# => boomed has failed, and output matches nil
boomed.error
# => 'test must at least be 3'
Testing
There are some Minitest
assertions included in this library.
require 'commande/minitest'
Assert | Refute | |
---|---|---|
assert_successful(command_result) |
refute_successful |
passes if the command is successful? |
assert_valid(command, *args_for_valid) |
refute_valid |
passes if the command is valid |
assert_with_error(expected, actual) |
refute_with_error |
passes if the command has a certain error message |
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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 SleeplessByte/commmand. 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 Shrine::ConfigurableStorage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.