snoozer05-aasm 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +33 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +77 -0
- data/Rakefile +94 -0
- data/TODO +9 -0
- data/doc/jamis.rb +591 -0
- data/lib/aasm.rb +155 -0
- data/lib/event.rb +54 -0
- data/lib/persistence.rb +16 -0
- data/lib/persistence/active_record_persistence.rb +246 -0
- data/lib/state.rb +33 -0
- data/lib/state_machine.rb +29 -0
- data/lib/state_transition.rb +36 -0
- data/lib/version.rb +9 -0
- metadata +74 -0
data/lib/aasm.rb
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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.0.2'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class InvalidTransition < RuntimeError
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.included(base) #:nodoc:
|
|
15
|
+
# TODO - need to ensure that a machine is being created because
|
|
16
|
+
# AASM was either included or arrived at via inheritance. It
|
|
17
|
+
# cannot be both.
|
|
18
|
+
base.extend AASM::ClassMethods
|
|
19
|
+
AASM::Persistence.set_persistence(base)
|
|
20
|
+
AASM::StateMachine[base] = AASM::StateMachine.new('')
|
|
21
|
+
|
|
22
|
+
base.class_eval do
|
|
23
|
+
def base.inherited(klass)
|
|
24
|
+
AASM::StateMachine[klass] = AASM::StateMachine[self].dup
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module ClassMethods
|
|
30
|
+
def initial_state(set_state=nil)
|
|
31
|
+
if set_state
|
|
32
|
+
AASM::StateMachine[self].initial_state = set_state
|
|
33
|
+
else
|
|
34
|
+
AASM::StateMachine[self].initial_state
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initial_state=(state)
|
|
39
|
+
AASM::StateMachine[self].initial_state = state
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def state(name, options={})
|
|
43
|
+
sm = AASM::StateMachine[self]
|
|
44
|
+
sm.create_state(name, options)
|
|
45
|
+
sm.initial_state = name unless sm.initial_state
|
|
46
|
+
|
|
47
|
+
define_method("#{name.to_s}?") do
|
|
48
|
+
current_state == name
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def event(name, options = {}, &block)
|
|
53
|
+
sm = AASM::StateMachine[self]
|
|
54
|
+
|
|
55
|
+
unless sm.events.has_key?(name)
|
|
56
|
+
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
define_method("#{name.to_s}!") do |*args|
|
|
60
|
+
fire_event(name, true, *args)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
define_method("#{name.to_s}") do |*args|
|
|
64
|
+
fire_event(name, false, *args)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def states
|
|
69
|
+
AASM::StateMachine[self].states
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def events
|
|
73
|
+
AASM::StateMachine[self].events
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def states_for_select
|
|
77
|
+
AASM::StateMachine[self].states.map { |state| state.for_select }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Instance methods
|
|
83
|
+
def current_state
|
|
84
|
+
return @current_state if @current_state
|
|
85
|
+
|
|
86
|
+
if self.respond_to?(:read_state) || self.private_methods.include?('read_state')
|
|
87
|
+
@current_state = read_state
|
|
88
|
+
end
|
|
89
|
+
return @current_state if @current_state
|
|
90
|
+
self.class.initial_state
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def events_for_current_state
|
|
94
|
+
events_for_state(current_state)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def events_for_state(state)
|
|
98
|
+
events = self.class.events.values.select {|event| event.transitions_from_state?(state) }
|
|
99
|
+
events.map {|event| event.name}
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
def set_current_state_with_persistence(state)
|
|
104
|
+
save_success = true
|
|
105
|
+
if self.respond_to?(:write_state) || self.private_methods.include?('write_state')
|
|
106
|
+
save_success = write_state(state)
|
|
107
|
+
end
|
|
108
|
+
self.current_state = state if save_success
|
|
109
|
+
|
|
110
|
+
save_success
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def current_state=(state)
|
|
114
|
+
if self.respond_to?(:write_state_without_persistence) || self.private_methods.include?('write_state_without_persistence')
|
|
115
|
+
write_state_without_persistence(state)
|
|
116
|
+
end
|
|
117
|
+
@current_state = state
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def state_object_for_state(name)
|
|
121
|
+
self.class.states.find {|s| s == name}
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def fire_event(name, persist, *args)
|
|
125
|
+
state_object_for_state(current_state).call_action(:exit, self)
|
|
126
|
+
|
|
127
|
+
new_state = self.class.events[name].fire(self, *args)
|
|
128
|
+
|
|
129
|
+
unless new_state.nil?
|
|
130
|
+
state_object_for_state(new_state).call_action(:enter, self)
|
|
131
|
+
|
|
132
|
+
persist_successful = true
|
|
133
|
+
if persist
|
|
134
|
+
persist_successful = set_current_state_with_persistence(new_state)
|
|
135
|
+
self.class.events[name].execute_success_callback(self) if persist_successful
|
|
136
|
+
else
|
|
137
|
+
self.current_state = new_state
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
if persist_successful
|
|
141
|
+
self.event_fired(self.current_state, new_state) if self.respond_to?(:event_fired)
|
|
142
|
+
else
|
|
143
|
+
self.event_failed(name) if self.respond_to?(:event_failed)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
persist_successful
|
|
147
|
+
else
|
|
148
|
+
if self.respond_to?(:event_failed)
|
|
149
|
+
self.event_failed(name)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
false
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
data/lib/event.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'state_transition')
|
|
2
|
+
|
|
3
|
+
module AASM
|
|
4
|
+
module SupportingClasses
|
|
5
|
+
class Event
|
|
6
|
+
attr_reader :name, :success
|
|
7
|
+
|
|
8
|
+
def initialize(name, options = {}, &block)
|
|
9
|
+
@name = name
|
|
10
|
+
@success = options[:success]
|
|
11
|
+
@transitions = []
|
|
12
|
+
instance_eval(&block) if block
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def fire(obj, to_state=nil, *args)
|
|
16
|
+
transitions = @transitions.select { |t| t.from == obj.current_state }
|
|
17
|
+
raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.current_state}'" if transitions.size == 0
|
|
18
|
+
|
|
19
|
+
next_state = nil
|
|
20
|
+
transitions.each do |transition|
|
|
21
|
+
next if to_state and !Array(transition.to).include?(to_state)
|
|
22
|
+
if transition.perform(obj)
|
|
23
|
+
next_state = to_state || Array(transition.to).first
|
|
24
|
+
transition.execute(obj, *args)
|
|
25
|
+
break
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
next_state
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def transitions_from_state?(state)
|
|
32
|
+
@transitions.any? { |t| t.from == state }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def execute_success_callback(obj)
|
|
36
|
+
case success
|
|
37
|
+
when String, Symbol:
|
|
38
|
+
obj.send(success)
|
|
39
|
+
when Array:
|
|
40
|
+
success.each { |meth| obj.send(meth) }
|
|
41
|
+
when Proc:
|
|
42
|
+
success.call(obj)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
def transitions(trans_opts)
|
|
48
|
+
Array(trans_opts[:from]).each do |s|
|
|
49
|
+
@transitions << SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/persistence.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module AASM
|
|
2
|
+
module Persistence
|
|
3
|
+
|
|
4
|
+
# Checks to see this class or any of it's superclasses inherit from
|
|
5
|
+
# ActiveRecord::Base and if so includes ActiveRecordPersistence
|
|
6
|
+
def self.set_persistence(base)
|
|
7
|
+
# Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
|
|
8
|
+
hierarchy = base.ancestors.map {|klass| klass.to_s}
|
|
9
|
+
|
|
10
|
+
if hierarchy.include?("ActiveRecord::Base")
|
|
11
|
+
require File.join(File.dirname(__FILE__), 'persistence', 'active_record_persistence')
|
|
12
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
module AASM
|
|
2
|
+
module Persistence
|
|
3
|
+
module ActiveRecordPersistence
|
|
4
|
+
# This method:
|
|
5
|
+
#
|
|
6
|
+
# * extends the model with ClassMethods
|
|
7
|
+
# * includes InstanceMethods
|
|
8
|
+
#
|
|
9
|
+
# Unless the corresponding methods are already defined, it includes
|
|
10
|
+
# * ReadState
|
|
11
|
+
# * WriteState
|
|
12
|
+
# * WriteStateWithoutPersistence
|
|
13
|
+
#
|
|
14
|
+
# Adds
|
|
15
|
+
#
|
|
16
|
+
# before_validation_on_create :ensure_initial_state
|
|
17
|
+
#
|
|
18
|
+
# As a result, it doesn't matter when you define your methods - the following 2 are equivalent
|
|
19
|
+
#
|
|
20
|
+
# class Foo < ActiveRecord::Base
|
|
21
|
+
# def write_state(state)
|
|
22
|
+
# "bar"
|
|
23
|
+
# end
|
|
24
|
+
# include AASM
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# class Foo < ActiveRecord::Base
|
|
28
|
+
# include AASM
|
|
29
|
+
# def write_state(state)
|
|
30
|
+
# "bar"
|
|
31
|
+
# end
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
def self.included(base)
|
|
35
|
+
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
|
|
36
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
|
37
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:read_state)
|
|
38
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:write_state)
|
|
39
|
+
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:write_state_without_persistence)
|
|
40
|
+
|
|
41
|
+
if base.respond_to?(:named_scope)
|
|
42
|
+
base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
|
|
43
|
+
|
|
44
|
+
base.class_eval do
|
|
45
|
+
class << self
|
|
46
|
+
alias_method :state_without_named_scope, :state
|
|
47
|
+
alias_method :state, :state_with_named_scope
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
base.before_validation_on_create :ensure_initial_state
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
module ClassMethods
|
|
56
|
+
# Maps to the aasm_column in the database. Deafults to "state". You can write:
|
|
57
|
+
#
|
|
58
|
+
# create_table :foos do |t|
|
|
59
|
+
# t.string :name
|
|
60
|
+
# t.string :state
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
63
|
+
# class Foo < ActiveRecord::Base
|
|
64
|
+
# include AASM
|
|
65
|
+
# end
|
|
66
|
+
#
|
|
67
|
+
# OR:
|
|
68
|
+
#
|
|
69
|
+
# create_table :foos do |t|
|
|
70
|
+
# t.string :name
|
|
71
|
+
# t.string :status
|
|
72
|
+
# end
|
|
73
|
+
#
|
|
74
|
+
# class Foo < ActiveRecord::Base
|
|
75
|
+
# include AASM
|
|
76
|
+
# state_column :status
|
|
77
|
+
# end
|
|
78
|
+
#
|
|
79
|
+
# This method is both a getter and a setter
|
|
80
|
+
def state_column(column_name=nil)
|
|
81
|
+
if column_name
|
|
82
|
+
AASM::StateMachine[self].config.column = column_name.to_sym
|
|
83
|
+
# @state_column = column_name.to_sym
|
|
84
|
+
else
|
|
85
|
+
AASM::StateMachine[self].config.column ||= :state
|
|
86
|
+
# @state_column ||= :state
|
|
87
|
+
end
|
|
88
|
+
# @state_column
|
|
89
|
+
AASM::StateMachine[self].config.column
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def find_in_state(number, state, *args)
|
|
93
|
+
with_state_scope state do
|
|
94
|
+
find(number, *args)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def count_in_state(state, *args)
|
|
99
|
+
with_state_scope state do
|
|
100
|
+
count(*args)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def calculate_in_state(state, *args)
|
|
105
|
+
with_state_scope state do
|
|
106
|
+
calculate(*args)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
protected
|
|
111
|
+
def with_state_scope(state)
|
|
112
|
+
with_scope :find => {:conditions => ["#{table_name}.#{state_column} = ?", state.to_s]} do
|
|
113
|
+
yield if block_given?
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
module InstanceMethods
|
|
119
|
+
|
|
120
|
+
# Returns the current state of the object. Respects reload and
|
|
121
|
+
# any changes made to the state field directly
|
|
122
|
+
#
|
|
123
|
+
# Internally just calls <tt>aasm_read_state</tt>
|
|
124
|
+
#
|
|
125
|
+
# foo = Foo.find(1)
|
|
126
|
+
# foo.current_state # => :pending
|
|
127
|
+
# foo.state = "opened"
|
|
128
|
+
# foo.current_state # => :opened
|
|
129
|
+
# foo.close # => calls write_state_without_persistence
|
|
130
|
+
# foo.current_state # => :closed
|
|
131
|
+
# foo.reload
|
|
132
|
+
# foo.current_state # => :pending
|
|
133
|
+
#
|
|
134
|
+
def current_state
|
|
135
|
+
@current_state = read_state
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
# Ensures that if the state column is nil and the record is new
|
|
141
|
+
# that the initial state gets populated before validation on create
|
|
142
|
+
#
|
|
143
|
+
# foo = Foo.new
|
|
144
|
+
# foo.state # => nil
|
|
145
|
+
# foo.valid?
|
|
146
|
+
# foo.state # => "open" (where :open is the initial state)
|
|
147
|
+
#
|
|
148
|
+
#
|
|
149
|
+
# foo = Foo.find(:first)
|
|
150
|
+
# foo.state # => 1
|
|
151
|
+
# foo.state = nil
|
|
152
|
+
# foo.valid?
|
|
153
|
+
# foo.state # => nil
|
|
154
|
+
#
|
|
155
|
+
def ensure_initial_state
|
|
156
|
+
send("#{self.class.state_column}=", self.current_state.to_s)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
module WriteStateWithoutPersistence
|
|
162
|
+
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
|
163
|
+
#
|
|
164
|
+
# foo = Foo.find(1)
|
|
165
|
+
# foo.current_state # => :opened
|
|
166
|
+
# foo.close
|
|
167
|
+
# foo.current_state # => :closed
|
|
168
|
+
# Foo.find(1).current_state # => :opened
|
|
169
|
+
# foo.save
|
|
170
|
+
# foo.current_state # => :closed
|
|
171
|
+
# Foo.find(1).current_state # => :closed
|
|
172
|
+
#
|
|
173
|
+
# NOTE: intended to be called from an event
|
|
174
|
+
def write_state_without_persistence(state)
|
|
175
|
+
write_attribute(self.class.state_column, state.to_s)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
module WriteState
|
|
180
|
+
# Writes <tt>state</tt> to the state column and persists it to the database
|
|
181
|
+
# using update_attribute (which bypasses validation)
|
|
182
|
+
#
|
|
183
|
+
# foo = Foo.find(1)
|
|
184
|
+
# foo.current_state # => :opened
|
|
185
|
+
# foo.close!
|
|
186
|
+
# foo.current_state # => :closed
|
|
187
|
+
# Foo.find(1).aasm_current_state # => :closed
|
|
188
|
+
#
|
|
189
|
+
# NOTE: intended to be called from an event
|
|
190
|
+
def write_state(state)
|
|
191
|
+
old_value = read_attribute(self.class.state_column)
|
|
192
|
+
write_attribute(self.class.state_column, state.to_s)
|
|
193
|
+
|
|
194
|
+
unless self.save
|
|
195
|
+
write_attribute(self.class.state_column, old_value)
|
|
196
|
+
return false
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
true
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
module ReadState
|
|
204
|
+
|
|
205
|
+
# Returns the value of the aasm_column - called from <tt>aasm_current_state</tt>
|
|
206
|
+
#
|
|
207
|
+
# If it's a new record, and the aasm state column is blank it returns the initial state:
|
|
208
|
+
#
|
|
209
|
+
# class Foo < ActiveRecord::Base
|
|
210
|
+
# include AASM
|
|
211
|
+
# column :status
|
|
212
|
+
# state :opened
|
|
213
|
+
# state :closed
|
|
214
|
+
# end
|
|
215
|
+
#
|
|
216
|
+
# foo = Foo.new
|
|
217
|
+
# foo.current_state # => :opened
|
|
218
|
+
# foo.close
|
|
219
|
+
# foo.current_state # => :closed
|
|
220
|
+
#
|
|
221
|
+
# foo = Foo.find(1)
|
|
222
|
+
# foo.current_state # => :opened
|
|
223
|
+
# foo.state = nil
|
|
224
|
+
# foo.current_state # => nil
|
|
225
|
+
#
|
|
226
|
+
# NOTE: intended to be called from an event
|
|
227
|
+
#
|
|
228
|
+
# This allows for nil aasm states - be sure to add validation to your model
|
|
229
|
+
def read_state
|
|
230
|
+
if new_record?
|
|
231
|
+
send(self.class.state_column).blank? ? self.class.initial_state : send(self.class.state_column).to_sym
|
|
232
|
+
else
|
|
233
|
+
send(self.class.state_column).nil? ? nil : send(self.class.state_column).to_sym
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
module NamedScopeMethods
|
|
239
|
+
def state_with_named_scope name, options = {}
|
|
240
|
+
state_without_named_scope name, options
|
|
241
|
+
self.named_scope name, :conditions => {self.state_column => name.to_s} unless self.scopes.include?(name)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|