eric-aasm 2.0.3 → 2.0.4
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/README.rdoc +2 -26
- data/Rakefile +2 -2
- data/lib/aasm.rb +34 -21
- data/lib/event.rb +12 -1
- data/lib/persistence/active_record_persistence.rb +9 -1
- data/lib/state.rb +3 -3
- data/lib/state_machine.rb +6 -0
- data/lib/state_transition.rb +3 -3
- metadata +2 -3
- data/lib/version.rb +0 -5
data/README.rdoc
CHANGED
@@ -44,13 +44,12 @@ Here's a quick example highlighting some of the features.
|
|
44
44
|
aasm_initial_state :new
|
45
45
|
|
46
46
|
aasm_state :new
|
47
|
-
aasm_state :read
|
47
|
+
aasm_state :read
|
48
48
|
aasm_state :closed
|
49
49
|
|
50
50
|
|
51
51
|
aasm_event :view do
|
52
|
-
transitions :to => :read, :from => [:new]
|
53
|
-
:on_transition => :send_received_receipt, :guard => :user_authorized
|
52
|
+
transitions :to => :read, :from => [:new]
|
54
53
|
end
|
55
54
|
|
56
55
|
aasm_event :close do
|
@@ -58,29 +57,6 @@ Here's a quick example highlighting some of the features.
|
|
58
57
|
end
|
59
58
|
end
|
60
59
|
|
61
|
-
|
62
|
-
== Explanation of transitions
|
63
|
-
|
64
|
-
The current state machine has the following process. If we were to call
|
65
|
-
<tt>Conversation#view!</tt>, we would get:
|
66
|
-
|
67
|
-
state:new
|
68
|
-
|
|
69
|
-
| <-- :exit called on state[new]
|
70
|
-
|
|
71
|
-
| <-- :guard called on state[read]
|
72
|
-
|
|
73
|
-
| <-- :on_transition called on state[read]
|
74
|
-
|
|
75
|
-
| <-- aasm_current_state set to "read"
|
76
|
-
|
|
77
|
-
| <-- :success called on state[read]
|
78
|
-
|
|
79
|
-
| <-- :enter called on state[read]
|
80
|
-
|
|
81
|
-
state:read
|
82
|
-
|
83
|
-
|
84
60
|
= Other Stuff
|
85
61
|
|
86
62
|
Author:: Scott Barron <scott at elitists dot net>
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ rescue Exception
|
|
14
14
|
nil
|
15
15
|
end
|
16
16
|
|
17
|
-
if `ruby -Ilib -
|
17
|
+
if `ruby -Ilib -raasm -e "print AASM.Version"` =~ /([0-9.]+)$/
|
18
18
|
CURRENT_VERSION = $1
|
19
19
|
else
|
20
20
|
CURRENT_VERSION = '0.0.0'
|
@@ -71,7 +71,7 @@ else
|
|
71
71
|
Spec::Rake::SpecTask.new('cruise') do |t|
|
72
72
|
t.spec_files = FileList['spec/**/*.rb']
|
73
73
|
t.rcov = true
|
74
|
-
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'Library']
|
74
|
+
t.rcov_opts = ['--exclude', 'spec', '--exclude', 'Library', '--exclude', 'rcov.rb']
|
75
75
|
end
|
76
76
|
|
77
77
|
desc "Run all examples"
|
data/lib/aasm.rb
CHANGED
@@ -4,8 +4,15 @@ require File.join(File.dirname(__FILE__), 'state_machine')
|
|
4
4
|
require File.join(File.dirname(__FILE__), 'persistence')
|
5
5
|
|
6
6
|
module AASM
|
7
|
+
def self.Version
|
8
|
+
'2.0.4'
|
9
|
+
end
|
10
|
+
|
7
11
|
class InvalidTransition < RuntimeError
|
8
12
|
end
|
13
|
+
|
14
|
+
class UndefinedState < RuntimeError
|
15
|
+
end
|
9
16
|
|
10
17
|
def self.included(base) #:nodoc:
|
11
18
|
# TODO - need to ensure that a machine is being created because
|
@@ -14,15 +21,14 @@ module AASM
|
|
14
21
|
base.extend AASM::ClassMethods
|
15
22
|
AASM::Persistence.set_persistence(base)
|
16
23
|
AASM::StateMachine[base] = AASM::StateMachine.new('')
|
17
|
-
|
18
|
-
base.class_eval do
|
19
|
-
def base.inherited(klass)
|
20
|
-
AASM::StateMachine[klass] = AASM::StateMachine[self].dup
|
21
|
-
end
|
22
|
-
end
|
23
24
|
end
|
24
25
|
|
25
26
|
module ClassMethods
|
27
|
+
def inherited(klass)
|
28
|
+
AASM::StateMachine[klass] = AASM::StateMachine[self].clone
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
26
32
|
def aasm_initial_state(set_state=nil)
|
27
33
|
if set_state
|
28
34
|
AASM::StateMachine[self].initial_state = set_state
|
@@ -96,11 +102,14 @@ module AASM
|
|
96
102
|
end
|
97
103
|
|
98
104
|
private
|
99
|
-
def
|
105
|
+
def set_aasm_current_state_with_persistence(state)
|
106
|
+
save_success = true
|
100
107
|
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
|
101
|
-
aasm_write_state(state)
|
108
|
+
save_success = aasm_write_state(state)
|
102
109
|
end
|
103
|
-
self.aasm_current_state = state
|
110
|
+
self.aasm_current_state = state if save_success
|
111
|
+
|
112
|
+
save_success
|
104
113
|
end
|
105
114
|
|
106
115
|
def aasm_current_state=(state)
|
@@ -111,30 +120,34 @@ module AASM
|
|
111
120
|
end
|
112
121
|
|
113
122
|
def aasm_state_object_for_state(name)
|
114
|
-
self.class.aasm_states.find {|s| s == 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
|
115
126
|
end
|
116
127
|
|
117
128
|
def aasm_fire_event(name, persist, *args)
|
118
|
-
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
|
129
|
+
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self, *args)
|
119
130
|
|
120
|
-
new_state = self.class.aasm_events[name].fire(self, *args)
|
131
|
+
new_state = self.class.aasm_events[name].fire(self, nil, *args)
|
121
132
|
|
122
133
|
unless new_state.nil?
|
134
|
+
aasm_state_object_for_state(new_state).call_action(:enter, self, *args)
|
135
|
+
|
136
|
+
persist_successful = true
|
123
137
|
if persist
|
124
|
-
|
138
|
+
persist_successful = set_aasm_current_state_with_persistence(new_state)
|
139
|
+
self.class.aasm_events[name].execute_success_callback(self, *args) if persist_successful
|
125
140
|
else
|
126
141
|
self.aasm_current_state = new_state
|
127
142
|
end
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
if self.respond_to?(:aasm_event_fired)
|
134
|
-
self.aasm_event_fired(self.aasm_current_state, new_state)
|
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)
|
135
148
|
end
|
136
149
|
|
137
|
-
|
150
|
+
persist_successful
|
138
151
|
else
|
139
152
|
if self.respond_to?(:aasm_event_failed)
|
140
153
|
self.aasm_event_failed(name)
|
data/lib/event.rb
CHANGED
@@ -19,7 +19,7 @@ module AASM
|
|
19
19
|
next_state = nil
|
20
20
|
transitions.each do |transition|
|
21
21
|
next if to_state and !Array(transition.to).include?(to_state)
|
22
|
-
if transition.perform(obj)
|
22
|
+
if transition.perform(obj, *args)
|
23
23
|
next_state = to_state || Array(transition.to).first
|
24
24
|
transition.execute(obj, *args)
|
25
25
|
break
|
@@ -32,6 +32,17 @@ module AASM
|
|
32
32
|
@transitions.any? { |t| t.from == state }
|
33
33
|
end
|
34
34
|
|
35
|
+
def execute_success_callback(obj, *args)
|
36
|
+
case success
|
37
|
+
when String, Symbol:
|
38
|
+
obj.send(success, *args)
|
39
|
+
when Array:
|
40
|
+
success.each { |meth| obj.send(meth, *args) }
|
41
|
+
when Proc:
|
42
|
+
success.call(obj, *args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
35
46
|
private
|
36
47
|
def transitions(trans_opts)
|
37
48
|
Array(trans_opts[:from]).each do |s|
|
@@ -188,7 +188,15 @@ module AASM
|
|
188
188
|
#
|
189
189
|
# NOTE: intended to be called from an event
|
190
190
|
def aasm_write_state(state)
|
191
|
-
|
191
|
+
old_value = read_attribute(self.class.aasm_column)
|
192
|
+
write_attribute(self.class.aasm_column, state.to_s)
|
193
|
+
|
194
|
+
unless self.save
|
195
|
+
write_attribute(self.class.aasm_column, old_value)
|
196
|
+
return false
|
197
|
+
end
|
198
|
+
|
199
|
+
true
|
192
200
|
end
|
193
201
|
end
|
194
202
|
|
data/lib/state.rb
CHANGED
@@ -15,13 +15,13 @@ module AASM
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def call_action(action, record)
|
18
|
+
def call_action(action, record, *args)
|
19
19
|
action = @options[action]
|
20
20
|
case action
|
21
21
|
when Symbol, String
|
22
|
-
record.send(action)
|
22
|
+
record.send(action, *args)
|
23
23
|
when Proc
|
24
|
-
action.call(record)
|
24
|
+
action.call(record, *args)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
data/lib/state_machine.rb
CHANGED
@@ -22,6 +22,12 @@ module AASM
|
|
22
22
|
@config = OpenStruct.new
|
23
23
|
end
|
24
24
|
|
25
|
+
def clone
|
26
|
+
klone = super
|
27
|
+
klone.states = states.clone
|
28
|
+
klone
|
29
|
+
end
|
30
|
+
|
25
31
|
def create_state(name, options)
|
26
32
|
@states << AASM::SupportingClasses::State.new(name, options) unless @states.include?(name)
|
27
33
|
end
|
data/lib/state_transition.rb
CHANGED
@@ -8,12 +8,12 @@ module AASM
|
|
8
8
|
@opts = opts
|
9
9
|
end
|
10
10
|
|
11
|
-
def perform(obj)
|
11
|
+
def perform(obj, *args)
|
12
12
|
case @guard
|
13
13
|
when Symbol, String
|
14
|
-
obj.send(@guard)
|
14
|
+
obj.send(@guard, *args)
|
15
15
|
when Proc
|
16
|
-
@guard.call(obj)
|
16
|
+
@guard.call(obj, *args)
|
17
17
|
else
|
18
18
|
true
|
19
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eric-aasm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.4
|
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: 2008-
|
12
|
+
date: 2008-12-22 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -37,7 +37,6 @@ files:
|
|
37
37
|
- lib/state.rb
|
38
38
|
- lib/state_machine.rb
|
39
39
|
- lib/state_transition.rb
|
40
|
-
- lib/version.rb
|
41
40
|
- doc/jamis.rb
|
42
41
|
has_rdoc: true
|
43
42
|
homepage: http://github.com/rubyist/aasm
|