Top Banner
STATE MACHINES By CodeOfficer codeofficer.com github.com/codeofficer twitter.com/codeofficer Friday, May 1, 2009
12

State Machines

Nov 14, 2014

Download

Documents

Russell jones

2009 April Meeting of the NH Ruby Group:

http://nhruby.org/2009/4/3/april-meeting-state-machines-git

State Machines are a useful design pattern in any language, and in Ruby they are super easy to implement. State_Machine and Acts_As_State_Machine (AASM) both help ease the pain of implementation and provide some amazing features.

In this presentation, I’ll cover some basic aspects of the pattern and talk a bit about both plugins. I’ll also demonstrate where state machines were useful in my own applications, and some of the cool things you can do with them.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

STATE MACHINESBy CodeOfcer

codeofcer.com github.com/codeofcer twitter.com/codeofcerFriday, May 1, 2009

State Machines?A model of behavior composed of a nite number of states, transitions between those states, and actions. A nite state machine is an abstract model of a machine with a primitive internal memory.http://en.wikipedia.org/wiki/Finite_state_machine

Friday, May 1, 2009

So, Whats in a State Machine?

Machine States Events Transitions

Friday, May 1, 2009

Machines? Consist of states, events and transitionsof that class that dene how a state changes after an event is red

Model behavior for a class via an attribute Multiple state machines can be used in aclass, each one tracked by a unique attribute

Friday, May 1, 2009

States? Have an initial state Represent the value for a particular machine in an attribute of your class Attribute can be a value of any type. The default type in Ruby is often String Can be used to dene a behavioral context for a given machine

Friday, May 1, 2009

Events? Dene an action that transitions a machinefrom one state to another

Guards can be put in place to make statetransitions conditional when an action is red

Friday, May 1, 2009

Common Uses? Spree for processing orders RestfulAuthentication ActiveModel in Rails Tracks (GTD) for TODO statusespassive, pending, active, suspended, deleted validations, callbacks, observers active, project_hidden, completed, deferredFriday, May 1, 2009

in_progress, new, canceled, returned, resumed, paid, shipped

Gems and PluginsState MachineA plugin By Aaron Pfeifer (pluginaweek) http://github.com/pluginaweek/state_machine/ http://api.pluginaweek.org/state_machine/

Acts As State MachineA gem By Scott Barron http://github.com/rubyist/aasm/Which state machine project will have the most github watchers by RailsConf 2009? https://opensource.inklingmarkets.com/markets/18368

Friday, May 1, 2009

A Simple Machine(using the State Machine plugin)class Light < ActiveRecord::Base attr_accessor :intensity state_machine :state, :initial =>:off do state :off { def intensity; 0; end } state :low { def intensity; 5; end } state :high { def intensity; 10; end } event :switch do transition :off => :low, :low => :high, :high => :off end end end

@light = Light.new @light.state @light.intensity @light.off? @light.can_switch? @light.switch!

#=> #=> #=> #=> #=>

off 0 true true true

@light.state #=> @light.intensity #=> @light.off? #=> @light.can_switch? #=> @light.switch! # repeat and rinse ...

low 5 false true

Friday, May 1, 2009

Another Machine(also using the State Machine plugin)class Vehicle state_machine :initial => :parked do event :park do transition [:idling, :first_gear] => :parked end event :ignite do transition :stalled => same, :parked => :idling end event :idle do transition :first_gear => :idling end event :shift_up do transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear end event :shift_down do transition :third_gear => :second_gear, :second_gear => :first_gear end event :crash do transition [:first_gear, :second_gear, :third_gear] => :stalled end event :repair do transition :stalled => :parked end end end

Friday, May 1, 2009

Machine Integrations(for the State Machine plugin)

Database Transactions Automatically Saves Records Named Scopes Validation Rrrors Observersevery transition is wrapped within a transactionFriday, May 1, 2009

@thing.event vs @thing.event! (bang throws an exception on fail)

Thing.with_state(:off).all or Thing.with_state([:off, :on]).all

@thing.errors.full_messages # => ["State cannot ... via :xxx from :yyy"]

observers can hook into before/after callbacks for events and generic transitions

Textmate BundlesAuto completion for machines, events, transitions and callbacks

State Machine Acts As State Machine

http://github.com/drnic/ruby-state-machine-tmbundle/

http://github.com/levicole/acts-as-state-machine-tm-bundle/

Friday, May 1, 2009