rubyist-aasm 2.0.5 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +57 -9
- data/Rakefile +10 -9
- data/lib/aasm/aasm.rb +185 -0
- data/lib/{event.rb → aasm/event.rb} +32 -10
- data/lib/{persistence → aasm/persistence}/active_record_persistence.rb +7 -5
- data/lib/{persistence.rb → aasm/persistence.rb} +2 -2
- data/lib/{state.rb → aasm/state.rb} +2 -0
- data/lib/{state_machine.rb → aasm/state_machine.rb} +2 -2
- data/lib/{state_transition.rb → aasm/state_transition.rb} +1 -1
- data/lib/aasm.rb +1 -159
- metadata +11 -10
data/README.rdoc
CHANGED
@@ -11,14 +11,32 @@ AASM has the following features:
|
|
11
11
|
* Events
|
12
12
|
* Transitions
|
13
13
|
|
14
|
-
==
|
14
|
+
== New Callbacks
|
15
15
|
|
16
|
-
The
|
16
|
+
The callback chain & order on a successful event looks like:
|
17
|
+
|
18
|
+
oldstate:exit*
|
19
|
+
event:before
|
20
|
+
__find transition, if possible__
|
21
|
+
transition:on_transition*
|
22
|
+
oldstate:before_exit
|
23
|
+
newstate:before_enter
|
24
|
+
newstate:enter*
|
25
|
+
__update state__
|
26
|
+
event:success*
|
27
|
+
oldstate:after_exit
|
28
|
+
oldstate:after_enter
|
29
|
+
event:after
|
30
|
+
obj:aasm_event_fired*
|
31
|
+
|
32
|
+
(*) marks old callbacks
|
17
33
|
|
18
|
-
* http://github.com/rubyist/aasm/tree/master
|
19
34
|
|
20
|
-
|
35
|
+
== Download
|
36
|
+
|
37
|
+
The latest AASM can currently be pulled from the git repository on github.
|
21
38
|
|
39
|
+
* http://github.com/ttilley/aasm/tree/master
|
22
40
|
|
23
41
|
|
24
42
|
== Installation
|
@@ -26,12 +44,12 @@ A release and a gem are forthcoming.
|
|
26
44
|
=== From GitHub hosted gems
|
27
45
|
|
28
46
|
% sudo gem sources -a http://gems.github.com # (you only need to do this once)
|
29
|
-
% sudo gem install
|
47
|
+
% sudo gem install ttilley-aasm
|
30
48
|
|
31
49
|
=== Building your own gems
|
32
50
|
|
33
51
|
% rake gem
|
34
|
-
% sudo gem install pkg/aasm-2.
|
52
|
+
% sudo gem install pkg/aasm-2.1.gem
|
35
53
|
|
36
54
|
|
37
55
|
== Simple Example
|
@@ -57,14 +75,44 @@ Here's a quick example highlighting some of the features.
|
|
57
75
|
end
|
58
76
|
end
|
59
77
|
|
78
|
+
== A Slightly More Complex Example
|
79
|
+
|
80
|
+
This example uses a few of the more complex features available.
|
81
|
+
|
82
|
+
class Relationship
|
83
|
+
include AASM
|
84
|
+
|
85
|
+
aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }
|
86
|
+
|
87
|
+
aasm_state :dating, :enter => :make_happy, :exit => :make_depressed
|
88
|
+
aasm_state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
|
89
|
+
aasm_state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover
|
90
|
+
|
91
|
+
aasm_event :get_intimate do
|
92
|
+
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
|
93
|
+
end
|
94
|
+
|
95
|
+
aasm_event :get_married do
|
96
|
+
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
|
97
|
+
end
|
98
|
+
|
99
|
+
def strictly_for_fun?; end
|
100
|
+
def drunk?; end
|
101
|
+
def willing_to_give_up_manhood?; end
|
102
|
+
def make_happy; end
|
103
|
+
def make_depressed; end
|
104
|
+
def make_very_happy; end
|
105
|
+
def never_speak_again; end
|
106
|
+
def give_up_intimacy; end
|
107
|
+
def buy_exotic_car_and_wear_a_combover; end
|
108
|
+
end
|
109
|
+
|
60
110
|
= Other Stuff
|
61
111
|
|
62
112
|
Author:: Scott Barron <scott at elitists dot net>
|
63
|
-
License:: Copyright 2006, 2007, 2008 by Scott Barron.
|
113
|
+
License:: Original code Copyright 2006, 2007, 2008 by Scott Barron.
|
64
114
|
Released under an MIT-style license. See the LICENSE file
|
65
115
|
included in the distribution.
|
66
|
-
Bugs:: http://rubyist.lighthouseapp.com/projects/13207-aasm/
|
67
|
-
GitHub:: http://github.com/rubyist/aasm/tree/master
|
68
116
|
|
69
117
|
== Warranty
|
70
118
|
|
data/Rakefile
CHANGED
@@ -22,8 +22,8 @@ end
|
|
22
22
|
$package_version = CURRENT_VERSION
|
23
23
|
|
24
24
|
PKG_FILES = FileList['[A-Z]*',
|
25
|
-
'lib/**/*.rb',
|
26
|
-
'doc/**/*'
|
25
|
+
'lib/**/*.rb',
|
26
|
+
'doc/**/*'
|
27
27
|
]
|
28
28
|
|
29
29
|
desc 'Generate documentation for the acts as state machine plugin.'
|
@@ -34,7 +34,7 @@ rd = Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
34
34
|
rdoc.title = 'AASM'
|
35
35
|
rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README.rdoc' << '--title' << 'AASM'
|
36
36
|
rdoc.rdoc_files.include('README.rdoc', 'MIT-LICENSE', 'TODO', 'CHANGELOG')
|
37
|
-
rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
|
37
|
+
rdoc.rdoc_files.include('lib/*.rb', 'lib/**/*.rb', 'doc/**/*.rdoc')
|
38
38
|
end
|
39
39
|
|
40
40
|
if !defined?(Gem)
|
@@ -44,18 +44,19 @@ else
|
|
44
44
|
s.name = 'aasm'
|
45
45
|
s.version = $package_version
|
46
46
|
s.summary = 'State machine mixin for Ruby objects'
|
47
|
-
s.description =
|
48
|
-
|
49
|
-
|
47
|
+
s.description = <<EOF
|
48
|
+
AASM is a continuation of the acts as state machine rails plugin, built for plain Ruby objects.
|
49
|
+
EOF
|
50
|
+
|
50
51
|
s.files = PKG_FILES.to_a
|
51
52
|
s.require_path = 'lib'
|
52
53
|
s.has_rdoc = true
|
53
54
|
s.extra_rdoc_files = rd.rdoc_files.reject {|fn| fn =~ /\.rb$/}.to_a
|
54
55
|
s.rdoc_options = rd.options
|
55
56
|
|
56
|
-
s.
|
57
|
-
s.email = '
|
58
|
-
s.homepage = 'http://
|
57
|
+
s.authors = ['Scott Barron', 'Scott Petersen', 'Travis Tilley']
|
58
|
+
s.email = 'ttilley@gmail.com'
|
59
|
+
s.homepage = 'http://github.com/ttilley/aasm'
|
59
60
|
end
|
60
61
|
|
61
62
|
package_task = Rake::GemPackageTask.new(spec) do |pkg|
|
data/lib/aasm/aasm.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'event')
|
2
|
+
require File.join(File.dirname(__FILE__), 'state')
|
3
|
+
require File.join(File.dirname(__FILE__), 'state_machine')
|
4
|
+
require File.join(File.dirname(__FILE__), 'persistence')
|
5
|
+
|
6
|
+
module AASM
|
7
|
+
def self.Version
|
8
|
+
'2.1.1'
|
9
|
+
end
|
10
|
+
|
11
|
+
class InvalidTransition < RuntimeError
|
12
|
+
end
|
13
|
+
|
14
|
+
class UndefinedState < RuntimeError
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.included(base) #:nodoc:
|
18
|
+
base.extend AASM::ClassMethods
|
19
|
+
AASM::Persistence.set_persistence(base)
|
20
|
+
unless AASM::StateMachine[base]
|
21
|
+
AASM::StateMachine[base] = AASM::StateMachine.new('')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def inherited(klass)
|
27
|
+
AASM::StateMachine[klass] = AASM::StateMachine[self].clone
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def aasm_initial_state(set_state=nil)
|
32
|
+
if set_state
|
33
|
+
AASM::StateMachine[self].initial_state = set_state
|
34
|
+
else
|
35
|
+
AASM::StateMachine[self].initial_state
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def aasm_initial_state=(state)
|
40
|
+
AASM::StateMachine[self].initial_state = state
|
41
|
+
end
|
42
|
+
|
43
|
+
def aasm_state(name, options={})
|
44
|
+
sm = AASM::StateMachine[self]
|
45
|
+
sm.create_state(name, options)
|
46
|
+
sm.initial_state = name unless sm.initial_state
|
47
|
+
|
48
|
+
define_method("#{name.to_s}?") do
|
49
|
+
aasm_current_state == name
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def aasm_event(name, options = {}, &block)
|
54
|
+
sm = AASM::StateMachine[self]
|
55
|
+
|
56
|
+
unless sm.events.has_key?(name)
|
57
|
+
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method("#{name.to_s}!") do |*args|
|
61
|
+
aasm_fire_event(name, true, *args)
|
62
|
+
end
|
63
|
+
|
64
|
+
define_method("#{name.to_s}") do |*args|
|
65
|
+
aasm_fire_event(name, false, *args)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def aasm_states
|
70
|
+
AASM::StateMachine[self].states
|
71
|
+
end
|
72
|
+
|
73
|
+
def aasm_events
|
74
|
+
AASM::StateMachine[self].events
|
75
|
+
end
|
76
|
+
|
77
|
+
def aasm_states_for_select
|
78
|
+
AASM::StateMachine[self].states.map { |state| state.for_select }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
# Instance methods
|
84
|
+
def aasm_current_state
|
85
|
+
return @aasm_current_state if @aasm_current_state
|
86
|
+
|
87
|
+
if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
|
88
|
+
@aasm_current_state = aasm_read_state
|
89
|
+
end
|
90
|
+
return @aasm_current_state if @aasm_current_state
|
91
|
+
aasm_determine_state_name(self.class.aasm_initial_state)
|
92
|
+
end
|
93
|
+
|
94
|
+
def aasm_events_for_current_state
|
95
|
+
aasm_events_for_state(aasm_current_state)
|
96
|
+
end
|
97
|
+
|
98
|
+
def aasm_events_for_state(state)
|
99
|
+
events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
|
100
|
+
events.map {|event| event.name}
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def set_aasm_current_state_with_persistence(state)
|
105
|
+
save_success = true
|
106
|
+
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
|
107
|
+
save_success = aasm_write_state(state)
|
108
|
+
end
|
109
|
+
self.aasm_current_state = state if save_success
|
110
|
+
|
111
|
+
save_success
|
112
|
+
end
|
113
|
+
|
114
|
+
def aasm_current_state=(state)
|
115
|
+
if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
|
116
|
+
aasm_write_state_without_persistence(state)
|
117
|
+
end
|
118
|
+
@aasm_current_state = state
|
119
|
+
end
|
120
|
+
|
121
|
+
def aasm_determine_state_name(state)
|
122
|
+
case state
|
123
|
+
when Symbol, String
|
124
|
+
state
|
125
|
+
when Proc
|
126
|
+
state.call(self)
|
127
|
+
else
|
128
|
+
raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def aasm_state_object_for_state(name)
|
133
|
+
obj = self.class.aasm_states.find {|s| s == name}
|
134
|
+
raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
|
135
|
+
obj
|
136
|
+
end
|
137
|
+
|
138
|
+
def aasm_fire_event(name, persist, *args)
|
139
|
+
old_state = aasm_state_object_for_state(aasm_current_state)
|
140
|
+
event = self.class.aasm_events[name]
|
141
|
+
|
142
|
+
old_state.call_action(:exit, self)
|
143
|
+
|
144
|
+
# new event before callback
|
145
|
+
event.call_action(:before, self)
|
146
|
+
|
147
|
+
new_state_name = event.fire(self, *args)
|
148
|
+
|
149
|
+
unless new_state_name.nil?
|
150
|
+
new_state = aasm_state_object_for_state(new_state_name)
|
151
|
+
|
152
|
+
# new before_ callbacks
|
153
|
+
old_state.call_action(:before_exit, self)
|
154
|
+
new_state.call_action(:before_enter, self)
|
155
|
+
|
156
|
+
new_state.call_action(:enter, self)
|
157
|
+
|
158
|
+
persist_successful = true
|
159
|
+
if persist
|
160
|
+
persist_successful = set_aasm_current_state_with_persistence(new_state_name)
|
161
|
+
event.execute_success_callback(self) if persist_successful
|
162
|
+
else
|
163
|
+
self.aasm_current_state = new_state_name
|
164
|
+
end
|
165
|
+
|
166
|
+
if persist_successful
|
167
|
+
old_state.call_action(:after_exit, self)
|
168
|
+
new_state.call_action(:after_enter, self)
|
169
|
+
event.call_action(:after, self)
|
170
|
+
|
171
|
+
self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
|
172
|
+
else
|
173
|
+
self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
|
174
|
+
end
|
175
|
+
|
176
|
+
persist_successful
|
177
|
+
else
|
178
|
+
if self.respond_to?(:aasm_event_failed)
|
179
|
+
self.aasm_event_failed(name, old_state.name)
|
180
|
+
end
|
181
|
+
|
182
|
+
false
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -3,12 +3,13 @@ require File.join(File.dirname(__FILE__), 'state_transition')
|
|
3
3
|
module AASM
|
4
4
|
module SupportingClasses
|
5
5
|
class Event
|
6
|
-
attr_reader :name, :success
|
6
|
+
attr_reader :name, :success, :options
|
7
7
|
|
8
8
|
def initialize(name, options = {}, &block)
|
9
9
|
@name = name
|
10
10
|
@success = options[:success]
|
11
11
|
@transitions = []
|
12
|
+
@options = options
|
12
13
|
instance_eval(&block) if block
|
13
14
|
end
|
14
15
|
|
@@ -31,18 +32,39 @@ module AASM
|
|
31
32
|
def transitions_from_state?(state)
|
32
33
|
@transitions.any? { |t| t.from == state }
|
33
34
|
end
|
34
|
-
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
35
|
+
|
36
|
+
def transitions_from_state(state)
|
37
|
+
@transitions.select { |t| t.from == state }
|
38
|
+
end
|
39
|
+
|
40
|
+
def execute_success_callback(obj, success = nil)
|
41
|
+
callback = success || @success
|
42
|
+
case(callback)
|
43
|
+
when String, Symbol
|
44
|
+
obj.send(callback)
|
45
|
+
when Proc
|
46
|
+
callback.call(obj)
|
47
|
+
when Array
|
48
|
+
callback.each{|meth|self.execute_success_callback(obj, meth)}
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
52
|
+
def call_action(action, record)
|
53
|
+
action = @options[action]
|
54
|
+
case action
|
55
|
+
when Symbol, String
|
56
|
+
record.send(action)
|
57
|
+
when Proc
|
58
|
+
action.call(record)
|
59
|
+
when Array
|
60
|
+
action.each { |a| record.send(a) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def all_transitions
|
65
|
+
@transitions
|
66
|
+
end
|
67
|
+
|
46
68
|
private
|
47
69
|
def transitions(trans_opts)
|
48
70
|
Array(trans_opts[:from]).each do |s|
|
@@ -43,8 +43,10 @@ module AASM
|
|
43
43
|
|
44
44
|
base.class_eval do
|
45
45
|
class << self
|
46
|
-
|
47
|
-
|
46
|
+
unless method_defined?(:aasm_state_without_named_scope)
|
47
|
+
alias_method :aasm_state_without_named_scope, :aasm_state
|
48
|
+
alias_method :aasm_state, :aasm_state_with_named_scope
|
49
|
+
end
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
@@ -191,7 +193,7 @@ module AASM
|
|
191
193
|
old_value = read_attribute(self.class.aasm_column)
|
192
194
|
write_attribute(self.class.aasm_column, state.to_s)
|
193
195
|
|
194
|
-
unless self.save
|
196
|
+
unless self.save(false)
|
195
197
|
write_attribute(self.class.aasm_column, old_value)
|
196
198
|
return false
|
197
199
|
end
|
@@ -228,7 +230,7 @@ module AASM
|
|
228
230
|
# This allows for nil aasm states - be sure to add validation to your model
|
229
231
|
def aasm_read_state
|
230
232
|
if new_record?
|
231
|
-
send(self.class.aasm_column).blank? ? self.class.aasm_initial_state : send(self.class.aasm_column).to_sym
|
233
|
+
send(self.class.aasm_column).blank? ? aasm_determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
|
232
234
|
else
|
233
235
|
send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
|
234
236
|
end
|
@@ -238,7 +240,7 @@ module AASM
|
|
238
240
|
module NamedScopeMethods
|
239
241
|
def aasm_state_with_named_scope name, options = {}
|
240
242
|
aasm_state_without_named_scope name, options
|
241
|
-
self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.respond_to?(name)
|
243
|
+
self.named_scope name, :conditions => { "#{table_name}.#{self.aasm_column}" => name.to_s} unless self.respond_to?(name)
|
242
244
|
end
|
243
245
|
end
|
244
246
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module AASM
|
2
2
|
module Persistence
|
3
|
-
|
3
|
+
|
4
4
|
# Checks to see this class or any of it's superclasses inherit from
|
5
5
|
# ActiveRecord::Base and if so includes ActiveRecordPersistence
|
6
6
|
def self.set_persistence(base)
|
7
7
|
# Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
|
8
8
|
hierarchy = base.ancestors.map {|klass| klass.to_s}
|
9
|
-
|
9
|
+
|
10
10
|
if hierarchy.include?("ActiveRecord::Base")
|
11
11
|
require File.join(File.dirname(__FILE__), 'persistence', 'active_record_persistence')
|
12
12
|
base.send(:include, AASM::Persistence::ActiveRecordPersistence)
|
data/lib/aasm.rb
CHANGED
@@ -1,159 +1 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '
|
2
|
-
require File.join(File.dirname(__FILE__), 'state')
|
3
|
-
require File.join(File.dirname(__FILE__), 'state_machine')
|
4
|
-
require File.join(File.dirname(__FILE__), 'persistence')
|
5
|
-
|
6
|
-
module AASM
|
7
|
-
def self.Version
|
8
|
-
'2.0.5'
|
9
|
-
end
|
10
|
-
|
11
|
-
class InvalidTransition < RuntimeError
|
12
|
-
end
|
13
|
-
|
14
|
-
class UndefinedState < RuntimeError
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.included(base) #:nodoc:
|
18
|
-
# TODO - need to ensure that a machine is being created because
|
19
|
-
# AASM was either included or arrived at via inheritance. It
|
20
|
-
# cannot be both.
|
21
|
-
base.extend AASM::ClassMethods
|
22
|
-
AASM::Persistence.set_persistence(base)
|
23
|
-
AASM::StateMachine[base] = AASM::StateMachine.new('')
|
24
|
-
end
|
25
|
-
|
26
|
-
module ClassMethods
|
27
|
-
def inherited(klass)
|
28
|
-
AASM::StateMachine[klass] = AASM::StateMachine[self].clone
|
29
|
-
super
|
30
|
-
end
|
31
|
-
|
32
|
-
def aasm_initial_state(set_state=nil)
|
33
|
-
if set_state
|
34
|
-
AASM::StateMachine[self].initial_state = set_state
|
35
|
-
else
|
36
|
-
AASM::StateMachine[self].initial_state
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def aasm_initial_state=(state)
|
41
|
-
AASM::StateMachine[self].initial_state = state
|
42
|
-
end
|
43
|
-
|
44
|
-
def aasm_state(name, options={})
|
45
|
-
sm = AASM::StateMachine[self]
|
46
|
-
sm.create_state(name, options)
|
47
|
-
sm.initial_state = name unless sm.initial_state
|
48
|
-
|
49
|
-
define_method("#{name.to_s}?") do
|
50
|
-
aasm_current_state == name
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def aasm_event(name, options = {}, &block)
|
55
|
-
sm = AASM::StateMachine[self]
|
56
|
-
|
57
|
-
unless sm.events.has_key?(name)
|
58
|
-
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
59
|
-
end
|
60
|
-
|
61
|
-
define_method("#{name.to_s}!") do |*args|
|
62
|
-
aasm_fire_event(name, true, *args)
|
63
|
-
end
|
64
|
-
|
65
|
-
define_method("#{name.to_s}") do |*args|
|
66
|
-
aasm_fire_event(name, false, *args)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def aasm_states
|
71
|
-
AASM::StateMachine[self].states
|
72
|
-
end
|
73
|
-
|
74
|
-
def aasm_events
|
75
|
-
AASM::StateMachine[self].events
|
76
|
-
end
|
77
|
-
|
78
|
-
def aasm_states_for_select
|
79
|
-
AASM::StateMachine[self].states.map { |state| state.for_select }
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
# Instance methods
|
85
|
-
def aasm_current_state
|
86
|
-
return @aasm_current_state if @aasm_current_state
|
87
|
-
|
88
|
-
if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
|
89
|
-
@aasm_current_state = aasm_read_state
|
90
|
-
end
|
91
|
-
return @aasm_current_state if @aasm_current_state
|
92
|
-
self.class.aasm_initial_state
|
93
|
-
end
|
94
|
-
|
95
|
-
def aasm_events_for_current_state
|
96
|
-
aasm_events_for_state(aasm_current_state)
|
97
|
-
end
|
98
|
-
|
99
|
-
def aasm_events_for_state(state)
|
100
|
-
events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
|
101
|
-
events.map {|event| event.name}
|
102
|
-
end
|
103
|
-
|
104
|
-
private
|
105
|
-
def set_aasm_current_state_with_persistence(state)
|
106
|
-
save_success = true
|
107
|
-
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
|
108
|
-
save_success = aasm_write_state(state)
|
109
|
-
end
|
110
|
-
self.aasm_current_state = state if save_success
|
111
|
-
|
112
|
-
save_success
|
113
|
-
end
|
114
|
-
|
115
|
-
def aasm_current_state=(state)
|
116
|
-
if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
|
117
|
-
aasm_write_state_without_persistence(state)
|
118
|
-
end
|
119
|
-
@aasm_current_state = state
|
120
|
-
end
|
121
|
-
|
122
|
-
def aasm_state_object_for_state(name)
|
123
|
-
obj = self.class.aasm_states.find {|s| s == name}
|
124
|
-
raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
|
125
|
-
obj
|
126
|
-
end
|
127
|
-
|
128
|
-
def aasm_fire_event(name, persist, *args)
|
129
|
-
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
|
130
|
-
|
131
|
-
new_state = self.class.aasm_events[name].fire(self, *args)
|
132
|
-
|
133
|
-
unless new_state.nil?
|
134
|
-
aasm_state_object_for_state(new_state).call_action(:enter, self)
|
135
|
-
|
136
|
-
persist_successful = true
|
137
|
-
if persist
|
138
|
-
persist_successful = set_aasm_current_state_with_persistence(new_state)
|
139
|
-
self.class.aasm_events[name].execute_success_callback(self) if persist_successful
|
140
|
-
else
|
141
|
-
self.aasm_current_state = new_state
|
142
|
-
end
|
143
|
-
|
144
|
-
if persist_successful
|
145
|
-
self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
|
146
|
-
else
|
147
|
-
self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
|
148
|
-
end
|
149
|
-
|
150
|
-
persist_successful
|
151
|
-
else
|
152
|
-
if self.respond_to?(:aasm_event_failed)
|
153
|
-
self.aasm_event_failed(name)
|
154
|
-
end
|
155
|
-
|
156
|
-
false
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
1
|
+
require File.join(File.dirname(__FILE__), 'aasm', 'aasm')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyist-aasm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Barron
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -31,16 +31,17 @@ files:
|
|
31
31
|
- README.rdoc
|
32
32
|
- TODO
|
33
33
|
- lib/aasm.rb
|
34
|
-
- lib/
|
35
|
-
- lib/
|
36
|
-
- lib/persistence.rb
|
37
|
-
- lib/
|
38
|
-
- lib/
|
39
|
-
- lib/
|
40
|
-
- lib/
|
34
|
+
- lib/aasm/aasm.rb
|
35
|
+
- lib/aasm/event.rb
|
36
|
+
- lib/aasm/persistence/active_record_persistence.rb
|
37
|
+
- lib/aasm/persistence.rb
|
38
|
+
- lib/aasm/state.rb
|
39
|
+
- lib/aasm/state_machine.rb
|
40
|
+
- lib/aasm/state_transition.rb
|
41
41
|
- doc/jamis.rb
|
42
42
|
has_rdoc: true
|
43
43
|
homepage: http://github.com/rubyist/aasm
|
44
|
+
licenses:
|
44
45
|
post_install_message:
|
45
46
|
rdoc_options:
|
46
47
|
- --line-numbers
|
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
67
|
requirements: []
|
67
68
|
|
68
69
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.3.5
|
70
71
|
signing_key:
|
71
72
|
specification_version: 2
|
72
73
|
summary: State machine mixin for Ruby objects
|