gocardless/statesman-events

Name: statesman-events

Owner: GoCardless

Description: Event support for Statesman

Created: 2015-12-05 14:53:49.0

Updated: 2018-01-02 22:30:38.0

Pushed: 2016-05-24 06:06:48.0

Homepage:

Size: 7

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Statesman Events

Event support for Statesman.

Gem Version Build Status


Installation
install statesman-events
TL;DR Usage
s TaskStateMachine
clude Statesman::Machine
clude Statesman::Events

ate :unstarted, initial: true
ate :started
ate :finished
ate :cancelled

ent :start do
transition from: :unstarted,  to: :started
d

ent :finish do
transition from: :started,    to: :finished
d

ent :cancel do
transition from: :unstarted,  to: :cancelled
transition from: :started,    to: :cancelled
d

ent :restart do
transition from: :finished,   to: :started
transition from: :cancelled,  to: :started
d


s Task < ActiveRecord::Base
legate :current_state, :trigger, :available_events, to: :state_machine

f state_machine
@state_machine ||= TaskStateMachine.new(self)
d


 = Task.new

.current_state          # => "unstarted"
.trigger(:start)        # => true/false
.current_state          # => "started"
.available_events       # => [:finish, :cancel]
Class methods
Events.event
pleMachine.event(:some_event) do
ansition from: :some_state,       to: :another_state
ansition from: :some_other_state, to: :yet_another_state

Define an event rule. When triggered, the first available transition from the current state will be called.

Instance methods
Event#trigger
ance.trigger(:some_event)

Triggers the passed event, returning true on success. Returns false on failure.

Event#trigger!
ance.trigger!(:some_event)

Triggers the passed event, returning true on success. Raises Statesman::GuardFailedError or Statesman::TransitionFailedError on failure.

Event#available_events
ance.available_events

Returns an array of events you can trigger from the current state.


GoCardless ? open source. If you do too, come join us.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.