Sha256: aec85a2d39c2910eb50b2fe03d19103f44f33dc1620bed65271ae51fa27656bc

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

Contents

require 'rubygems'
require 'stateflow'

class Test
  include Stateflow
  
  stateflow do
    
    initial :love
    
    state :love do
      enter lambda { |t| p "Entering love" }
      exit :exit_love
    end
    
    state :hate do
      enter lambda { |t| p "Entering hate" }
      exit lambda { |t| p "Exiting hate" }
    end
    
    state :mixed do
      enter lambda { |t| p "Entering mixed" }
      exit lambda { |t| p "Exiting mixed" }
    end
    
    event :b do
      transitions :from => :love, :to => :hate, :if => :no_ice_cream
      transitions :from => :hate, :to => :love
    end
    
    event :a do
      transitions :from => :love, :to => [:hate, :mixed], :decide => :likes_ice_cream?
      transitions :from => [:hate, :mixed], :to => :love
    end
  end
  
  def likes_ice_cream?
    rand(10) > 5 ? :mixed : :hate
  end
  
  def exit_love
    p "Exiting love"
  end
  
  def no_ice_cream
    rand(4) > 2 ? true : false
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stateflow-0.0.1 examples/test.rb