From 1e4c038eeec9c6a2a751196a9e36f165e144b721 Mon Sep 17 00:00:00 2001 From: Travis Tilley Date: Sat, 8 Aug 2009 16:53:47 -0400 Subject: [PATCH] reorganize to match rubyist/aasm --- Rakefile | 8 +- aasm.gemspec | 4 +- aasm.rb | 1 - lib/aasm.rb | 186 +----------------- lib/aasm/aasm.rb | 185 +++++++++++++++++ lib/{ => aasm}/event.rb | 0 lib/{ => aasm}/persistence.rb | 0 .../persistence/active_record_persistence.rb | 0 lib/{ => aasm}/state.rb | 0 lib/{ => aasm}/state_machine.rb | 0 lib/{ => aasm}/state_transition.rb | 0 spec/spec_helper.rb | 2 +- 12 files changed, 193 insertions(+), 193 deletions(-) delete mode 100644 aasm.rb create mode 100644 lib/aasm/aasm.rb rename lib/{ => aasm}/event.rb (100%) rename lib/{ => aasm}/persistence.rb (100%) rename lib/{ => aasm}/persistence/active_record_persistence.rb (100%) rename lib/{ => aasm}/state.rb (100%) rename lib/{ => aasm}/state_machine.rb (100%) rename lib/{ => aasm}/state_transition.rb (100%) diff --git a/Rakefile b/Rakefile index 8f4c7f6..a547215 100644 --- a/Rakefile +++ b/Rakefile @@ -34,7 +34,7 @@ rd = Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.title = 'AASM' rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README.rdoc' << '--title' << 'AASM' rdoc.rdoc_files.include('README.rdoc', 'MIT-LICENSE', 'TODO', 'CHANGELOG') - rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc') + rdoc.rdoc_files.include('lib/*.rb', 'lib/**/*.rb', 'doc/**/*.rdoc') end if !defined?(Gem) @@ -54,9 +54,9 @@ EOF s.extra_rdoc_files = rd.rdoc_files.reject {|fn| fn =~ /\.rb$/}.to_a s.rdoc_options = rd.options - s.authors = ['Scott Barron', 'Scott Petersen'] - s.email = 'petersen@dunedain289.com' - s.homepage = 'http://github.com/dunedain289/aasm' + s.authors = ['Scott Barron', 'Scott Petersen', 'Travis Tilley'] + s.email = 'ttilley@gmail.com' + s.homepage = 'http://github.com/ttilley/aasm' end package_task = Rake::GemPackageTask.new(spec) do |pkg| diff --git a/aasm.gemspec b/aasm.gemspec index df7d5c9..c6dc2e2 100644 --- a/aasm.gemspec +++ b/aasm.gemspec @@ -1,8 +1,8 @@ -PKG_FILES = ["CHANGELOG", "MIT-LICENSE", "Rakefile", "README.rdoc", "TODO", "lib/aasm.rb", "lib/event.rb", "lib/persistence/active_record_persistence.rb", "lib/persistence.rb", "lib/state.rb", "lib/state_machine.rb", "lib/state_transition.rb", "doc/jamis.rb"] +PKG_FILES = ["CHANGELOG", "MIT-LICENSE", "Rakefile", "README.rdoc", "TODO", "lib/aasm/aasm.rb", "lib/aasm/event.rb", "lib/aasm/persistence/active_record_persistence.rb", "lib/aasm/persistence.rb", "lib/aasm/state.rb", "lib/aasm/state_machine.rb", "lib/aasm/state_transition.rb", "lib/aasm.rb", "doc/jamis.rb"] Gem::Specification.new do |s| s.name = 'aasm' - s.version = "2.1.0" + s.version = "2.1.1" s.summary = %q{State machine mixin for Ruby objects} s.description = %q{AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.} diff --git a/aasm.rb b/aasm.rb deleted file mode 100644 index 2d02590..0000000 --- a/aasm.rb +++ /dev/null @@ -1 +0,0 @@ -require File.join(File.dirname(__FILE__), 'lib', 'aasm') diff --git a/lib/aasm.rb b/lib/aasm.rb index 2e1de3b..09d4a7c 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -1,185 +1 @@ -require File.join(File.dirname(__FILE__), 'event') -require File.join(File.dirname(__FILE__), 'state') -require File.join(File.dirname(__FILE__), 'state_machine') -require File.join(File.dirname(__FILE__), 'persistence') - -module AASM - def self.Version - '2.1.0' - end - - class InvalidTransition < RuntimeError - end - - class UndefinedState < RuntimeError - end - - def self.included(base) #:nodoc: - base.extend AASM::ClassMethods - AASM::Persistence.set_persistence(base) - unless AASM::StateMachine[base] - AASM::StateMachine[base] = AASM::StateMachine.new('') - end - end - - module ClassMethods - def inherited(klass) - AASM::StateMachine[klass] = AASM::StateMachine[self].clone - super - end - - def aasm_initial_state(set_state=nil) - if set_state - AASM::StateMachine[self].initial_state = set_state - else - AASM::StateMachine[self].initial_state - end - end - - def aasm_initial_state=(state) - AASM::StateMachine[self].initial_state = state - end - - def aasm_state(name, options={}) - sm = AASM::StateMachine[self] - sm.create_state(name, options) - sm.initial_state = name unless sm.initial_state - - define_method("#{name.to_s}?") do - aasm_current_state == name - end - end - - def aasm_event(name, options = {}, &block) - sm = AASM::StateMachine[self] - - unless sm.events.has_key?(name) - sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block) - end - - define_method("#{name.to_s}!") do |*args| - aasm_fire_event(name, true, *args) - end - - define_method("#{name.to_s}") do |*args| - aasm_fire_event(name, false, *args) - end - end - - def aasm_states - AASM::StateMachine[self].states - end - - def aasm_events - AASM::StateMachine[self].events - end - - def aasm_states_for_select - AASM::StateMachine[self].states.map { |state| state.for_select } - end - - end - - # Instance methods - def aasm_current_state - return @aasm_current_state if @aasm_current_state - - if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state') - @aasm_current_state = aasm_read_state - end - return @aasm_current_state if @aasm_current_state - aasm_determine_state_name(self.class.aasm_initial_state) - end - - def aasm_events_for_current_state - aasm_events_for_state(aasm_current_state) - end - - def aasm_events_for_state(state) - events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) } - events.map {|event| event.name} - end - - private - def set_aasm_current_state_with_persistence(state) - save_success = true - if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state') - save_success = aasm_write_state(state) - end - self.aasm_current_state = state if save_success - - save_success - end - - def aasm_current_state=(state) - if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence') - aasm_write_state_without_persistence(state) - end - @aasm_current_state = state - end - - def aasm_determine_state_name(state) - case state - when Symbol, String - state - when Proc - state.call(self) - else - raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc." - end - end - - def aasm_state_object_for_state(name) - obj = self.class.aasm_states.find {|s| s == name} - raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil? - obj - end - - def aasm_fire_event(name, persist, *args) - old_state = aasm_state_object_for_state(aasm_current_state) - event = self.class.aasm_events[name] - - old_state.call_action(:exit, self) - - # new event before callback - event.call_action(:before, self) - - new_state_name = event.fire(self, *args) - - unless new_state_name.nil? - new_state = aasm_state_object_for_state(new_state_name) - - # new before_ callbacks - old_state.call_action(:before_exit, self) - new_state.call_action(:before_enter, self) - - new_state.call_action(:enter, self) - - persist_successful = true - if persist - persist_successful = set_aasm_current_state_with_persistence(new_state_name) - event.execute_success_callback(self) if persist_successful - else - self.aasm_current_state = new_state_name - end - - if persist_successful - old_state.call_action(:after_exit, self) - new_state.call_action(:after_enter, self) - event.call_action(:after, self) - - self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired) - else - self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed) - end - - persist_successful - else - if self.respond_to?(:aasm_event_failed) - self.aasm_event_failed(name, old_state.name) - end - - false - end - end -end +require File.join(File.dirname(__FILE__), 'aasm', 'aasm') diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb new file mode 100644 index 0000000..9c23162 --- /dev/null +++ b/lib/aasm/aasm.rb @@ -0,0 +1,185 @@ +require File.join(File.dirname(__FILE__), 'event') +require File.join(File.dirname(__FILE__), 'state') +require File.join(File.dirname(__FILE__), 'state_machine') +require File.join(File.dirname(__FILE__), 'persistence') + +module AASM + def self.Version + '2.1.1' + end + + class InvalidTransition < RuntimeError + end + + class UndefinedState < RuntimeError + end + + def self.included(base) #:nodoc: + base.extend AASM::ClassMethods + AASM::Persistence.set_persistence(base) + unless AASM::StateMachine[base] + AASM::StateMachine[base] = AASM::StateMachine.new('') + end + end + + module ClassMethods + def inherited(klass) + AASM::StateMachine[klass] = AASM::StateMachine[self].clone + super + end + + def aasm_initial_state(set_state=nil) + if set_state + AASM::StateMachine[self].initial_state = set_state + else + AASM::StateMachine[self].initial_state + end + end + + def aasm_initial_state=(state) + AASM::StateMachine[self].initial_state = state + end + + def aasm_state(name, options={}) + sm = AASM::StateMachine[self] + sm.create_state(name, options) + sm.initial_state = name unless sm.initial_state + + define_method("#{name.to_s}?") do + aasm_current_state == name + end + end + + def aasm_event(name, options = {}, &block) + sm = AASM::StateMachine[self] + + unless sm.events.has_key?(name) + sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block) + end + + define_method("#{name.to_s}!") do |*args| + aasm_fire_event(name, true, *args) + end + + define_method("#{name.to_s}") do |*args| + aasm_fire_event(name, false, *args) + end + end + + def aasm_states + AASM::StateMachine[self].states + end + + def aasm_events + AASM::StateMachine[self].events + end + + def aasm_states_for_select + AASM::StateMachine[self].states.map { |state| state.for_select } + end + + end + + # Instance methods + def aasm_current_state + return @aasm_current_state if @aasm_current_state + + if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state') + @aasm_current_state = aasm_read_state + end + return @aasm_current_state if @aasm_current_state + aasm_determine_state_name(self.class.aasm_initial_state) + end + + def aasm_events_for_current_state + aasm_events_for_state(aasm_current_state) + end + + def aasm_events_for_state(state) + events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) } + events.map {|event| event.name} + end + + private + def set_aasm_current_state_with_persistence(state) + save_success = true + if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state') + save_success = aasm_write_state(state) + end + self.aasm_current_state = state if save_success + + save_success + end + + def aasm_current_state=(state) + if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence') + aasm_write_state_without_persistence(state) + end + @aasm_current_state = state + end + + def aasm_determine_state_name(state) + case state + when Symbol, String + state + when Proc + state.call(self) + else + raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc." + end + end + + def aasm_state_object_for_state(name) + obj = self.class.aasm_states.find {|s| s == name} + raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil? + obj + end + + def aasm_fire_event(name, persist, *args) + old_state = aasm_state_object_for_state(aasm_current_state) + event = self.class.aasm_events[name] + + old_state.call_action(:exit, self) + + # new event before callback + event.call_action(:before, self) + + new_state_name = event.fire(self, *args) + + unless new_state_name.nil? + new_state = aasm_state_object_for_state(new_state_name) + + # new before_ callbacks + old_state.call_action(:before_exit, self) + new_state.call_action(:before_enter, self) + + new_state.call_action(:enter, self) + + persist_successful = true + if persist + persist_successful = set_aasm_current_state_with_persistence(new_state_name) + event.execute_success_callback(self) if persist_successful + else + self.aasm_current_state = new_state_name + end + + if persist_successful + old_state.call_action(:after_exit, self) + new_state.call_action(:after_enter, self) + event.call_action(:after, self) + + self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired) + else + self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed) + end + + persist_successful + else + if self.respond_to?(:aasm_event_failed) + self.aasm_event_failed(name, old_state.name) + end + + false + end + end +end diff --git a/lib/event.rb b/lib/aasm/event.rb similarity index 100% rename from lib/event.rb rename to lib/aasm/event.rb diff --git a/lib/persistence.rb b/lib/aasm/persistence.rb similarity index 100% rename from lib/persistence.rb rename to lib/aasm/persistence.rb diff --git a/lib/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb similarity index 100% rename from lib/persistence/active_record_persistence.rb rename to lib/aasm/persistence/active_record_persistence.rb diff --git a/lib/state.rb b/lib/aasm/state.rb similarity index 100% rename from lib/state.rb rename to lib/aasm/state.rb diff --git a/lib/state_machine.rb b/lib/aasm/state_machine.rb similarity index 100% rename from lib/state_machine.rb rename to lib/aasm/state_machine.rb diff --git a/lib/state_transition.rb b/lib/aasm/state_transition.rb similarity index 100% rename from lib/state_transition.rb rename to lib/aasm/state_transition.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d8a95fa..7619412 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,2 @@ require 'spec' -require File.join(File.dirname(__FILE__), '..', 'aasm') +require File.join(File.dirname(__FILE__), '..', 'lib', 'aasm', 'aasm')