ruby - peter-murach/finite_machine restore persisted state -
using finite machine gem ruby piotr murach.
would restore state machine persisted (stored) state. documentation teaches initial state, , restoring state , activerecord model.
all i've been able find dsl lets me define initial state, or define event transitions initial state. both require me define initial state @ coding time.
fm = finitemachine.define initial :red
or
fm = finitemachine.define events { event :start, :none => :red
in practice, i'm defining "standalone" along lines of,
class engine < finitemachine::definition initial :neutral
what i'd define initial state in initializer class, like:
class engine < finitemachine::definition def initialize(car, state) initial state target car end
however not work. :none
current state after initialization.
found restore!
method , section in doc persisting state activerecord. tried along lines of:
class engine < finitemachine::definition def initialize(car, state) restore! state target car end
however constructor returns class finitemachine::statemachine
when called new
on it. new
method takes number of arguments, , initialize
method of class never called. returns different class.
here output program follows:
gsm class finitemachine::statemachine gsm current state red .gems/gems/finite_machine-0.10.1/lib/finite_machine/state_machine.rb:259:in `valid_state?': inappropriate current state 'red' (finitemachine::invalidstateerror)
the program:
require 'finite_machine' class genericstatemachine < finitemachine::definition initial :red def initialize(light) puts "initializer #{light}" super restore! light.state target light end events { event :start, :red => :green event :stop, :green => :red } callbacks { on_enter { |event| target.state = event.to } } end class light attr_accessor :state def initialize state = 'green' end def to_s "light in state #{state}" end end light = light.new gsm = genericstatemachine.new(light) puts "gsm class #{gsm.class.to_s}" puts "gsm current state #{gsm.current}" gsm.stop puts "gsm state after stop #{gsm.current}" puts "light state after stop #{light.state}"
what works better make state machine factory method, uses dsl via finitemachine.define
here output program follows:
creating machine light in state green gsm class finitemachine::statemachine gsm current state green gsm state after stop red light state after stop red
the program:
require 'finite_machine' class lightmachinefactory def self.create_machine(light) finitemachine.define puts "creating machine #{light}" initial light.state target light events { event :start, :red => :green event :stop, :green => :red } callbacks { on_enter { |event| target.state = event.to } } end end end class light attr_accessor :state def initialize @state = 'green' end def to_s "light in state #{state}" end def state @state.to_sym end end light = light.new gsm = lightmachinefactory.create_machine(light) puts "gsm class #{gsm.class.to_s}" puts "gsm current state #{gsm.current}" gsm.stop puts "gsm state after stop #{gsm.current}" puts "light state after stop #{light.state}"
this content shared (my) closed issue in github source repository gem. (this better forum more usage question issue.)
Comments
Post a Comment