diff --git a/lib/aasm/state_machine.rb b/lib/aasm/state_machine.rb index 92313cf..5e76955 100644 --- a/lib/aasm/state_machine.rb +++ b/lib/aasm/state_machine.rb @@ -1,13 +1,12 @@ class AASM::StateMachine - def self.[](*args) - (@machines ||= {})[args] + def self.[](clazz) + (@machines ||= {})[clazz.to_s] end - - def self.[]=(*args) - val = args.pop - (@machines ||= {})[args] = val + + def self.[]=(clazz, machine) + (@machines ||= {})[clazz.to_s] = machine end - + attr_accessor :states, :events, :initial_state, :config attr_reader :name diff --git a/test/models/process.rb b/test/models/process.rb new file mode 100644 index 0000000..dc9f809 --- /dev/null +++ b/test/models/process.rb @@ -0,0 +1,18 @@ +module Models + class Process + include AASM + + aasm_state :sleeping + aasm_state :running + aasm_state :suspended + + aasm_event :start do + transitions :from => :sleeping, :to => :running + end + + aasm_event :stop do + transitions :from => :running, :to => :suspended + end + + end +end diff --git a/test/unit/state_machine_test.rb b/test/unit/state_machine_test.rb new file mode 100644 index 0000000..6bf4f01 --- /dev/null +++ b/test/unit/state_machine_test.rb @@ -0,0 +1,37 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') + +class StateMachineTest < Test::Unit::TestCase + + context "state machines" do + + should "be created without memory leak" do + assert_equal 1, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + assert_number_of_objects AASM::SupportingClasses::State, 5 # AuthMachine + assert_number_of_objects AASM::SupportingClasses::Event, 10 # AuthMachine + assert_number_of_objects AASM::SupportingClasses::StateTransition, 18 # AuthMachine + + load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb') + assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process + assert_number_of_objects Models::Process, 0 + assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process + assert_number_of_objects AASM::SupportingClasses::Event, 12 # AuthMachine + Process + assert_number_of_objects AASM::SupportingClasses::StateTransition, 20 # AuthMachine + Process + + Models.send(:remove_const, "Process") if Models.const_defined?("Process") + load File.expand_path(File.dirname(__FILE__) + '/../models/process.rb') + assert_equal 2, AASM::StateMachine.instance_variable_get("@machines").size # AuthMachine + Process + assert_number_of_objects AASM::SupportingClasses::State, 8 # AuthMachine + Process + assert_number_of_objects AASM::SupportingClasses::Event, 12 # AuthMachine + Process + assert_number_of_objects AASM::SupportingClasses::StateTransition, 20 # AuthMachine + Process + end + + end + + private + + def assert_number_of_objects(clazz, num) + count = ObjectSpace.each_object(clazz) {} + assert_equal num, count, "#{num} expected, but we had #{count} #{clazz}" + end + +end