diff options
author | rick <technoweenie@gmail.com> | 2008-06-29 12:13:58 -0700 |
---|---|---|
committer | rick <technoweenie@gmail.com> | 2008-06-29 12:13:58 -0700 |
commit | 01db5ded54b0e3a2ea80d28e4841d40fcec23cdf (patch) | |
tree | 9d30b4c51da7e8f0fb0fc77126450f63fd08e8fa /activemodel/lib/active_model/state_machine.rb | |
parent | 4cf93935b2478201863c01569e894c9dcf7e9074 (diff) | |
parent | c9e366e997c6f3a383cfaa6351fa847e92de7fe4 (diff) | |
download | rails-01db5ded54b0e3a2ea80d28e4841d40fcec23cdf.tar.gz rails-01db5ded54b0e3a2ea80d28e4841d40fcec23cdf.tar.bz2 rails-01db5ded54b0e3a2ea80d28e4841d40fcec23cdf.zip |
Merge branch 'state_machine'
Some big changes:
* Added some redundant requires so active_support/inflecto can be loaded
without the rest of ActiveSupport.
* Disabled callbacks and validations until they are added and tested.
* Converted specs back to tests, using ActiveSupport::TestCase and the new
#test helper.
* As an experiment, I imported Scott Barron's awesome AASM gem into
ActiveModel. I added multiple state machine support and vastly improved
the API (no more aasm_* prefixes). All the old tests pass. If this bothers
people, I have no problems removing this and contributing the changes back to
AASM. I just feel like AMo is a better spot for all these 'modelish' features.
Diffstat (limited to 'activemodel/lib/active_model/state_machine.rb')
-rw-r--r-- | activemodel/lib/active_model/state_machine.rb | 66 |
1 files changed, 66 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..96df6539ae --- /dev/null +++ b/activemodel/lib/active_model/state_machine.rb @@ -0,0 +1,66 @@ +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 + class InvalidTransition < Exception + end + + 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, new_state = nil, persist = false) + sm = self.class.state_machine(name) + ivar = sm.current_state_variable + if name && new_state + if persist && respond_to?(:write_state) + write_state(sm, new_state) + end + + if respond_to?(:write_state_without_persistence) + write_state_without_persistence(sm, new_state) + end + + instance_variable_set(ivar, new_state) + else + instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar) + value = instance_variable_get(ivar) + return value if value + + if respond_to?(:read_state) + value = instance_variable_set(ivar, read_state(sm)) + end + + value || sm.initial_state + end + end + end +end
\ No newline at end of file |