Skip to content
Browse files

Added a new non persistent transition

  • Loading branch information...
1 parent 293e9da commit bd1588546068186fc94d4ee87da2d7fe819a5baf Ryan Oberholzer committed Dec 3, 2010
View
14 lib/stateflow.rb
@@ -27,8 +27,12 @@ def stateflow(&block)
end
@machine.events.keys.each do |key|
+ define_method "#{key}" do
+ fire_event(key, :save => false)
+ end
+
define_method "#{key}!" do
- fire_event(key)
+ fire_event(key, :save => true)
end
end
end
@@ -41,8 +45,8 @@ def current_state
@current_state ||= load_from_persistence.nil? ? machine.initial_state : machine.states[load_from_persistence.to_sym]
end
- def current_state=(new_state)
- save_to_persistence(new_state.name.to_s)
+ def set_current_state(new_state, options)
+ save_to_persistence(new_state.name.to_s, options)
@current_state = new_state
end
@@ -51,10 +55,10 @@ def machine
end
private
- def fire_event(event_name)
+ def fire_event(event_name, options = {})
event = machine.events[event_name.to_sym]
raise Stateflow::NoEventFound.new("No event matches #{event_name}") if event.nil?
- event.fire(current_state, self)
+ event.fire(current_state, self, options)
end
end
View
4 lib/stateflow/event.rb
@@ -10,7 +10,7 @@ def initialize(name, machine=nil, &transitions)
instance_eval(&transitions)
end
- def fire(current_state, klass)
+ def fire(current_state, klass, options)
transition = @transitions.select{ |t| t.from.include? current_state.name }.first
raise NoTransitionFound.new("No transition found for event #{@name}") if transition.nil?
@@ -20,7 +20,7 @@ def fire(current_state, klass)
raise NoStateFound.new("Invalid state #{transition.to.to_s} for transition.") if new_state.nil?
current_state.execute_action(:exit, klass)
- klass.current_state = new_state
+ klass.set_current_state(new_state, options)
klass._previous_state = current_state.name.to_s
new_state.execute_action(:enter, klass)
View
7 lib/stateflow/persistence/active_record.rb
@@ -13,8 +13,9 @@ def load_from_persistence
self.send machine.state_column.to_sym
end
- def save_to_persistence(new_state)
- self.update_attribute(machine.state_column.to_sym, new_state)
+ def save_to_persistence(new_state, options)
+ self.send(machine.state_column.to_sym, new_state)
@fj
fj added a note Dec 5, 2010

This is an error; it sends to the accessor, not to the setter. You probably want something like "#{machine.state_column}=".to_sym.

@ryanza
Owner
ryanza added a note Dec 5, 2010

Cool thanks. I was in a bit of a rush, will fix this just now. I want to get some tests out for the persistence layers as thy seem to be creating most of the issues at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.save if options[:save]
end
def ensure_initial_state
@@ -23,4 +24,4 @@ def ensure_initial_state
end
end
end
-end
+end
View
7 lib/stateflow/persistence/mongo_mapper.rb
@@ -11,8 +11,9 @@ def load_from_persistence
self.send machine.state_column.to_sym
end
- def save_to_persistence(new_state)
- self.update_attributes(machine.state_column.to_sym => new_state)
+ def save_to_persistence(new_state, options)
+ self.send(machine.state_column.to_sym, new_state)
@fj
fj added a note Dec 5, 2010

This is an error; it sends to the accessor, not to the setter. You probably want something like "#{machine.state_column}=".to_sym.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.save if options[:save]
end
def ensure_initial_state
@@ -21,4 +22,4 @@ def ensure_initial_state
end
end
end
-end
+end
View
5 lib/stateflow/persistence/mongoid.rb
@@ -11,8 +11,9 @@ def load_from_persistence
self.send machine.state_column.to_sym
end
- def save_to_persistence(new_state)
- self.update_attributes(machine.state_column.to_sym => new_state)
+ def save_to_persistence(new_state, options = {})
+ self.send(machine.state_column.to_sym, new_state)
@fj
fj added a note Dec 5, 2010

This is an error; it sends to the accessor, not to the setter. You probably want something like "#{machine.state_column}=".to_sym.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self.save if options[:save]
end
def ensure_initial_state
View
4 lib/stateflow/persistence/none.rb
@@ -12,10 +12,10 @@ def load_from_persistence
@state
end
- def save_to_persistence(new_state)
+ def save_to_persistence(new_state, options)
@state = new_state
end
end
end
end
-end
+end
View
8 spec/stateflow_spec.rb
@@ -146,7 +146,7 @@ class Stater
end
it "should respond to the current state setter" do
- @r.should respond_to(:current_state=)
+ @r.should respond_to(:set_current_state)
end
it "should respond to the current machine" do
@@ -204,7 +204,7 @@ class Stater
it "should call the fire_event method" do
robot = Robot.new
- robot.should_receive(:fire_event).with(:change_color)
+ robot.should_receive(:fire_event).with(:change_color, {:save=>true})
robot.change_color!
end
@@ -215,7 +215,7 @@ class Stater
it "should fire the event" do
robot = Robot.new
- robot.should_receive(:fire_event).with(:change_color)
+ robot.should_receive(:fire_event).with(:change_color, {:save=>true})
robot.change_color!
end
@@ -246,7 +246,7 @@ class Stater
describe "persistence" do
it "should attempt to persist the new state and the name should be a string" do
robot = Robot.new
- robot.should_receive(:save_to_persistence).with("yellow")
+ robot.should_receive(:save_to_persistence).with("yellow", {:save=>true})
robot.change_color!
end

0 comments on commit bd15885

Please sign in to comment.
Something went wrong with that request. Please try again.