Quickstart

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

Installation

Add this line to your application's Gemfile:

Gemfile

gem "light-services", "~> 2.1"

Or you can install it yourself by running:

Terminal

bundle add light-services --version "~> 2.1"

Create ApplicationService

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.

Terminal

mkdir app/services

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

app/services/application_service.rb

class ApplicationService < Light::Services::Base
  # Nothing to put here yet
end

Create Your First Service

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

app/services/greet_service.rb

class GreetService < ApplicationService
  # Arguments
  arg :name, type: String

  # Steps
  step :greet

  # Outputs
  output :greeted, default: false

  private

  def greet
    puts "Hello, #{name}!"
    self.greeted = true
  end
end

Run the Service

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

Anywhere

service = GreetService.run(name: "John")
service.greeted # => true
Next: Concepts

Was this page helpful?