以下是系统规格:=
现在我有一个Person模型,其中两个字段分别保持状态。
class Person
include Mongoid::Document
include Mongoid::Timestamps
# Field Names
#-----------------
field :name, :type => String
field :sms_state, :type => String, :default => 'main_menu'
field :ivr_state, :type => String, :default => 'main_menu'
end
我使用Monkey Patching添加状态机,因为我需要根据请求控制器更新状态。即,如果请求SMS控制器,则只有用于sms_state的SMS状态机应该进行Monkey Patched,对于IVR Controller应该类似 我这样做猴子修补,(除了状态字段和事件改变之外,整个流程和状态机状态相同的猴子修补原因...)
Person.class_eval do
state_machine :state_field, :initial => :main_menu do
# State Machine Flow
# Events Details
#===============================================================
event :state_reset do
transition all - [:country_confirmation, :topup_confirmation] => :main_menu
end
event :country_list do
transition [:main_menu, :did_purchased, :country_probed] => :country_listed
end
# States Details
#===============================================================================
state :main_menu do
define_method (:get_msg) {|obj| current_module.get_main_menu_msg(obj.dids.count, obj.spokn_id) }
end
state :country_listed do
define_method (:get_msg) {|obj| current_module.get_country_list_msg Country.all }
end
end
end
但它仅适用于State Machine。假设它对SMS控制器工作正常,如果请求来自IVR控制器,则Ivr状态机是Monkey Patched。 但State Field没有,我得到了ERROR
StateMachine::InvalidTransition - Cannot transition sms_state via :ivr_country_list from :country_listed