0.03
No commit activity in last 3 years
No release in over 3 years
Event support for Statesman
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 3.1
~> 1.1
~> 0.30.0

Runtime

>= 1.3
 Project Readme

Statesman Events (UNMAINTAINED)

Event support for Statesman.

Gem Version Build Status

THIS REPO IS UNMAINTAINED. PLEASE DO NOT USE IT.


Installation

gem install statesman-events

TL;DR Usage

class TaskStateMachine
  include Statesman::Machine
  include Statesman::Events

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

  event :start do
    transition from: :unstarted,  to: :started
  end

  event :finish do
    transition from: :started,    to: :finished
  end

  event :cancel do
    transition from: :unstarted,  to: :cancelled
    transition from: :started,    to: :cancelled
  end

  event :restart do
    transition from: :finished,   to: :started
    transition from: :cancelled,  to: :started
  end
end

class Task < ActiveRecord::Base
  delegate :current_state, :trigger, :available_events, to: :state_machine

  def state_machine
    @state_machine ||= TaskStateMachine.new(self)
  end
end

task = Task.new

task.current_state          # => "unstarted"
task.trigger(:start)        # => true/false
task.current_state          # => "started"
task.available_events       # => [:finish, :cancel]

Class methods

Events.event

ExampleMachine.event(:some_event) do
  transition from: :some_state,       to: :another_state
  transition from: :some_other_state, to: :yet_another_state
end

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

Instance methods

Event#trigger

instance.trigger(:some_event)

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

Event#trigger!

instance.trigger!(:some_event)

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

Event#available_events

instance.available_events

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