From b9528ad3c5379896b00772cb44faf1db0fd882d7 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Jun 2008 00:55:02 -0700 Subject: initial statemachine machine and state classes --- activemodel/lib/active_model/state_machine.rb | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 activemodel/lib/active_model/state_machine.rb (limited to 'activemodel/lib/active_model/state_machine.rb') 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 -- cgit v1.2.3