Rockflow
What is rockflow? Well with rockflow you are able to define workflows (yes even parallel workflows) by writing simple small steps and aggregating them in your flow (the bigger picture... you know)
Let's start the tour!
Installation
Add this line to your application's Gemfile:
gem 'rockflow', '~> 1.2.4'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rockflow
Getting started
Run the bundle command to install it. After you install Rockflow and add it to your Gemfile, you need to run the generator:
rails generate rockflow:install
The generator will install an initializer which describes all of Rockflow's configuration options.
Usage
To write your own flow you need two ingredients: the flow and your steps lets start by looking at the flow.
Flow
# app/workflows/awesome_flow.rb
class AwesomeFlow < Rockflow::Flow
def setup
rock RockStep1, conditions: [{pre: -> { payload[:foo].present? }}]
rock RockStep2, params: {lorem: 'ipsum'}
rock RockStep3, after: [RockStep1, RockStep2]
end
end
Easy right? Notice that RockStep1 and RockStep2 will be executed parallel (so beware of thread safety and stuff). RockStep3 will only execute if RockStep1 and RockStep2 are finished. Here is a picture.
Steps
Look at my steps ... my steps are amazing...
# app/steps/rock_step1.rb
class RockStep1 < Rockflow::Step
def it_up
puts "Iam RockStep1 and i am adding something to the payload"
add_payload :a, 2
end
end
# app/steps/rock_step2.rb
class RockStep2 < Rockflow::Step
def it_up
puts "Iam RockStep2 and i am adding something to the payload"
puts "And here are my params #{params}"
add_payload :b, 2
end
end
# app/steps/rock_step3.rb
class RockStep3 < Rockflow::Step
def it_up
puts "Iam RockStep3 and i am aggregating something from the payload"
result = payload[:a] + payload[:b]
puts "Result: #{result}"
end
end
So now i have defined my steps and my flow but how can i execute it? Well simple just use it like this:
flow = AwesomeFlow.new
flow.concert! # execute all steps and returns all steps. If an error occurs inside the steps it is raised.
flow.concert # returns false or true depending if a step fails.
Please note that if any of your steps fail the parallel execution is interrupted and and an exception is raised.
Summary
Define your flow by inheriting from RockFlow::Flow. Inside your defined flow override the setup method and use the rock keyword defined by your step class. Available options for the rock method are at the moment:
- after which takes a StepClass or an array of step classes.
- params you can specify extra params to be used in a step.
- conditions are used to define pre or post conditions for every step.
Conditions
Conditions are used to define pre or post conditions for every step. To finish a step each given pre or post condition must evaluate to true. Let me give you an example:
class MyFlow < Rockflow::Flow
def setup
rock Step1, conditions: [{pre: -> { false }},
{post: -> { true }}]
end
end
flow = MyFlow.new
flow.concert # => returns false because the pre condition is false
After that start writing your steps by inheriting from RockFlow::Step and overriding the it_up method. Inside of your inherited class you can use following methods.
- add_payload key, value - This method adds data that can be used across your steps inside of your flow
- payload - This method gives you the chance to access your whole payload from your flow.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, 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
to create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
- Fork it ( https://github.com/[my-github-username]/rockflow/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request