eric-aasm 2.0.2 → 2.0.2.1
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 +1 -1
- data/lib/aasm.rb +27 -19
- 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
data/lib/aasm.rb
CHANGED
@@ -4,6 +4,10 @@ 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
|
+
'0.0.2'
|
9
|
+
end
|
10
|
+
|
7
11
|
class InvalidTransition < RuntimeError
|
8
12
|
end
|
9
13
|
|
@@ -14,15 +18,14 @@ module AASM
|
|
14
18
|
base.extend AASM::ClassMethods
|
15
19
|
AASM::Persistence.set_persistence(base)
|
16
20
|
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
21
|
end
|
24
22
|
|
25
23
|
module ClassMethods
|
24
|
+
def inherited(klass)
|
25
|
+
AASM::StateMachine[klass] = AASM::StateMachine[self].clone
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
26
29
|
def aasm_initial_state(set_state=nil)
|
27
30
|
if set_state
|
28
31
|
AASM::StateMachine[self].initial_state = set_state
|
@@ -96,11 +99,14 @@ module AASM
|
|
96
99
|
end
|
97
100
|
|
98
101
|
private
|
99
|
-
def
|
102
|
+
def set_aasm_current_state_with_persistence(state)
|
103
|
+
save_success = true
|
100
104
|
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
|
101
|
-
aasm_write_state(state)
|
105
|
+
save_success = aasm_write_state(state)
|
102
106
|
end
|
103
|
-
self.aasm_current_state = state
|
107
|
+
self.aasm_current_state = state if save_success
|
108
|
+
|
109
|
+
save_success
|
104
110
|
end
|
105
111
|
|
106
112
|
def aasm_current_state=(state)
|
@@ -115,26 +121,28 @@ module AASM
|
|
115
121
|
end
|
116
122
|
|
117
123
|
def aasm_fire_event(name, persist, *args)
|
118
|
-
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
|
124
|
+
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self, *args)
|
119
125
|
|
120
126
|
new_state = self.class.aasm_events[name].fire(self, *args)
|
121
127
|
|
122
128
|
unless new_state.nil?
|
129
|
+
aasm_state_object_for_state(new_state).call_action(:enter, self, *args)
|
130
|
+
|
131
|
+
persist_successful = true
|
123
132
|
if persist
|
124
|
-
|
133
|
+
persist_successful = set_aasm_current_state_with_persistence(new_state)
|
134
|
+
self.class.aasm_events[name].execute_success_callback(self, *args) if persist_successful
|
125
135
|
else
|
126
136
|
self.aasm_current_state = new_state
|
127
137
|
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)
|
138
|
+
|
139
|
+
if persist_successful
|
140
|
+
self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
|
141
|
+
else
|
142
|
+
self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
|
135
143
|
end
|
136
144
|
|
137
|
-
|
145
|
+
persist_successful
|
138
146
|
else
|
139
147
|
if self.respond_to?(:aasm_event_failed)
|
140
148
|
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.2
|
4
|
+
version: 2.0.2.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: 2008-
|
12
|
+
date: 2008-10-27 00:00:00 -07: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
|