Sha256: 47d384ed6873257655f5751f3b65eedeffd862ac9c9fe614ba144bf5a40bfdb3

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

# Operatic

[![GitHub Actions status](https://github.com/benpickles/operatic/workflows/Ruby/badge.svg)](https://github.com/benpickles/operatic)

A minimal standard interface for your operations.

## Installation

Add Operatic to your application's Gemfile and run `bundle install`.

```ruby
gem 'operatic'
```

## Usage

An Operatic class encapsulates an operation and communicates the status of the operation via its result object. As well as being either a `#success?` or a `#failure?` further data can be attached via `#success!`, `#failure!` or convenience accessors.

```ruby
class SayHello
  include Operatic

  # Readers for attributes passed via `.call`.
  attr_reader :name

  # Declare convenience accessors on the result.
  result :message

  def call
    # Exit the method and mark the result as a failure.
    return failure! unless name

    # Mark the result as a success and attach further data.
    success!(message: "Hello #{name}")
  end
end

result = SayHello.call(name: 'Dave')
result.success? # => true
result.message  # => "Hello Dave"
result.to_h     # => {:message=>"Hello Dave"}

result = SayHello.call
result.failure? # => true
result.success? # => false
result.message  # => nil
result.to_h     # => {}
```

A Rails controller might use Operatic like this:

```ruby
class HellosController < ApplicationController
  def create
    result = SayHello.call(name: params[:name])

    if result.success?
      render plain: result.message
    else
      render :new
    end
  end
end
```

## 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 Operatic project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/benpickles/operatic/blob/master/CODE_OF_CONDUCT.md).

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
operatic-0.4.0 README.md
operatic-0.3.1 README.md
operatic-0.3.0 README.md