Servizio
Servizio is a gem to support you creating service objects. It was created after I read a blog post about service objects from Philippe Creux. Realy great post, check it out.
I liked the ideas presented there, so I began to use them. Quickly I realised, that combining the basic concepts presented in this post with something like ActiveModel
would be awesome. So there was Servizio
.
TL;DR
Servizio is a class you can derive your service classes from. It includes ActiveModel::Model
and ActiveModel::Validations
and wraps the call
method of the derived class by prepending some code to the derived class' singleton class. The main purpose is to provide some conventions for and to ease the creation of service classes.
class MyService < Servizio::Service
attr_accessor :summands
validates_presence_of :summands
def call
summands.reduce(:+)
end
end
# create an instance of a service (a.k.a. an operation)
operation = MyService.new(operands: [1,2,3])
# call the operation and get it's result
operation.call!.result # => 6
Additional readings
- http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html
- https://netguru.co/blog/service-objects-in-rails-will-help
Contributing
- Fork it ( https://github.com/msievers/servizio/fork )
- 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 a new Pull Request