0.01
No commit activity in last 3 years
No release in over 3 years
General purpose service object abstraction
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
 Project Readme

⚠️ ATTENTION ⚠️

BusinessProcess gem is no longer supported, as it has been replaced by rails-patterns gem, which includes more light-weight version of Service Object pattern

BusinessProcess

ServiceObject'a'like pattern

Setup

# Gemfile

gem 'business_process'

then bundle

Usage

# Define business process

class DoSomething < BusinessProcess::Base
  # Specify requirements
  needs :some_method
  needs :some_other_method
  
  # Specify process (action)
  def call
    some_method + some_other_method
  end
end

# Execute using named parameters
DoSomething.call(some_method: 10, some_other_method: 20)

# Execute using parameter object
class Something
  def some_method
    10
  end 
  
  def some_other_method
    20
  end
end

DoSomething.call(Something.new)

# Read result of execution
service = DoSomething.call(Something.new)
service.result # => 30
service.success? # => true

Failing steps

To indicate failure of business process, use #fail method. Whatever you pass into the method, will be available through #error attribute on the business process object. If you pass a class that inherits from Exception it will also be raised.

# Define business process

class DoSomething < BusinessProcess::Base
 # Specify requirements
 needs :some_method
 needs :some_other_method
 
 # Specify process (action)
 def call
   do_something
   do_something_else
 end
 
 private
 
 def do_something
   fail(:too_low) if some_method < 10 
 end
 
 def do_something_else
   some_other_method + 20
 end
end

# Execute using named parameters
DoSomething.call(some_method: 5, some_other_method: 20)


# Read result of execution
service = DoSomething.call(Something.new)
service.result # => 25
service.success? # => false
service.error # => :too_low

Process definition using .steps

# Define business process

class DoSomething < BusinessProcess::Base
  # Specify requirements
  needs :some_method
  needs :some_other_method
  
  # Specify steps  
  steps :do_something,
        :do_something_else
  
  private
  
  def do_something
    @some_result = some_method + 10
  end
  
  def do_something_else
    some_other_method * 20 + @some_result 
  end  
end

Process definition using .steps and calling related service object

This is useful when some business processes can be composed of other business processes. Remember, that caller business process should provide needs for the callee business process.

# Define business process

class DoSomething < BusinessProcess::Base
  # Specify requirements
  needs :some_method
  needs :some_other_method
  
  # Specify steps  
  steps :do_something,
        :do_something_else
  
  private
  
  def do_something
    @some_result = some_method + 10
  end  
end

class DoSomethingElse < BusinessProcess::Base
  needs :some_other_method
  
  steps :do_something_fancy

  private
  
  def do_something_fancy
    100.times { puts "#{some_other_method} is fancy!" }    
  end
end

Running specs

rspec spec