Quickstart

Operandi are framework-agnostic and can be used in any Ruby project.

Installation

Add this line to your application's Gemfile:

gem "operandi", "~> 3.0"

Or you can install it yourself by running:

bundle add operandi --version "~> 3.0"

Create ApplicationService

This step is optional but recommended. Creating a base class for your services can help organize your code. This base class will act as the parent for all your services, where you can include common logic such as helpers, logging, error handling, etc.

For Rails Applications

If you're using Rails, you can use the install generator to set up Operandi automatically:

bin/rails generate operandi:install

This will create the ApplicationService base class, an initializer, and a spec file (if RSpec is detected). See Rails Generators for more details.

For Non-Rails Applications

First, create a folder for your services. The path will depend on the framework you are using. For Rails, you can create a folder in app/services.

Next, create your base class. You can name it as you wish, but we recommend ApplicationService.

Create Your First Service

Now let's create our first service. We'll make a simple service that returns a greeting message.

Rails users: You can use the service generator to create services quickly:

See Rails Generators for more information.

Run the Service

Now you can run your service from anywhere in your application.

Check for Success or Failure

Raise on Error with run!

Use run! when you want errors to raise exceptions instead of being collected:

This is equivalent to:

Looks easy, right? But this is just the beginning. Operandi can do much more 🚀

What's Next?

Learn how to configure Operandi for your application:

Next: Configuration

Last updated