Class: StateTransition::StateMachine
- Inherits:
-
Object
- Object
- StateTransition::StateMachine
- Defined in:
- lib/state_transition.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#state_list ⇒ Object
Returns the value of attribute state_list.
Instance Method Summary collapse
- #can_move?(state) ⇒ Boolean
- #create_callbacks(callbacks) ⇒ Object
- #create_edge(from, to) ⇒ Object
- #create_move_action(actions) ⇒ Object
- #have_state?(state) ⇒ Boolean
-
#initialize(data = nil) ⇒ StateMachine
constructor
A new instance of StateMachine.
- #set_state(state) ⇒ Object
Constructor Details
#initialize(data = nil) ⇒ StateMachine
Returns a new instance of StateMachine.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/state_transition.rb', line 8 def initialize(data = nil) @state_list = [] @state_graph = Hash.new{|hash, key| hash[key] = []} @callbacks = {} begin @current = data[:initial] rescue raise StandardError, "StateMachine: Not define first state." end set_state(data[:initial]) create_move_action(data[:actions] || []) create_callbacks(data[:callbacks] || []) end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
5 6 7 |
# File 'lib/state_transition.rb', line 5 def current @current end |
#state_list ⇒ Object
Returns the value of attribute state_list.
6 7 8 |
# File 'lib/state_transition.rb', line 6 def state_list @state_list end |
Instance Method Details
#can_move?(state) ⇒ Boolean
63 64 65 |
# File 'lib/state_transition.rb', line 63 def can_move?(state) @state_graph[@current].include?(state) end |
#create_callbacks(callbacks) ⇒ Object
57 58 59 60 61 |
# File 'lib/state_transition.rb', line 57 def create_callbacks(callbacks) callbacks.each do |name, function| @callbacks[name] = function end end |
#create_edge(from, to) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/state_transition.rb', line 71 def create_edge(from, to) if from.kind_of?(Array) from.each do |state| @state_graph[state].push to unless @state_graph[state].include?(to) end else @state_graph[from].push to end end |
#create_move_action(actions) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/state_transition.rb', line 29 def create_move_action(actions) actions.each do |action| name = action[:name] from = action[:from] to = action[:to] set_state(from) set_state(to) create_edge(from, to) StateMachine.class_eval do define_method name do if can_move?(to) before_func = ("before_" + to.to_s).to_sym after_func = ("after_" + to.to_s).to_sym if @callbacks[before_func] @callbacks[before_func].call end @current = to @callbacks[after_func].call if @callbacks[after_func] else raise StandardError, "Can not move from '#{@current}' to '#{to}'!" end end end end end |
#have_state?(state) ⇒ Boolean
25 26 27 |
# File 'lib/state_transition.rb', line 25 def have_state?(state) @state_list.include?(state) end |
#set_state(state) ⇒ Object
67 68 69 |
# File 'lib/state_transition.rb', line 67 def set_state(state) @state_list.push state unless have_state?(state) || !state.kind_of?(Symbol) end |