An open API service indexing awesome lists of open source software.

https://github.com/sleeplessbyte/command

:arrow_forward: Command adds the Command Design Pattern to any Class.
https://github.com/sleeplessbyte/command

command pattern

Last synced: 4 months ago
JSON representation

:arrow_forward: Command adds the Command Design Pattern to any Class.

Awesome Lists containing this project

README

          

# Command
[![Build Status: master](https://travis-ci.com/SleeplessByte/command.svg?branch=master)](https://travis-ci.com/SleeplessByte/command)
[![Gem Version](https://badge.fury.io/rb/commande.svg)](https://badge.fury.io/rb/commande)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![Maintainability](https://api.codeclimate.com/v1/badges/070b90f32c99865db49f/maintainability)](https://codeclimate.com/github/SleeplessByte/command/maintainability)

Command adds the [Command Design Pattern](https://sourcemaking.com/design_patterns/command) 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`](https://rubygems.org/gems/command) has been taken on rubygems (but not updated since 2013), and
[`commando`](https://rubygems.org/gems/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`](https://rubygems.org/gems/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'`.

```Ruby
# Gemfile
gem 'commande', require: 'command'
```

## Installation

Add this line to your application's Gemfile:

```Ruby
gem 'commande'
```

or alternatively if you would like to refer to commande as `Command`:

```Ruby
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:

```Ruby
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.

```ruby
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.

```Ruby
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](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at [SleeplessByte/commmand](https://github.com/SleeplessByte/command).
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## 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](https://github.com/SleeplessByte/command/blob/master/CODE_OF_CONDUCT.md).