Skip to content

Instantly share code, notes, and snippets.

@knewter
Created April 24, 2012 02:19
Show Gist options
  • Save knewter/2475534 to your computer and use it in GitHub Desktop.
Save knewter/2475534 to your computer and use it in GitHub Desktop.
Issue I'm having with celluloid and FSM
jruby-1.6.7 :002 > require './person'
=> true
jruby-1.6.7 :003 > p=Person.new 'Kristen'
=> <Person @name=Kristen @age=0>
jruby-1.6.7 :004 > p.machine
=> #<Person::Machine:0x1f48b272 @state=:living, @actor=<Person @name=Kristen @age=0>>
jruby-1.6.7 :005 > p.machine.transition(:aging)
=> nil
jruby-1.6.7 :006 > p.machine
=> #<Person::Machine:0x1f48b272>

See how the machine lacks any state or actor info after the transition?

require 'rubygems'
require 'bundler/setup'
require 'celluloid'
class Person
include Celluloid
def self.max_age
15
end
# State machine to manage lifecycle
class Machine
include Celluloid::FSM
default_state :living
state :aging, :to => [:living, :dead] do
actor.get_older
if actor.too_old?
transition :dead
else
transition :living
end
end
state :dead do
puts "oops I'm dead"
end
end
def initialize name
@name = name
@age = 0
@machine = Machine.new
end
attr_reader :name
attr_accessor :age
attr_reader :machine
def to_s
"#{name}: #{age} years old"
end
def get_older(years=1)
@age += years
end
def too_old?
@age > Person.max_age
end
def inspect
"<Person @name=#{@name} @age=#{@age}>"
end
end
if __FILE__ == $0
puts 'making josh'
p = Person.new "Josh Adams"
puts p
puts p.machine
p.machine.transition(:aging)
puts p
puts p.machine
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment