Skip to content

Instantly share code, notes, and snippets.

@dgreen
Created January 20, 2013 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgreen/d08cbdf7e52ed2cf0074 to your computer and use it in GitHub Desktop.
Save dgreen/d08cbdf7e52ed2cf0074 to your computer and use it in GitHub Desktop.
Exploring rtwomey/stately
#!/usr/bin/env ruby
=begin
This is an example of limitations of the present stately gem.
1. What appear to be instance variables within the Order class (i.e. @count) are not, they
seem to be instance variables of some class State or Machine or ??? which is part of
the underlying implementation of the machine.
2. Examples don't show the need to have requires (a newbie issue only)
3. The instance variable state had to be created to avoid an error message.
=end
require 'rubygems'
require 'stately'
class Order
attr_accessor :count, :state
def initialize
@count = -1
end
stately start: :processing do
state :started do
@count = 0
puts "@count = #{@count}"
end
state :incremented, action: :increment do
@count = 1
puts "@count = #{@count}"
end
end
def count
@count
end
end
order = Order.new
order.start
order.increment
puts "order.count = #{order.count}"
puts "order.state = #{order.state}"
require 'rubygems'
require 'stately'
class Order
attr_accessor :state, :count
stately start: :processing do
state :started do
after_transition do: :zero_count
end
state :incremented, action: :increment do
after_transition do: :increment_count
end
state :waiting, action: :wait
end
def zero_count
@count = 0
end
def increment_count
@count += 1
end
end
order = Order.new
order.start
order.increment
order.wait
order.increment
puts "order.state = #{order.state}"
puts "order.count = #{order.count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment