Project

servus

0.01
A long-lived project that still receives updates
Servus is a Ruby gem that provides a structured way to create and manage service objects, promoting clean code architecture and separation of concerns in your applications.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

 Project Readme

Docs Gem Version CI License: MIT Ruby Rails

Servus

A disciplined service-object pattern for Ruby and Rails.

Most mature Rails apps grow a services/ directory. Servus gives each service the same shape — one entrypoint, one response object, and opt-in layers for validation, guards, events, and async.

Documentation

Full documentation at zarpay.github.io/servus

Quick Start

Add to your Gemfile:

gem 'servus'
bundle install

Generate a service:

rails g servus:service treasury/transfer_gold from_account to_account gold_dragons

Write the service:

module Treasury
  module TransferGold
    class Service < Servus::Base
      def initialize(from_account:, to_account:, gold_dragons:)
        @from_account = from_account
        @to_account = to_account
        @gold_dragons = gold_dragons
      end

      def call
        @from_account.withdraw!(@gold_dragons)
        @to_account.deposit!(@gold_dragons)

        success(
          transferred: @gold_dragons,
          from_balance: @from_account.balance,
          to_balance: @to_account.balance
        )
      end
    end
  end
end

Call it:

result = Treasury::TransferGold::Service.call(
  from_account: crown_account,
  to_account: night_watch_account,
  gold_dragons: 50
)

result.success?          # => true
result.data.transferred  # => 50
result.data.from_balance # => 950
result.data.to_balance   # => 550

Features

  • Schema validation — JSON Schema for arguments, results, and failure data
  • Error handling — HTTP-aligned error hierarchy with rescue_from
  • Guards — reusable precondition checks with structured errors
  • Events — decouple follow-up work from the service that triggered it
  • Async execution.call_async runs the same service through ActiveJob
  • Lazy resolvers — accept an instance or an ID, resolve on first access
  • Logging — automatic call, outcome, and duration logging
  • Rails integration — controller helpers, generators, autoloading via Railtie

Requirements

  • Ruby >= 3.0
  • ActiveSupport >= 8.0
  • Rails integration is automatic via Railtie; core works in any Ruby application

License

MIT