Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Selleo/business_process
ServiceObject pattern
https://github.com/Selleo/business_process
Last synced: 3 months ago
JSON representation
ServiceObject pattern
- Host: GitHub
- URL: https://github.com/Selleo/business_process
- Owner: Selleo
- License: mit
- Created: 2014-06-29T11:35:44.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-07-16T08:35:59.000Z (over 3 years ago)
- Last Synced: 2024-07-20T12:22:50.570Z (4 months ago)
- Language: Ruby
- Size: 12.7 KB
- Stars: 48
- Watchers: 10
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
⚠️ ATTENTION ⚠️
BusinessProcess gem is no longer supported, as it has been replaced by [rails-patterns](https://github.com/Selleo/pattern) gem, which includes more light-weight version of Service Object pattern
# BusinessProcess
ServiceObject'a'like pattern
#### Setup
```ruby
# Gemfilegem 'business_process'
```then `bundle`
#### Usage
```ruby
# Define business processclass 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
endDoSomething.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.
```ruby
# Define business processclass 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
```ruby
# Define business processclass 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.
```ruby
# Define business processclass 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
endclass DoSomethingElse < BusinessProcess::Base
needs :some_other_method
steps :do_something_fancyprivate
def do_something_fancy
100.times { puts "#{some_other_method} is fancy!" }
end
end
```#### Running specs
```
rspec spec
```