StatusWorkflow
Basic state machine using Redis for locking.
Usage
require 'redis'
StatusWorkflow.redis = Redis.new
You need an object that has status
, status_changed_at
, and status_error
.
Expects but does not require ActiveRecord (you just have to respond to #reload
, #id
, and #update_columns
)
class Pet < ActiveRecord::Base
before_create do
self.status ||= 'sleep'
end
include StatusWorkflow
status_workflow(
sleep: [:fed],
fed: [:sleep, :run],
run: [:sleep],
)
end
where
sleep: [:fed],
fed: [:sleep, :run],
run: [:sleep],
means:
- from sleep, i can go to fed
- from fed, i can go to sleep or run
- from run, i can go to sleep
If you want >1 status, you do
include StatusWorkflow
status_workflow(
nil => {
sleep: [:feeding],
feeding: [:fed],
fed: [:sleep, :run],
run: [:sleep],
},
alt: {
sleep2: [:feeding2],
feeding2: [:fed2],
fed2: [:sleep2, :run2],
run2: [:sleep2],
}
)
You need an object that has alt_status
, alt_status_changed_at
, and alt_status_error
.
Sponsor
We use status_workflow
for B2C customer lifecycle optimization at Faraday.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/status_workflow. 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.
Code of Conduct
Everyone interacting in the StatusWorkflow project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Copyright
Copyright 2018 Faraday