lib/finite/machine.rb in finite-1.0.0 vs lib/finite/machine.rb in finite-1.1.0
- old
+ new
@@ -32,55 +32,74 @@
raise "Method already taken can_#{event_name}?" if @class.methods.include?(:"can_#{event_name}?")
raise "Method already taken #{event_name}" if @class.methods.include?(:"#{event_name}")
raise 'Event #{event_name} already exists. Rename or combine the events' if events.include? event_name
event = Event.new(event_name, &block)
@events[event_name] = event
+
+ # Add the states from the transition
event.transitions.each_value do |transition|
add_state transition.to
add_state transition.from
end
+
+ # Creates the method can_event_name?
@class.send(:define_method, :"can_#{event_name}?") do
event.transitions.key? current_state.name
end
+
+ # Creates the method event_name
@class.send(:define_method, :"#{event_name}") do
if event.transitions.key? current_state.name
- event.callbacks[:before].each do |callback|
- self.instance_eval &callback
- end
+ # Makes sure the transition can happen
+ transition = event.transitions[current_state.name]
+ if transition.condition.nil? or self.instance_exec(&transition.condition)
- if callbacks[:before].key? :all
- callbacks[:before][:all].each do |callback|
+ new_state = states[event.transitions[current_state.name].to]
+
+ # Call all of the "before event" callbacks
+ event.callbacks[:before].each do |callback|
self.instance_eval &callback
end
- end
- new_state = states[event.transitions[current_state.name].to]
+ # Call all of the before_all callbacks
+ if callbacks[:before].key? :all
+ callbacks[:before][:all].each do |callback|
+ self.instance_eval &callback
+ end
+ end
- if callbacks[:before].key? new_state.name
- callbacks[:before][new_state.name].each do |callback|
- self.instance_eval &callback
+ # Call the "before state" callbacks
+ if callbacks[:before].key? new_state.name
+ callbacks[:before][new_state.name].each do |callback|
+ self.instance_eval &callback
+ end
end
- end
- @current_state = new_state
- if callbacks[:after].key? :all
- callbacks[:after][:all].each do |callback|
- self.instance_eval &callback
+ # Set the current state
+ @current_state = new_state
+
+ # call the "after state" callbacks
+ if callbacks[:after].key? current_state.name
+ callbacks[:after][current_state.name].each do |callback|
+ self.instance_eval &callback
+ end
end
- end
- if callbacks[:after].key? current_state.name
- callbacks[:after][current_state.name].each do |callback|
+
+ # call the "after all" callbacks
+ if callbacks[:after].key? :all
+ callbacks[:after][:all].each do |callback|
+ self.instance_eval &callback
+ end
+ end
+
+ # Call the "after event" callbacks
+ event.callbacks[:after].each do |callback|
self.instance_eval &callback
end
+ self
end
-
- event.callbacks[:after].each do |callback|
- self.instance_eval &callback
- end
- else
- raise 'Invalid Transition'
end
end
end
# Add a state to the the state machine if the state hasn't already been
@@ -116,6 +135,6 @@
callbacks[callback][:all] << block
end
end
end
end
-end
\ No newline at end of file
+end