Created
August 5, 2011 23:12
-
-
Save obrie/1128744 to your computer and use it in GitHub Desktop.
state_machine alternate syntax support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/state_machine/condition_proxy.rb b/lib/state_machine/condition_proxy.rb | |
index a738d02..53ae3c1 100644 | |
--- a/lib/state_machine/condition_proxy.rb | |
+++ b/lib/state_machine/condition_proxy.rb | |
@@ -51,9 +51,18 @@ module StateMachine | |
include EvalHelpers | |
# Creates a new proxy to the given class, merging in the given condition | |
- def initialize(klass, condition) | |
- @klass = klass | |
- @condition = condition | |
+ def initialize(state) | |
+ @state = state | |
+ @klass = state.machine.owner_class | |
+ | |
+ machine_name = state.machine.name | |
+ name = state.name | |
+ @condition = lambda {|object| object.class.state_machine(machine_name).states.matches?(object, name)} | |
+ end | |
+ | |
+ def transition(options) | |
+ from = @state.name | |
+ @state.machine.event(options.delete(:on)) { transition(options.merge(:from => from)) } | |
end | |
# Hooks in condition-merging to methods that don't exist in this module | |
diff --git a/lib/state_machine/state.rb b/lib/state_machine/state.rb | |
index 116ddfd..8a36dd4 100644 | |
--- a/lib/state_machine/state.rb | |
+++ b/lib/state_machine/state.rb | |
@@ -184,7 +184,7 @@ module StateMachine | |
name = self.name | |
# Evaluate the method definitions | |
- context = ConditionProxy.new(owner_class, lambda {|object| object.class.state_machine(machine_name).state | |
+ context = ConditionProxy.new(self) | |
context.class_eval(&block) | |
context.instance_methods.each do |method| | |
methods[method.to_sym] = context.instance_method(method) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment