Project

state_attr

0.0
No commit activity in last 3 years
No release in over 3 years
Minimalistic state machine.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

About

StateAttr is an minimalistic state machine approach for rails allowing multiple state attributes at the same time.

Installation

gem install state_attr

Examples

state_attr :state, {
  nil => :first,
  :first => [:second, :third],
  :second => :last,
  :third => nil,
}

state_attr :special_state, {
  nil => :first,
  :first => :special,
  :second => :special,
  :third => nil,
}, :groups => {
  :special => %w(second third)
}

state_attr :invitation_state, {
  :invited => %w{approved rejected},
  :approved => :rejected,
  :rejected => :approved,
}, :initial => :invited

state_attr :progress, {
  nil => :one,
  :one => :two,
  :two => :three
} do |old, new|
  if new == :three
    send_mail "You just finished."
  else
    send_mail "You just made to #{new}."
  end
  true #if false then state will be not set
end

Options

  • :initial => :value - initial value (not yet implemented)
  • :setter => :exception - raise exception when calling state=
  • :switch_not_allowed => :silent - do not raise exception when switch is not allowed
  • :groups => hash - an hash of mappings for allowed values, aimed as shortcut for repeated states