SimpleServiceProvider
This gem provides a simple interface for your service objects.
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
Copyright (c) 2013 Suman Mukherjee
MIT License
For further information about the license, look into License.txt