Skip to content

Commit

Permalink
Fixed conflicts after merging with rubyist-aasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacqui Maher committed Sep 17, 2009
2 parents 1ab8287 + dfc2874 commit fe13954
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 259 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ rdoc
pkg
coverage
*~
*.sw?
62 changes: 55 additions & 7 deletions README.rdoc
Expand Up @@ -11,15 +11,33 @@ AASM has the following features:
* Events
* Transitions

== New Callbacks

The callback chain & order on a successful event looks like:

oldstate:exit*
event:before
__find transition, if possible__
transition:on_transition*
oldstate:before_exit
newstate:before_enter
newstate:enter*
__update state__
event:success*
oldstate:after_exit
oldstate:after_enter
event:after
obj:aasm_event_fired*

(*) marks old callbacks


== Download

The latest AASM can currently be pulled from the git repository on github.

* http://github.com/rubyist/aasm/tree/master

A release and a gem are forthcoming.



== Installation

Expand All @@ -31,7 +49,7 @@ A release and a gem are forthcoming.
=== Building your own gems

% rake gem
% sudo gem install pkg/aasm-2.0.1.gem
% sudo gem install pkg/aasm-2.1.gem


== Simple Example
Expand All @@ -57,14 +75,44 @@ Here's a quick example highlighting some of the features.
end
end

== A Slightly More Complex Example

This example uses a few of the more complex features available.

class Relationship
include AASM

aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }

aasm_state :dating, :enter => :make_happy, :exit => :make_depressed
aasm_state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
aasm_state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover

aasm_event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end

aasm_event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
end

def strictly_for_fun?; end
def drunk?; end
def willing_to_give_up_manhood?; end
def make_happy; end
def make_depressed; end
def make_very_happy; end
def never_speak_again; end
def give_up_intimacy; end
def buy_exotic_car_and_wear_a_combover; end
end

= Other Stuff

Author:: Scott Barron <scott at elitists dot net>
License:: Copyright 2006, 2007, 2008 by Scott Barron.
License:: Original code Copyright 2006, 2007, 2008 by Scott Barron.
Released under an MIT-style license. See the LICENSE file
included in the distribution.
Bugs:: http://rubyist.lighthouseapp.com/projects/13207-aasm/
GitHub:: http://github.com/rubyist/aasm/tree/master

== Warranty

Expand Down
19 changes: 10 additions & 9 deletions Rakefile
Expand Up @@ -22,8 +22,8 @@ end
$package_version = CURRENT_VERSION

PKG_FILES = FileList['[A-Z]*',
'lib/**/*.rb',
'doc/**/*'
'lib/**/*.rb',
'doc/**/*'
]

desc 'Generate documentation for the acts as state machine plugin.'
Expand All @@ -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)
Expand All @@ -44,18 +44,19 @@ else
s.name = 'aasm'
s.version = $package_version
s.summary = 'State machine mixin for Ruby objects'
s.description = <<-EOF
AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.
EOF
s.description = <<EOF
AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.
EOF

s.files = PKG_FILES.to_a
s.require_path = 'lib'
s.has_rdoc = true
s.extra_rdoc_files = rd.rdoc_files.reject {|fn| fn =~ /\.rb$/}.to_a
s.rdoc_options = rd.options

s.author = 'Scott Barron'
s.email = 'scott@elitists.net'
s.homepage = 'http://rubyi.st/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|
Expand Down
4 changes: 2 additions & 2 deletions 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", "lib/version.rb", "doc/jamis.rb"]
PKG_FILES = ["CHANGELOG", "MIT-LICENSE", "Rakefile", "README.rdoc", "TODO", "lib/aasm.rb", "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", "doc/jamis.rb"]

Gem::Specification.new do |s|
s.name = 'aasm'
s.version = "2.0.5"
s.version = "2.1.1"
s.summary = 'State machine mixin for Ruby objects'
s.description = <<-EOF
AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects. This fork adds Ruby 1.9.1 compatibility.
Expand Down
1 change: 0 additions & 1 deletion aasm.rb

This file was deleted.

160 changes: 1 addition & 159 deletions lib/aasm.rb
@@ -1,159 +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.0.5'
end

class InvalidTransition < RuntimeError
end

class UndefinedState < RuntimeError
end

def self.included(base) #:nodoc:
# TODO - need to ensure that a machine is being created because
# AASM was either included or arrived at via inheritance. It
# cannot be both.
base.extend AASM::ClassMethods
AASM::Persistence.set_persistence(base)
AASM::StateMachine[base] = AASM::StateMachine.new('')
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
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_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)
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)

new_state = self.class.aasm_events[name].fire(self, *args)

unless new_state.nil?
aasm_state_object_for_state(new_state).call_action(:enter, self)

persist_successful = true
if persist
persist_successful = set_aasm_current_state_with_persistence(new_state)
self.class.aasm_events[name].execute_success_callback(self) if persist_successful
else
self.aasm_current_state = new_state
end

if persist_successful
self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
else
self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
end

persist_successful
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name)
end

false
end
end
end
require File.join(File.dirname(__FILE__), 'aasm', 'aasm')

0 comments on commit fe13954

Please sign in to comment.