Skip to content

Instantly share code, notes, and snippets.

@dgreen
Created Jan 20, 2013
Embed
What would you like to do?
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