0.0
No commit activity in last 3 years
No release in over 3 years
Template for service objects in ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

BaseService

The client should extend this module, in order to implement this common service object pattern.

Advanced Syntax

MyService.new(param1, param2).call do |on|
  on.success { |result| puts "Success #{result}" }

  on.failure { |error| puts "Generic failure #{error}" }
  on.failure(:param1_error) { |error| puts "Problem with param1 #{param1}" }
  on.failure(:param2_error) { |error| puts "Problem with param1 #{param1}" }
end

Semi-Advanced Syntax

MyService.new(param1, param2).call do |on|
  puts "Result: #{on.result}"
end

Support for (depreacted) legacy behaviour

No command-query separation. :(

result = MyService.new(param1, param2).call

Example Service

class MyService
  include BaseService

  def initialize(param1, param2)
    @param1, @param2 = param1, param2
  end

  private
  attr_reader :param1, :param2

  def perform
    return failure "no param1", :param1_error  unless param1.present?
    return failure "no param2", :param2_error  unless param2.present?
    return failure "neither param1 or 2" unless param1.present? && param2.present?

    return "success!"
  end
end