araluce / serviz

:triangular_ruler: Command object Interface for Ruby

Home Page:https://rubygems.org/gems/serviz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serviz

Gem Build Status Maintainability

Minimalistic Service Class Interface for Ruby

Serviz provides a minimal interface to unify and homogenize your Service or Command objects in your Ruby code.

Installation

Add this line to your application's Gemfile:

gem 'serviz'

And then execute:

> bundle install

Or install it yourself as:

> gem install serviz

Usage

  • Your class should inherit from Serviz::Base class
  • Your class should implement #call method:
    • Add results via self.result=
    • Add errors via self.errors=

Example:

class RegisterUser < Serviz::Base
  def initialize(user)
    @user = user
  end

  def call
    if @user.valid?
      @user.register_and_notify

      self.result = @user
    else
      self.errors << 'Invalid user'
    end
  end
end
operation = RegisterUser.call(user)

if operation.success?
  puts "[SUCCESS] #{operation.result.name} registered!"
else
  puts "[ERROR] #{operation.errors.join(', ')}"
end

You can also use the #failure? method:

if operation.failure?
  puts "Error! Please try again ..."
  return
end

Development

Any kind of feedback, bug report or enhancement are really welcome!

To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add specs for behaviour changes and run the test suite:

> bundle exec rspec

License

Copyright (c) Marc Anguera. Serviz is released under the MIT License.

About

:triangular_ruler: Command object Interface for Ruby

https://rubygems.org/gems/serviz

License:MIT License


Languages

Language:Ruby 100.0%