BoringService
Provides a lightweight, standard (quite frankly, boring) implementation for the service-object pattern.
The benefits of using this over a PORO, besides having a standard way to define and run service-object methods, is the addition of parameter type-checking and before hooks.
In every other way, this leaves you with vanilla, semantic Ruby. Boring is powerful.
Installation
Add this line to your application's Gemfile:
gem 'boring-service'
And then execute:
$ bundle
Or install it yourself as:
$ gem install boring-service
Usage
Parameters
Defining Parameters
class RandomService < BoringService
parameter :random_object
def call
random_object&.class
end
end
RandomService.call
RandomService.call(random_object: 'test')
RandomService.call(random_object: 1)
Basic Parameter Type Checking
class CalculationService < BoringService
parameter :start_number, Integer
parameter :end_number, Integer, default: 2
def call
@magic_number = 42
perform_complex_calculation
end
private
def perform_complex_calculation
start_number + end_number + @magic_number
end
end
CalculationService.call(start_number: 1, end_number: 3)
CalculationService.call(start_number: 1)
CalculationService.call(start_number: '1')
CalculationService.call(end_number: 3)
Advanced Options for Parameter Type Checking
class MoreOptionsService < BoringService
parameter :times_to_run, [String, Integer]
parameter :return_output, -> (param) { param.in?([true, false]) }
parameter :timestamp, Time, default: -> { Time.now.beginning_of_day }
def call
Array.new(times_to_run.to_i) { timestamp if return_output }
end
end
MoreOptionsService.call(times_to_run: 1, return_output: true)
MoreOptionsService.call(times_to_run: 1, return_output: false)
MoreOptionsService.call(times_to_run: 5, return_output: false)
MoreOptionsService.call(times_to_run: '5', return_output: false)
MoreOptionsService.call(times_to_run: 5, return_output: 'false')
MoreOptionsService.call(
times_to_run: 1,
return_output: true,
timestamp: Time.now.beginning_of_day - 1.day
)
Hooks
class RandomService < BoringService
before :set_start_time
before { puts "Started at #{@start_time}" }
def call
puts "Called"
end
private
def set_start_time
@start_time = Time.now
end
end
RandomService.call
Errors
BoringService::ParameterRequired
is raised when a required parameter is not set on call
BoringService::InvalidParameterValue
is raised when a given value's type does not match the type specified for the parameterBoringService::UnknownParameter
is raised when using an undefined parameter
All these classes inherit from BoringService::ParameterError
, which inherits from ArgumentError
.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/LeadSimple/boring-service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.