ServicePattern

Easy callback service pattern for Ruby on Rails.

Usage

How to use my plugin.

Installation

Add this line to your application's Gemfile:

gem "service_pattern"

Create an application service that your other services will enherit from in "app/services/application_service":

class ApplicationService < ServicePattern::Service
end

Create your first service in "app/services/users/activator_service":

class Users::ActivatorService < ApplicationService
  def execute
    User.all.find_each(&:activate!)
    succeed!
  end
end

Then call it like this:

response = Users::ActivatorService.()

if response.success?
  puts "Result: #{response.result}"
else
  puts "Errors: #{response.errors.join(". ")}"
end

Or like this:

response = Users::ActivatorService.execute()

if response.success?
  puts "Result: #{response.result}"
else
  puts "Errors: #{response.errors.join(". ")}"
end

Or raise an error if it fails and return the result directly:

result = Users::ActivatorService.execute!

puts "Result: #{result}"

Returning results

You can also return a result, which will automatically make the response successfull:

succeed!(message: "Hello world")

You can then retrieve it like this:

response = Users::ActivatorService.()
puts "Result: #{response.result}"

You can fail a service like this

fail! "Hello world"

Or with multiple errors:

fail! ["Hello world", "Hello again"]

License

The gem is available as open source under the terms of the MIT License.