Module: StateMachine::InstanceMethods
- Defined in:
- lib/ruby-state-machine/state_machine.rb
Instance Attribute Summary collapse
-
#event_history ⇒ Object
Returns the value of attribute event_history.
Instance Method Summary collapse
- #current_state ⇒ Object
- #current_state=(state) ⇒ Object
- #default_state ⇒ Object
- #initialize(args = nil) ⇒ Object
- #send_event(event) ⇒ Object
Instance Attribute Details
#event_history ⇒ Object
Returns the value of attribute event_history.
69 70 71 |
# File 'lib/ruby-state-machine/state_machine.rb', line 69 def event_history @event_history end |
Instance Method Details
#current_state ⇒ Object
128 129 130 131 |
# File 'lib/ruby-state-machine/state_machine.rb', line 128 def current_state check_current_state @current_state end |
#current_state=(state) ⇒ Object
133 134 135 |
# File 'lib/ruby-state-machine/state_machine.rb', line 133 def current_state=state @current_state = state.to_sym end |
#default_state ⇒ Object
137 138 139 |
# File 'lib/ruby-state-machine/state_machine.rb', line 137 def default_state (self.class.default_state) ? self.class.default_state : self.class.states.first end |
#initialize(args = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ruby-state-machine/state_machine.rb', line 71 def initialize(args=nil) @current_state = default_state @event_history = BoundedArray.new # Attempt to call super: begin super(args) rescue ArgumentError super() end #puts "Current state is #{@current_state} #{@current_state.class}" end |
#send_event(event) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby-state-machine/state_machine.rb', line 83 def send_event(event) # puts "Sending event: #{event}" check_current_state next_state_instruction = self.class.next_state_instruction(@current_state, event) if next_state_instruction.nil? cs = @current_state # This was causing problems in unit tests: # @current_state = default_state # puts "Returned to default state: [#{@current_state}]." raise InvalidStateError, "No valid next state for #{cs.inspect} using event #{event.inspect} (#{cs.class}/#{event.class})." end if(next_state_instruction) if(!String(next_state_instruction[:decider]).empty?) # Decider present, call method: decide_id = execute(next_state_instruction[:decider], event) # Error checking: if String(decide_id).empty? raise InvalidStateError, "Decider returned blank/nil for #{@current_state} using event #{event}. Next state from decider: [#{decide_id.inspect}]. Possible next states are [#{next_state_instruction[:next].inspect}]" end # Find next state: instruction = Array(next_state_instruction[:next]).detect { |i| i[:state].to_sym==decide_id.to_sym or i[:name].to_sym==decide_id.to_sym } # Error checking: if instruction.nil? raise InvalidStateError, "No valid next instruction for #{@current_state} using event #{event}. Next state from decider: [#{decide_id.inspect}(#{decide_id.class})]. Possible next states are [#{next_state_instruction[:next].inspect}]" end # Do it: process_instruction(instruction, event) else # Do it: process_instruction(next_state_instruction[:next], event) end @current_state end @event_history.push(event) end |