开发者

Ruby add dynamic events using AASM

开发者 https://www.devze.com 2023-04-11 11:16 出处:网络
I\'ve got a class in a program which is handling game states. I\'m actually handling it with AASM so to create an event I have to use something like aasm_event :name ... inside the class.

I've got a class in a program which is handling game states. I'm actually handling it with AASM so to create an event I have to use something like aasm_event :name ... inside the class.

I need to be able to load other files who dynamically have to add events and states to the class.

How is it possible ?

Thank you in advance开发者_JAVA百科.


The following should work, unless aasm_state and aasm_event are protected or private:

# game.rb
class Game
  include AASM

  aasm_initial_state :start
end

# add the require after the class definition, else it will complain of a missing constant
require "other_file"

# other_file.rb
Game.aasm_state :another_state
Game.aasm_event do
  transitions :to => :another_state, :from => [:start]
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号