Module: StateMachine::InstanceMethods

Defined in:
lib/ruby-state-machine/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_historyObject

Returns the value of attribute event_history.



66
67
68
# File 'lib/ruby-state-machine/state_machine.rb', line 66

def event_history
  @event_history
end

Instance Method Details

#current_stateObject



125
126
127
128
# File 'lib/ruby-state-machine/state_machine.rb', line 125

def current_state
  check_current_state
  @current_state
end

#current_state=(state) ⇒ Object



130
131
132
# File 'lib/ruby-state-machine/state_machine.rb', line 130

def current_state=state
  @current_state = state.to_sym
end

#default_stateObject



134
135
136
# File 'lib/ruby-state-machine/state_machine.rb', line 134

def default_state
  (self.class.default_state) ? self.class.default_state : self.class.states.first
end

#initialize(args = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby-state-machine/state_machine.rb', line 68

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



80
81
82
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
# File 'lib/ruby-state-machine/state_machine.rb', line 80

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