Module Opensteam::StateMachine::InstanceMethods
In: lib/opensteam/state_machine.rb

Methods

Public Instance methods

changes state to new_state

if current_state is equal new_state, return false

[Source]

     # File lib/opensteam/state_machine.rb, line 168
168:       def change_state( new_state )
169:         if new_state.is_a?( Module )
170:           new_state = new_state.name
171:         end
172:         
173:         current_state = state
174:         
175:         add_history "starting transition from state '#{current_state}' to state '#{new_state}'"
176:         
177:         if current_state == new_state
178:           add_history "transition failed: current_state '#{current_state}' and new_state '#{new_state}' are the same!"
179:           return false
180:         end
181:         
182:         write_attribute(:state, new_state.to_s)
183:         ret = save
184:         
185:         if ret
186:           add_history( "Successfull: transition from state '#{current_state}' to state '#{new_state.to_s}'" )
187:         else
188:           add_history("Failed: transition from state '#{current_state}' to state '#{new_state.to_s}'")
189:         end
190: 
191:         
192:         self.execute_observers
193: #        self.class.observers.each do |o|
194: #          o.exc( self )
195: #        end
196:         
197:         ret
198:   
199:       end

returns an array of event-methods for the current state -> the instance_methods of the current state_module

[Source]

     # File lib/opensteam/state_machine.rb, line 103
103:       def events ; (s = self.state_module ) ? s.instance_methods : [] ; end

[Source]

     # File lib/opensteam/state_machine.rb, line 202
202:       def execute_observers
203:         self.class.observers.each do |o|
204:           o.exc( self )
205:         end
206:       end

fire an event for current state

if self.state is nil, returns false if event is not defined for current state (not an instance method of state_module), an error is raised if an error occured during the event, an error-entry is saved into the history calls change_state (return value of the event is used as next-state, if return value is a Symbol or a Module)

[Source]

     # File lib/opensteam/state_machine.rb, line 135
135:       def fire_event(event, *args, &block )
136:         
137:         return false unless self.state
138:         
139:         add_history( "trying to fire event '#{event}' for state '#{self.state}'" )
140:         
141:         return false unless event_scope = self.state_module
142:         
143:         unless events.include?( event.to_s )
144:           add_history( msg = "Error: event '#{event}' not defined for state '#{self.state}'")
145:           raise EventNotDefined, msg
146:           return false
147:         end
148:         
149:         begin 
150:           event_return = event_scope.instance_method( event ).bind( self ).call( *args, &block )
151:           add_history( "Successfully executed event '#{event}' for state '#{self.state}'" )
152:         rescue
153:           add_history("Error: An error occured during event '#{event}' in state '#{self.state}' : '#{$!}'")
154:         end
155:         
156:         unless event_return == false
157:           self.state = event_return if( event_return.is_a?(Symbol) || event_return.is_a?(Module) )
158:           return event_return
159:         end
160: 
161:       end

overrides state-attribute setter calls change_state

[Source]

     # File lib/opensteam/state_machine.rb, line 121
121:       def state=(new_state)
122:         change_state(new_state)
123:       end

returns the corresponding module for the current state

ex:

  o = Order.create
  o.state = :pending
  o.state_module # => OrderStates::Pending

[Source]

     # File lib/opensteam/state_machine.rb, line 113
113:       def state_module
114:         return nil unless state
115:         "#{self.class.to_s.demodulize}States::#{self.state.to_s.classify}".constantize rescue nil
116:       end

[Validate]