aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/state_machine.rb
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
committerrick <technoweenie@gmail.com>2008-06-28 00:55:02 -0700
commitb9528ad3c5379896b00772cb44faf1db0fd882d7 (patch)
tree422ae423e519f744d51698d45a6fb931172b9cb2 /activemodel/lib/active_model/state_machine.rb
parentb7c6ceff9a31cc478c4bc89d57980900a775fbed (diff)
downloadrails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.gz
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.tar.bz2
rails-b9528ad3c5379896b00772cb44faf1db0fd882d7.zip
initial statemachine machine and state classes
Diffstat (limited to 'activemodel/lib/active_model/state_machine.rb')
-rw-r--r--activemodel/lib/active_model/state_machine.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb
new file mode 100644
index 0000000000..bb038f6b7a
--- /dev/null
+++ b/activemodel/lib/active_model/state_machine.rb
@@ -0,0 +1,43 @@
+Dir[File.dirname(__FILE__) + "/state_machine/*.rb"].sort.each do |path|
+ filename = File.basename(path)
+ require "active_model/state_machine/#{filename}"
+end
+
+module ActiveModel
+ module StateMachine
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def inherited(klass)
+ super
+ klass.state_machines = state_machines
+ end
+
+ def state_machines
+ @state_machines ||= {}
+ end
+
+ def state_machines=(value)
+ @state_machines = value ? value.dup : nil
+ end
+
+ def state_machine(name = nil, options = {}, &block)
+ if name.is_a?(Hash)
+ options = name
+ name = nil
+ end
+ name ||= :default
+ state_machines[name] ||= Machine.new(self, name)
+ block ? state_machines[name].update(options, &block) : state_machines[name]
+ end
+ end
+
+ def current_state(name = nil)
+ sm = self.class.state_machine(name)
+ ivar = "@#{sm.name}_current_state"
+ instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state)
+ end
+ end
+end \ No newline at end of file