Project

bravery

0.0
No commit activity in last 3 years
No release in over 3 years
Ruby state machine micro library. Maintains state and gets out of your way.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
 Project Readme

Bravery

A statemachine micro library for ruby.

... does anyone know where I can find one of those 'under construction' GIFs from the 90's?

Example

class Car
  include Bravery

  # Setting the default state.
  define_start_state :neutral do
    # Define the behavior when entering the state in this block.
    if self.key_in? && self.brake_applied?
      #Transition to a specified state. The block defined for the specified
      # state is immediately invoked.
      goto_state :idling
    else
      # Unlike goto_state, set_state does not call the state's block.
      # self.state will return :parked, but no further action occurs.
      set_state :parked 
    end
  end

  define_state :idling do
    'chug chug chug'
  end

  define_state :parked do
    '....'
  end
end

# New instances will have a default start state (if you specify one with
# define_start_state() )
herby = Car.new
herby.state
# => :neutral

# Instances have dynamic methods corresponding to a states name. 
# You probably never want to do this because it will override state tracking,
# but it's there if you ever need it.
herby.idling
# => 'chug chug chug'

Testing

This has been tested on ruby versions:

  • 1.9.3
  • 2.0.0

Bitdeli Badge