MyService
The main objective is to provide a way to encapsulate a piece of code to delegate responsibilities by calling it from anywhere
Installation
$ gem 'my_service', '~> 0.1.4'
Usage
- Inherit your class of
Service
# services/foo_service.rb
class FooService < Service
end- Define required params (any other params besides are allowed in your call)
class FooService < Service
require_args :arg1, :arg2
end- Define
performmethod
class FooService < Service
require_args :arg1, :arg2
def initialize
# do anything berofe perform
end
def perform
# here you can use params that have become instance variables (@arg1, @arg2 and other params besides)
# return
end
end- Call your service from anywhere
FooService.call(arg1: 1, arg2: 2)Example
This example service changes the status of a "transaction" to complete
# services/complete_transation_service.rb
class CompleteTransationService < Service
require_args :transation_id
def perform
transation.update_attribute(:status, :complete)
end
private
def transation
Transation.find(@transation_id)
end
end if CompleteTransationService.call(transation_id: 1)
puts 'Transation 1 complete!'
endContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/iago-silva/my_service.
License
The gem is available as open source under the terms of the MIT License.