No commit activity in last 3 years
No release in over 3 years
A gem that provides a simple interface for service objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

 Project Readme

SimpleServiceProvider

This gem provides a simple interface for your service objects.

Gem Version Build Status Code Climate Dependency Status Coverage Status

Installation

Add this line to your application's Gemfile:

gem 'simple_service_provider'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_service_provider

Usage

Create a Single Responsible service object class by inheriting from SimpleServiceProvider::Base. This gem depends on Virtus, so you can use the DummyConsultant.attribute method to declare the attributes. Also, you can use Rails like validations for your service object. The DummyConsultant class needs to define 2 methods - DummyConsultant#run and DummyConsultant#run!

  class DummyConsultant < SimpleServiceProvider::Base
    attribute :foo, String
    validates :foo, :presence => true

    def run
      puts "Performing the task"
    end

    def run!
      raise "Foo needs to be equal to bar" unless foo == "bar"
      puts "Performing the task!"
    end
  end

The DummyConsultant#run provides an implementation that does not mutate the service object itself. The DummyConsultant#run! provides an implementation that mutates the service object itself. Or it can also represent a dangerous version of run. One that can raise an error. If a separate implementation of run! is not needed, then it's implementation defaults to run.

While performing the task on the service, the gem provides 2 convenience methods - DummyConsultant#work and DummyConsultant#work! The method work! is a more dangerous version of work.

  dummy = DummyConsultant.new(:foo => "bar")
  dummy.work
  dummy.work!

If the service object is not valid, work! will raise an exception. The method work will not raise any exception, but won't perform the task.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2013 Suman Mukherjee

MIT License

For further information about the license, look into License.txt