Quickstart
Last updated
mkdir app/services# app/services/application_service.rb
class ApplicationService < Operandi::Base
# Add common arguments, callbacks, or helpers shared across all services.
#
# Example: Add a context argument for the current user
# arg :current_user, type: User, optional: true, context: true
endbin/rails generate operandi:service GreetService --args=name --steps=greet --outputs=greeted# app/services/greet_service.rb
class GreetService < ApplicationService
# Arguments
arg :name, type: String
# Steps
step :greet
# Outputs
output :greeted, type: [TrueClass, FalseClass], default: false
private
def greet
puts "Hello, #{name}!"
self.greeted = true
end
endservice = GreetService.run(name: "John")
service.greeted # => trueservice = GreetService.run(name: "John")
if service.success?
puts "Greeting sent!"
puts service.greeted
else
puts "Failed: #{service.errors.to_h}"
end# This will raise Operandi::Error if any errors are added
service = GreetService.run!(name: "John")service = GreetService.run({ name: "John" }, { raise_on_error: true })