pluginaweek / state_machine
- Source
- Commits
- Network (27)
- Downloads (26)
- Wiki (1)
- Graphs
-
Tree:
a654225
state_machine / README.rdoc
| f3565041 » | obrie | 2008-05-04 | 1 | == state_machine | |
| 03ead942 » | obrie | 2006-10-21 | 2 | ||
| 307ac8eb » | obrie | 2008-12-07 | 3 | +state_machine+ adds support for creating state machines for attributes on any | |
| 4 | Ruby class. | ||||
| 03ead942 » | obrie | 2006-10-21 | 5 | ||
| 29c1d69f » | obrie | 2007-01-01 | 6 | == Resources | |
| 7 | |||||
| 1c257492 » | obrie | 2007-09-26 | 8 | API | |
| 29c1d69f » | obrie | 2007-01-01 | 9 | ||
| f3565041 » | obrie | 2008-05-04 | 10 | * http://api.pluginaweek.org/state_machine | |
| 29c1d69f » | obrie | 2007-01-01 | 11 | ||
| 695fcf70 » | obrie | 2008-06-25 | 12 | Bugs | |
| 13 | |||||
| 14 | * http://pluginaweek.lighthouseapp.com/projects/13288-state_machine | ||||
| 15 | |||||
| 29c1d69f » | obrie | 2007-01-01 | 16 | Development | |
| 17 | |||||
| 695fcf70 » | obrie | 2008-06-25 | 18 | * http://github.com/pluginaweek/state_machine | |
| 29c1d69f » | obrie | 2007-01-01 | 19 | ||
| 1c257492 » | obrie | 2007-09-26 | 20 | Source | |
| 21 | |||||
| 695fcf70 » | obrie | 2008-06-25 | 22 | * git://github.com/pluginaweek/state_machine.git | |
| 1c257492 » | obrie | 2007-09-26 | 23 | ||
| 29c1d69f » | obrie | 2007-01-01 | 24 | == Description | |
| 25 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 26 | State machines make it dead-simple to manage the behavior of a class. Too often, | |
| b9d0ccc4 » | obrie | 2009-01-10 | 27 | the state of an object is kept by creating multiple boolean attributes and | |
| 307ac8eb » | obrie | 2008-12-07 | 28 | deciding how to behave based on the values. This can become cumbersome and | |
| 29 | difficult to maintain when the complexity of your class starts to increase. | ||||
| 29c1d69f » | obrie | 2007-01-01 | 30 | ||
| 179ce54f » | obrie | 2008-07-05 | 31 | +state_machine+ simplifies this design by introducing the various parts of a real | |
| b68d4bda » | obrie | 2008-12-14 | 32 | state machine, including states, events, transitions, and callbacks. However, | |
| 33 | the api is designed to be so simple you don't even need to know what a | ||||
| 34 | state machine is :) | ||||
| 35 | |||||
| 36 | Some brief, high-level features include: | ||||
| 37 | * Defining state machines on any Ruby class | ||||
| 38 | * Multiple state machines on a single class | ||||
| cfa7757d » | obrie | 2008-12-17 | 39 | * Namespaced state machines | |
| b68d4bda » | obrie | 2008-12-14 | 40 | * before/after transition hooks with explicit transition requirements | |
| 41 | * ActiveRecord integration | ||||
| 42 | * DataMapper integration | ||||
| 43 | * Sequel integration | ||||
| 48d83dfb » | obrie | 2008-12-14 | 44 | * State predicates | |
| 9508a446 » | obrie | 2009-03-03 | 45 | * State-driven instance / class behavior | |
| b9d0ccc4 » | obrie | 2009-01-10 | 46 | * State values of any data type | |
| 47 | * Dynamically-generated state values | ||||
| 17b472ca » | obrie | 2009-03-21 | 48 | * Event parallelization | |
| 2122d327 » | obrie | 2009-04-03 | 49 | * Attribute-based event transitions | |
| b9d0ccc4 » | obrie | 2009-01-10 | 50 | * Inheritance | |
| d0c99916 » | obrie | 2009-03-07 | 51 | * Internationalization | |
| b68d4bda » | obrie | 2008-12-14 | 52 | * GraphViz visualization creator | |
| 53 | |||||
| 54 | Examples of the usage patterns for some of the above features are shown below. | ||||
| b9d0ccc4 » | obrie | 2009-01-10 | 55 | You can find much more detailed documentation in the actual API. | |
| 8563ca6b » | obrie | 2007-09-21 | 56 | ||
| 57 | == Usage | ||||
| 29c1d69f » | obrie | 2007-01-01 | 58 | ||
| f3565041 » | obrie | 2008-05-04 | 59 | === Example | |
| 60 | |||||
| d71fedbe » | obrie | 2008-12-18 | 61 | Below is an example of many of the features offered by this plugin, including: | |
| 179ce54f » | obrie | 2008-07-05 | 62 | * Initial states | |
| db88b6fa » | obrie | 2008-12-19 | 63 | * Namespaced states | |
| 6a94afcc » | obrie | 2008-09-06 | 64 | * Transition callbacks | |
| 179ce54f » | obrie | 2008-07-05 | 65 | * Conditional transitions | |
| 9508a446 » | obrie | 2009-03-03 | 66 | * State-driven instance behavior | |
| b9d0ccc4 » | obrie | 2009-01-10 | 67 | * Customized state values | |
| 17b472ca » | obrie | 2009-03-21 | 68 | * Parallel events | |
| 179ce54f » | obrie | 2008-07-05 | 69 | ||
| d71fedbe » | obrie | 2008-12-18 | 70 | Class definition: | |
| 71 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 72 | class Vehicle | |
| 73 | attr_accessor :seatbelt_on | ||||
| 74 | |||||
| b9d0ccc4 » | obrie | 2009-01-10 | 75 | state_machine :state, :initial => :parked do | |
| 7f7037e4 » | obrie | 2009-03-08 | 76 | before_transition :parked => any - :parked, :do => :put_on_seatbelt | |
| 77 | |||||
| b9d0ccc4 » | obrie | 2009-01-10 | 78 | after_transition :on => :crash, :do => :tow | |
| 79 | after_transition :on => :repair, :do => :fix | ||||
| cdec7f2d » | obrie | 2009-03-02 | 80 | after_transition any => :parked do |vehicle, transition| | |
| 307ac8eb » | obrie | 2008-12-07 | 81 | vehicle.seatbelt_on = false | |
| 82 | end | ||||
| f3565041 » | obrie | 2008-05-04 | 83 | ||
| 84 | event :park do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 85 | transition [:idling, :first_gear] => :parked | |
| f3565041 » | obrie | 2008-05-04 | 86 | end | |
| 87 | |||||
| 88 | event :ignite do | ||||
| 7f7037e4 » | obrie | 2009-03-08 | 89 | transition :stalled => same, :parked => :idling | |
| f3565041 » | obrie | 2008-05-04 | 90 | end | |
| 91 | |||||
| 92 | event :idle do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 93 | transition :first_gear => :idling | |
| f3565041 » | obrie | 2008-05-04 | 94 | end | |
| 95 | |||||
| 96 | event :shift_up do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 97 | transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear | |
| f3565041 » | obrie | 2008-05-04 | 98 | end | |
| 99 | |||||
| 100 | event :shift_down do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 101 | transition :third_gear => :second_gear, :second_gear => :first_gear | |
| f3565041 » | obrie | 2008-05-04 | 102 | end | |
| 103 | |||||
| 6a94afcc » | obrie | 2008-09-06 | 104 | event :crash do | |
| 7f7037e4 » | obrie | 2009-03-08 | 105 | transition all - [:parked, :stalled] => :stalled, :unless => :auto_shop_busy? | |
| f3565041 » | obrie | 2008-05-04 | 106 | end | |
| 107 | |||||
| 6a94afcc » | obrie | 2008-09-06 | 108 | event :repair do | |
| cdec7f2d » | obrie | 2009-03-02 | 109 | transition :stalled => :parked, :if => :auto_shop_busy? | |
| f3565041 » | obrie | 2008-05-04 | 110 | end | |
| db88b6fa » | obrie | 2008-12-19 | 111 | ||
| b9d0ccc4 » | obrie | 2009-01-10 | 112 | state :parked do | |
| db88b6fa » | obrie | 2008-12-19 | 113 | def speed | |
| 114 | 0 | ||||
| 115 | end | ||||
| 116 | end | ||||
| 117 | |||||
| b9d0ccc4 » | obrie | 2009-01-10 | 118 | state :idling, :first_gear do | |
| db88b6fa » | obrie | 2008-12-19 | 119 | def speed | |
| 120 | 10 | ||||
| 121 | end | ||||
| 122 | end | ||||
| 123 | |||||
| b9d0ccc4 » | obrie | 2009-01-10 | 124 | state :second_gear do | |
| db88b6fa » | obrie | 2008-12-19 | 125 | def speed | |
| 126 | 20 | ||||
| 127 | end | ||||
| 128 | end | ||||
| f3565041 » | obrie | 2008-05-04 | 129 | end | |
| 179ce54f » | obrie | 2008-07-05 | 130 | ||
| 09a2e3cd » | obrie | 2009-03-21 | 131 | state_machine :alarm_state, :initial => :active, :namespace => 'alarm' do | |
| 132 | event :enable do | ||||
| 133 | transition all => :active | ||||
| cfa7757d » | obrie | 2008-12-17 | 134 | end | |
| 135 | |||||
| 09a2e3cd » | obrie | 2009-03-21 | 136 | event :disable do | |
| 137 | transition all => :off | ||||
| cfa7757d » | obrie | 2008-12-17 | 138 | end | |
| b9d0ccc4 » | obrie | 2009-01-10 | 139 | ||
| 09a2e3cd » | obrie | 2009-03-21 | 140 | state :active, :value => 1 | |
| 141 | state :off, :value => 0 | ||||
| cfa7757d » | obrie | 2008-12-17 | 142 | end | |
| 143 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 144 | def initialize | |
| 145 | @seatbelt_on = false | ||||
| d71fedbe » | obrie | 2008-12-18 | 146 | super() # NOTE: This *must* be called, otherwise states won't get initialized | |
| 179ce54f » | obrie | 2008-07-05 | 147 | end | |
| 148 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 149 | def put_on_seatbelt | |
| 150 | @seatbelt_on = true | ||||
| 179ce54f » | obrie | 2008-07-05 | 151 | end | |
| 152 | |||||
| 153 | def auto_shop_busy? | ||||
| 154 | false | ||||
| 155 | end | ||||
| 307ac8eb » | obrie | 2008-12-07 | 156 | ||
| 157 | def tow | ||||
| 158 | # tow the vehicle | ||||
| 159 | end | ||||
| 160 | |||||
| 161 | def fix | ||||
| 162 | # get the vehicle fixed by a mechanic | ||||
| 163 | end | ||||
| f3565041 » | obrie | 2008-05-04 | 164 | end | |
| b5066674 » | obrie | 2007-09-21 | 165 | ||
| 2623e4d3 » | obrie | 2009-04-05 | 166 | *Note* the comment made on the +initialize+ method in the class. In order for | |
| 167 | state machine attributes to be properly initialized, <tt>super()</tt> must be called. | ||||
| 168 | See StateMachine::MacroMethods for more information about this. | ||||
| 169 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 170 | Using the above class as an example, you can interact with the state machine | |
| 179ce54f » | obrie | 2008-07-05 | 171 | like so: | |
| 172 | |||||
| b68d4bda » | obrie | 2008-12-14 | 173 | vehicle = Vehicle.new # => #<Vehicle:0xb7cf4eac @state="parked", @seatbelt_on=false> | |
| b9d0ccc4 » | obrie | 2009-01-10 | 174 | vehicle.state # => "parked" | |
| 175 | vehicle.state_name # => :parked | ||||
| 48d83dfb » | obrie | 2008-12-14 | 176 | vehicle.parked? # => true | |
| b68d4bda » | obrie | 2008-12-14 | 177 | vehicle.can_ignite? # => true | |
| 26e1b8f6 » | obrie | 2009-03-19 | 178 | vehicle.ignite_transition # => #<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling> | |
| 5c2b36c4 » | obrie | 2009-03-19 | 179 | vehicle.state_events # => [:ignite] | |
| 180 | vehicle.state_transitions # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>] | ||||
| 37fc8a67 » | obrie | 2008-12-19 | 181 | vehicle.speed # => 0 | |
| 182 | |||||
| b68d4bda » | obrie | 2008-12-14 | 183 | vehicle.ignite # => true | |
| 48d83dfb » | obrie | 2008-12-14 | 184 | vehicle.parked? # => false | |
| 185 | vehicle.idling? # => true | ||||
| 37fc8a67 » | obrie | 2008-12-19 | 186 | vehicle.speed # => 10 | |
| b68d4bda » | obrie | 2008-12-14 | 187 | vehicle # => #<Vehicle:0xb7cf4eac @state="idling", @seatbelt_on=true> | |
| 37fc8a67 » | obrie | 2008-12-19 | 188 | ||
| b68d4bda » | obrie | 2008-12-14 | 189 | vehicle.shift_up # => true | |
| db88b6fa » | obrie | 2008-12-19 | 190 | vehicle.speed # => 10 | |
| 37fc8a67 » | obrie | 2008-12-19 | 191 | vehicle # => #<Vehicle:0xb7cf4eac @state="first_gear", @seatbelt_on=true> | |
| 192 | |||||
| b68d4bda » | obrie | 2008-12-14 | 193 | vehicle.shift_up # => true | |
| db88b6fa » | obrie | 2008-12-19 | 194 | vehicle.speed # => 20 | |
| 37fc8a67 » | obrie | 2008-12-19 | 195 | vehicle # => #<Vehicle:0xb7cf4eac @state="second_gear", @seatbelt_on=true> | |
| 179ce54f » | obrie | 2008-07-05 | 196 | ||
| 197 | # The bang (!) operator can raise exceptions if the event fails | ||||
| b9d0ccc4 » | obrie | 2009-01-10 | 198 | vehicle.park! # => StateMachine::InvalidTransition: Cannot transition state via :park from :second_gear | |
| 48d83dfb » | obrie | 2008-12-14 | 199 | ||
| 200 | # Generic state predicates can raise exceptions if the value does not exist | ||||
| 17b472ca » | obrie | 2009-03-21 | 201 | vehicle.state?(:parked) # => false | |
| e616242d » | obrie | 2009-04-04 | 202 | vehicle.state?(:invalid) # => IndexError: :invalid is an invalid name | |
| cfa7757d » | obrie | 2008-12-17 | 203 | ||
| 204 | # Namespaced machines have uniquely-generated methods | ||||
| 09a2e3cd » | obrie | 2009-03-21 | 205 | vehicle.alarm_state # => 1 | |
| 206 | vehicle.alarm_state_name # => :active | ||||
| b9d0ccc4 » | obrie | 2009-01-10 | 207 | ||
| 09a2e3cd » | obrie | 2009-03-21 | 208 | vehicle.can_disable_alarm? # => true | |
| 209 | vehicle.disable_alarm # => true | ||||
| 210 | vehicle.alarm_state # => 0 | ||||
| 211 | vehicle.alarm_state_name # => :off | ||||
| 212 | vehicle.can_enable_alarm? # => true | ||||
| cfa7757d » | obrie | 2008-12-17 | 213 | ||
| e616242d » | obrie | 2009-04-04 | 214 | vehicle.alarm_off? # => true | |
| 215 | vehicle.alarm_active? # => false | ||||
| 17b472ca » | obrie | 2009-03-21 | 216 | ||
| 217 | # Events can be fired in parallel | ||||
| 09a2e3cd » | obrie | 2009-03-21 | 218 | vehicle.fire_events(:shift_down, :enable_alarm) # => true | |
| 17b472ca » | obrie | 2009-03-21 | 219 | vehicle.state_name # => :first_gear | |
| 09a2e3cd » | obrie | 2009-03-21 | 220 | vehicle.alarm_state_name # => :active | |
| 17b472ca » | obrie | 2009-03-21 | 221 | ||
| e616242d » | obrie | 2009-04-04 | 222 | vehicle.fire_events!(:ignite, :enable_alarm) # => StateMachine::InvalidTransition: Cannot run events in parallel: ignite, enable_alarm | |
| 6a94afcc » | obrie | 2008-09-06 | 223 | ||
| 307ac8eb » | obrie | 2008-12-07 | 224 | == Integrations | |
| 225 | |||||
| 226 | In addition to being able to define state machines on all Ruby classes, a set of | ||||
| 227 | out-of-the-box integrations are available for some of the more popular Ruby | ||||
| 228 | libraries. These integrations add library-specific behavior, allowing for state | ||||
| 229 | machines to work more tightly with the conventions defined by those libraries. | ||||
| 230 | |||||
| 231 | The integrations currently available include: | ||||
| 232 | * ActiveRecord models | ||||
| 233 | * DataMapper resources | ||||
| b68d4bda » | obrie | 2008-12-14 | 234 | * Sequel models | |
| 307ac8eb » | obrie | 2008-12-07 | 235 | ||
| 236 | A brief overview of these integrations is described below. | ||||
| 237 | |||||
| 238 | === ActiveRecord | ||||
| 239 | |||||
| 240 | The ActiveRecord integration adds support for database transactions, automatically | ||||
| 8c843f01 » | obrie | 2009-03-07 | 241 | saving the record, named scopes, validation errors, and observers. For example, | |
| 307ac8eb » | obrie | 2008-12-07 | 242 | ||
| 243 | class Vehicle < ActiveRecord::Base | ||||
| b9d0ccc4 » | obrie | 2009-01-10 | 244 | state_machine :initial => :parked do | |
| 7f7037e4 » | obrie | 2009-03-08 | 245 | before_transition :parked => any - :parked, :do => :put_on_seatbelt | |
| cdec7f2d » | obrie | 2009-03-02 | 246 | after_transition any => :parked do |vehicle, transition| | |
| 307ac8eb » | obrie | 2008-12-07 | 247 | vehicle.seatbelt = 'off' | |
| 248 | end | ||||
| 249 | |||||
| 250 | event :ignite do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 251 | transition :parked => :idling | |
| 307ac8eb » | obrie | 2008-12-07 | 252 | end | |
| 9508a446 » | obrie | 2009-03-03 | 253 | ||
| 254 | state :first_gear, :second_gear do | ||||
| 255 | validates_presence_of :seatbelt_on | ||||
| 256 | end | ||||
| 307ac8eb » | obrie | 2008-12-07 | 257 | end | |
| 258 | |||||
| 259 | def put_on_seatbelt | ||||
| 260 | ... | ||||
| 261 | end | ||||
| 262 | end | ||||
| 263 | |||||
| 264 | class VehicleObserver < ActiveRecord::Observer | ||||
| 265 | # Callback for :ignite event *before* the transition is performed | ||||
| 266 | def before_ignite(vehicle, transition) | ||||
| 267 | # log message | ||||
| 268 | end | ||||
| 269 | |||||
| 2623e4d3 » | obrie | 2009-04-05 | 270 | # Generic transition callback *after* the transition is performed | |
| 307ac8eb » | obrie | 2008-12-07 | 271 | def after_transition(vehicle, transition) | |
| 272 | Audit.log(vehicle, transition) | ||||
| 273 | end | ||||
| 274 | end | ||||
| 275 | |||||
| 276 | For more information about the various behaviors added for ActiveRecord state | ||||
| 146afff4 » | obrie | 2008-12-14 | 277 | machines, see StateMachine::Integrations::ActiveRecord. | |
| 307ac8eb » | obrie | 2008-12-07 | 278 | ||
| 279 | === DataMapper | ||||
| 280 | |||||
| 281 | Like the ActiveRecord integration, the DataMapper integration adds support for | ||||
| 282 | database transactions, automatically saving the record, named scopes, Extlib-like | ||||
| 8c843f01 » | obrie | 2009-03-07 | 283 | callbacks, validation errors, and observers. For example, | |
| 307ac8eb » | obrie | 2008-12-07 | 284 | ||
| 285 | class Vehicle | ||||
| 286 | include DataMapper::Resource | ||||
| 287 | |||||
| 288 | property :id, Serial | ||||
| 289 | property :state, String | ||||
| 290 | |||||
| b9d0ccc4 » | obrie | 2009-01-10 | 291 | state_machine :initial => :parked do | |
| 7f7037e4 » | obrie | 2009-03-08 | 292 | before_transition :parked => any - :parked, :do => :put_on_seatbelt | |
| cdec7f2d » | obrie | 2009-03-02 | 293 | after_transition any => :parked do |transition| | |
| 307ac8eb » | obrie | 2008-12-07 | 294 | self.seatbelt = 'off' # self is the record | |
| 295 | end | ||||
| 296 | |||||
| 297 | event :ignite do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 298 | transition :parked => :idling | |
| 307ac8eb » | obrie | 2008-12-07 | 299 | end | |
| 9508a446 » | obrie | 2009-03-03 | 300 | ||
| 301 | state :first_gear, :second_gear do | ||||
| b3b2c56b » | obrie | 2009-03-03 | 302 | validates_present :seatbelt_on | |
| 9508a446 » | obrie | 2009-03-03 | 303 | end | |
| 307ac8eb » | obrie | 2008-12-07 | 304 | end | |
| 305 | |||||
| 306 | def put_on_seatbelt | ||||
| 307 | ... | ||||
| 308 | end | ||||
| 309 | end | ||||
| 310 | |||||
| 311 | class VehicleObserver | ||||
| 312 | include DataMapper::Observer | ||||
| 313 | |||||
| 314 | observe Vehicle | ||||
| 315 | |||||
| 316 | # Callback for :ignite event *before* the transition is performed | ||||
| 317 | before_transition :on => :ignite do |transition| | ||||
| 318 | # log message (self is the record) | ||||
| 319 | end | ||||
| 320 | |||||
| 2623e4d3 » | obrie | 2009-04-05 | 321 | # Generic transition callback *after* the transition is performed | |
| 804b02a5 » | obrie | 2009-03-28 | 322 | after_transition do |transition| | |
| 323 | Audit.log(self, transition) # self is the record | ||||
| 307ac8eb » | obrie | 2008-12-07 | 324 | end | |
| 325 | end | ||||
| 326 | |||||
| bae9e169 » | obrie | 2008-12-28 | 327 | *Note* that the DataMapper::Observer integration is optional and only available | |
| 328 | when the dm-observer library is installed. | ||||
| 329 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 330 | For more information about the various behaviors added for DataMapper state | |
| 146afff4 » | obrie | 2008-12-14 | 331 | machines, see StateMachine::Integrations::DataMapper. | |
| 307ac8eb » | obrie | 2008-12-07 | 332 | ||
| b68d4bda » | obrie | 2008-12-14 | 333 | === Sequel | |
| 334 | |||||
| 335 | Like the ActiveRecord integration, the Sequel integration adds support for | ||||
| 8c843f01 » | obrie | 2009-03-07 | 336 | database transactions, automatically saving the record, named scopes, validation | |
| 337 | errors and callbacks. For example, | ||||
| b68d4bda » | obrie | 2008-12-14 | 338 | ||
| 339 | class Vehicle < Sequel::Model | ||||
| b9d0ccc4 » | obrie | 2009-01-10 | 340 | state_machine :initial => :parked do | |
| 7f7037e4 » | obrie | 2009-03-08 | 341 | before_transition :parked => any - :parked, :do => :put_on_seatbelt | |
| cdec7f2d » | obrie | 2009-03-02 | 342 | after_transition any => :parked do |transition| | |
| b68d4bda » | obrie | 2008-12-14 | 343 | self.seatbelt = 'off' # self is the record | |
| 344 | end | ||||
| 345 | |||||
| 346 | event :ignite do | ||||
| cdec7f2d » | obrie | 2009-03-02 | 347 | transition :parked => :idling | |
| b68d4bda » | obrie | 2008-12-14 | 348 | end | |
| 9508a446 » | obrie | 2009-03-03 | 349 | ||
| 350 | state :first_gear, :second_gear do | ||||
| 351 | validates_presence_of :seatbelt_on | ||||
| 352 | end | ||||
| b68d4bda » | obrie | 2008-12-14 | 353 | end | |
| 354 | |||||
| 355 | def put_on_seatbelt | ||||
| 356 | ... | ||||
| 357 | end | ||||
| 358 | end | ||||
| 359 | |||||
| 360 | For more information about the various behaviors added for Sequel state | ||||
| 146afff4 » | obrie | 2008-12-14 | 361 | machines, see StateMachine::Integrations::Sequel. | |
| b68d4bda » | obrie | 2008-12-14 | 362 | ||
| a25b0da9 » | obrie | 2009-03-03 | 363 | == Compatibility | |
| 364 | |||||
| 365 | Although state_machine introduces a simplified syntax, it still remains | ||||
| 366 | backwards compatible with previous versions and other state-related libraries. | ||||
| 367 | For example, transitions and callbacks can continue to be defined like so: | ||||
| 368 | |||||
| 369 | class Vehicle | ||||
| 370 | state_machine :initial => :parked do | ||||
| 7f7037e4 » | obrie | 2009-03-08 | 371 | before_transition :from => :parked, :except_to => :parked, :do => :put_on_seatbelt | |
| a25b0da9 » | obrie | 2009-03-03 | 372 | after_transition :to => :parked do |transition| | |
| 373 | self.seatbelt = 'off' # self is the record | ||||
| 374 | end | ||||
| 375 | |||||
| 376 | event :ignite do | ||||
| 377 | transition :from => :parked, :to => :idling | ||||
| 378 | end | ||||
| 379 | end | ||||
| 380 | end | ||||
| 381 | |||||
| 382 | Although this verbose syntax will most likely always be supported, it is | ||||
| 383 | recommended that any state machines eventually migrate to the syntax introduced | ||||
| 384 | in version 0.6.0. | ||||
| 385 | |||||
| b5066674 » | obrie | 2007-09-21 | 386 | == Tools | |
| 387 | |||||
| 6f25124e » | jashmenn | 2008-12-08 | 388 | === Generating graphs | |
| 389 | |||||
| 390 | This library comes with built-in support for generating di-graphs based on the | ||||
| 391 | events, states, and transitions defined for a state machine using GraphViz[http://www.graphviz.org]. | ||||
| 392 | This requires that both the <tt>ruby-graphviz</tt> gem and graphviz library be | ||||
| 393 | installed on the system. | ||||
| 394 | |||||
| 395 | ==== Examples | ||||
| 396 | |||||
| 397 | To generate a graph for a specific file / class: | ||||
| 398 | |||||
| 399 | rake state_machine:draw FILE=vehicle.rb CLASS=Vehicle | ||||
| 400 | |||||
| 401 | To save files to a specific path: | ||||
| 402 | |||||
| 403 | rake state_machine:draw FILE=vehicle.rb CLASS=Vehicle TARGET=files | ||||
| 404 | |||||
| 51306ac5 » | obrie | 2009-03-10 | 405 | To customize the image format / orientation: | |
| 6f25124e » | jashmenn | 2008-12-08 | 406 | ||
| 51306ac5 » | obrie | 2009-03-10 | 407 | rake state_machine:draw FILE=vehicle.rb CLASS=Vehicle FORMAT=jpg ORIENTATION=landscape | |
| 6f25124e » | jashmenn | 2008-12-08 | 408 | ||
| 409 | To generate multiple state machine graphs: | ||||
| 410 | |||||
| 411 | rake state_machine:draw FILE=vehicle.rb,car.rb CLASS=Vehicle,Car | ||||
| 412 | |||||
| 413 | *Note* that this will generate a different file for every state machine defined | ||||
| 0b8609e7 » | obrie | 2008-12-15 | 414 | in the class. The generated files will use an output filename of the format | |
| 211763da » | obrie | 2009-06-14 | 415 | #{class_name}_#{machine_name}.#{format}. | |
| 6f25124e » | jashmenn | 2008-12-08 | 416 | ||
| 417 | For examples of actual images generated using this task, see those under the | ||||
| 0b8609e7 » | obrie | 2008-12-15 | 418 | examples folder. | |
| 6f25124e » | jashmenn | 2008-12-08 | 419 | ||
| 420 | ==== Ruby on Rails Integration | ||||
| 421 | |||||
| 422 | There is a special integration Rake task for generating state machines for | ||||
| 423 | classes used in a Ruby on Rails application. This task will load the application | ||||
| 424 | environment, meaning that it's unnecessary to specify the actual file to load. | ||||
| 425 | |||||
| 426 | For example, | ||||
| 427 | |||||
| 428 | rake state_machine:draw:rails CLASS=Vehicle | ||||
| 429 | |||||
| 430 | ==== Merb Integration | ||||
| 431 | |||||
| 432 | Like Ruby on Rails, there is a special integration Rake task for generating | ||||
| 433 | state machines for classes used in a Merb application. This task will load the | ||||
| 434 | application environment, meaning that it's unnecessary to specify the actual | ||||
| 435 | files to load. | ||||
| 436 | |||||
| 437 | For example, | ||||
| 438 | |||||
| 439 | rake state_machine:draw:merb CLASS=Vehicle | ||||
| 440 | |||||
| 441 | === Interactive graphs | ||||
| 442 | |||||
| 8563ca6b » | obrie | 2007-09-21 | 443 | Jean Bovet - {Visual Automata Simulator}[http://www.cs.usfca.edu/~jbovet/vas.html]. | |
| 444 | This is a great tool for "simulating, visualizing and transforming finite state | ||||
| 445 | automata and Turing Machines". This tool can help in the creation of states and | ||||
| 446 | events for your models. It is cross-platform, written in Java. | ||||
| b5066674 » | obrie | 2007-09-21 | 447 | ||
| f3565041 » | obrie | 2008-05-04 | 448 | == Testing | |
| 449 | |||||
| 48d83dfb » | obrie | 2008-12-14 | 450 | To run the entire test suite (will test ActiveRecord, DataMapper, and Sequel | |
| 0b8609e7 » | obrie | 2008-12-15 | 451 | integrations if the proper dependencies are available): | |
| 594ad289 » | obrie | 2008-05-12 | 452 | ||
| 307ac8eb » | obrie | 2008-12-07 | 453 | rake test | |
| 594ad289 » | obrie | 2008-05-12 | 454 | ||
| 59e0ea49 » | obrie | 2009-03-07 | 455 | Target specific versions of integrations like so: | |
| 456 | |||||
| 03e413c3 » | obrie | 2009-04-25 | 457 | rake test AR_VERSION=2.1.0 DM_VERSION=0.9.4 SEQUEL_VERSION=2.8.0 | |
| 59e0ea49 » | obrie | 2009-03-07 | 458 | ||
| 594ad289 » | obrie | 2008-05-12 | 459 | == Dependencies | |
| 460 | |||||
| 307ac8eb » | obrie | 2008-12-07 | 461 | By default, there are no dependencies. If using specific integrations, those | |
| 462 | dependencies are listed below. | ||||
| 463 | |||||
| 464 | * ActiveRecord[http://rubyonrails.org] integration: 2.1.0 or later | ||||
| 03e413c3 » | obrie | 2009-04-25 | 465 | * DataMapper[http://datamapper.org] integration: 0.9.4 or later | |
| b68d4bda » | obrie | 2008-12-14 | 466 | * Sequel[http://sequel.rubyforge.org] integration: 2.8.0 or later | |

