<commit_before>== state_machine

+state_machine+ adds support for creating state machines for attributes on any
Ruby class.

== Resources

API

* http://api.pluginaweek.org/state_machine

Bugs

* http://pluginaweek.lighthouseapp.com/projects/13288-state_machine

Development

* http://github.com/pluginaweek/state_machine

Source

* git://github.com/pluginaweek/state_machine.git

== Description

State machines make it dead-simple to manage the behavior of a class.  Too often,
the status of an object is kept by creating multiple boolean attributes and
deciding how to behave based on the values.  This can become cumbersome and
difficult to maintain when the complexity of your class starts to increase.

+state_machine+ simplifies this design by introducing the various parts of a real
state machine, including states, events, transitions, and callbacks.  However,
the api is designed to be so simple you don't even need to know what a
state machine is :)

Some brief, high-level features include:
* Defining state machines on any Ruby class
* Multiple state machines on a single class
* Namespaced state machines
* before/after transition hooks with explicit transition requirements
* ActiveRecord integration
* DataMapper integration
* Sequel integration
* States of any data type
* State predicates
* State-driven behavior
* GraphViz visualization creator

Examples of the usage patterns for some of the above features are shown below.
You can find more detailed documentation in the actual API.

== Usage

=== Example

Below is an example of many of the features offered by this plugin, including:
* Initial states
* Namespaced states
* Transition callbacks
* Conditional transitions
* State-driven behavior

Class definition:

  class Vehicle
    attr_accessor :seatbelt_on
    
    state_machine :state, :initial => 'parked' do
      before_transition :from => %w(parked idling), :do => :put_on_seatbelt
      after_transition :on => 'crash', :do => :tow
      after_transition :on => 'repair', :do => :fix
      after_transition :to => 'parked' do |vehicle, transition|
        vehicle.seatbelt_on = false
      end
      
      event :park do
        transition :to => 'parked', :from => %w(idling first_gear)
      end
      
      event :ignite do
        transition :to => 'stalled', :from => 'stalled'
        transition :to => 'idling', :from => 'parked'
      end
      
      event :idle do
        transition :to => 'idling', :from => 'first_gear'
      end
      
      event :shift_up do
        transition :to => 'first_gear', :from => 'idling'
        transition :to => 'second_gear', :from => 'first_gear'
        transition :to => 'third_gear', :from => 'second_gear'
      end
      
      event :shift_down do
        transition :to => 'second_gear', :from => 'third_gear'
        transition :to => 'first_gear', :from => 'second_gear'
      end
      
      event :crash do
        transition :to => 'stalled', :from => %w(first_gear second_gear third_gear), :unless => :auto_shop_busy?
      end
      
      event :repair do
        transition :to => 'parked', :from => 'stalled', :if => :auto_shop_busy?
      end
      
      state 'parked' do
        def speed
          0
        end
      end
      
      state 'idling', 'first_gear' do
        def speed
          10
        end
      end
      
      state 'second_gear' do
        def speed
          20
        end
      end
      
      state 'third_gear' do
        def speed
          30
        end
      end
    end
    
    state_machine :hood_state, :initial => 'closed', :namespace => 'hood' do<commit_msg>Simplify the README example just a bit<commit_after>== state_machine

+state_machine+ adds support for creating state machines for attributes on any
Ruby class.

== Resources

API

* http://api.pluginaweek.org/state_machine

Bugs

* http://pluginaweek.lighthouseapp.com/projects/13288-state_machine

Development

* http://github.com/pluginaweek/state_machine

Source

* git://github.com/pluginaweek/state_machine.git

== Description

State machines make it dead-simple to manage the behavior of a class.  Too often,
the status of an object is kept by creating multiple boolean attributes and
deciding how to behave based on the values.  This can become cumbersome and
difficult to maintain when the complexity of your class starts to increase.

+state_machine+ simplifies this design by introducing the various parts of a real
state machine, including states, events, transitions, and callbacks.  However,
the api is designed to be so simple you don't even need to know what a
state machine is :)

Some brief, high-level features include:
* Defining state machines on any Ruby class
* Multiple state machines on a single class
* Namespaced state machines
* before/after transition hooks with explicit transition requirements
* ActiveRecord integration
* DataMapper integration
* Sequel integration
* States of any data type
* State predicates
* State-driven behavior
* GraphViz visualization creator

Examples of the usage patterns for some of the above features are shown below.
You can find more detailed documentation in the actual API.

== Usage

=== Example

Below is an example of many of the features offered by this plugin, including:
* Initial states
* Namespaced states
* Transition callbacks
* Conditional transitions
* State-driven behavior

Class definition:

  class Vehicle
    attr_accessor :seatbelt_on
    
    state_machine :state, :initial => 'parked' do
      before_transition :from => %w(parked idling), :do => :put_on_seatbelt
      after_transition :on => 'crash', :do => :tow
      after_transition :on => 'repair', :do => :fix
      after_transition :to => 'parked' do |vehicle, transition|
        vehicle.seatbelt_on = false
      end
      
      event :park do
        transition :to => 'parked', :from => %w(idling first_gear)
      end
      
      event :ignite do
        transition :to => 'stalled', :from => 'stalled'
        transition :to => 'idling', :from => 'parked'
      end
      
      event :idle do
        transition :to => 'idling', :from => 'first_gear'
      end
      
      event :shift_up do
        transition :to => 'first_gear', :from => 'idling'
        transition :to => 'second_gear', :from => 'first_gear'
        transition :to => 'third_gear', :from => 'second_gear'
      end
      
      event :shift_down do
        transition :to => 'second_gear', :from => 'third_gear'
        transition :to => 'first_gear', :from => 'second_gear'
      end
      
      event :crash do
        transition :to => 'stalled', :from => %w(first_gear second_gear third_gear), :unless => :auto_shop_busy?
      end
      
      event :repair do
        transition :to => 'parked', :from => 'stalled', :if => :auto_shop_busy?
      end
      
      state 'parked' do
        def speed
          0
        end
      end
      
      state 'idling', 'first_gear' do
        def speed
          10
        end
      end
      
      state 'second_gear' do
        def speed
          20
        end
      end
    end
    
    state_machine :hood_state, :initial => 'closed', :namespace => 'hood' do
{'fraction_non_alphanumeric': 0.0853504967119071, 'fraction_numerical': 0.003078214635511403, 'mean_word_length': 2.7900318133616118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6455023', 'n_tokens_mistral': 2074, 'n_tokens_neox': 2004, 'n_words': 759}