ttilley-aasm 2.0.7.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.rdoc +25 -9
  2. data/Rakefile +3 -3
  3. data/lib/aasm.rb +28 -13
  4. data/lib/event.rb +23 -2
  5. data/lib/state.rb +2 -0
  6. metadata +1 -1
data/README.rdoc CHANGED
@@ -11,14 +11,32 @@ AASM has the following features:
11
11
  * Events
12
12
  * Transitions
13
13
 
14
- == Download
14
+ == New Callbacks
15
15
 
16
- The latest AASM can currently be pulled from the git repository on github.
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
- A release and a gem are forthcoming.
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 rubyist-aasm
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.0.1.gem
52
+ % sudo gem install pkg/aasm-2.1.gem
35
53
 
36
54
 
37
55
  == Simple Example
@@ -92,11 +110,9 @@ This example uses a few of the more complex features available.
92
110
  = Other Stuff
93
111
 
94
112
  Author:: Scott Barron <scott at elitists dot net>
95
- License:: Copyright 2006, 2007, 2008 by Scott Barron.
113
+ License:: Original code Copyright 2006, 2007, 2008 by Scott Barron.
96
114
  Released under an MIT-style license. See the LICENSE file
97
115
  included in the distribution.
98
- Bugs:: http://rubyist.lighthouseapp.com/projects/13207-aasm/
99
- GitHub:: http://github.com/rubyist/aasm/tree/master
100
116
 
101
117
  == Warranty
102
118
 
data/Rakefile CHANGED
@@ -54,9 +54,9 @@ EOF
54
54
  s.extra_rdoc_files = rd.rdoc_files.reject {|fn| fn =~ /\.rb$/}.to_a
55
55
  s.rdoc_options = rd.options
56
56
 
57
- s.author = 'Scott Barron'
58
- s.email = 'scott@elitists.net'
59
- s.homepage = 'http://rubyi.st/aasm'
57
+ s.authors = ['Scott Barron', 'Scott Petersen']
58
+ s.email = 'petersen@dunedain289.com'
59
+ s.homepage = 'http://github.com/dunedain289/aasm'
60
60
  end
61
61
 
62
62
  package_task = Rake::GemPackageTask.new(spec) do |pkg|
data/lib/aasm.rb CHANGED
@@ -5,7 +5,7 @@ require File.join(File.dirname(__FILE__), 'persistence')
5
5
 
6
6
  module AASM
7
7
  def self.Version
8
- '2.0.7.1'
8
+ '2.1.0'
9
9
  end
10
10
 
11
11
  class InvalidTransition < RuntimeError
@@ -137,32 +137,47 @@ module AASM
137
137
  end
138
138
 
139
139
  def aasm_fire_event(name, persist, *args)
140
- aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
140
+ old_state = aasm_state_object_for_state(aasm_current_state)
141
+ event = self.class.aasm_events[name]
141
142
 
142
- old_state = self.aasm_current_state
143
- new_state = self.class.aasm_events[name].fire(self, *args)
143
+ old_state.call_action(:exit, self)
144
144
 
145
- unless new_state.nil?
146
- aasm_state_object_for_state(new_state).call_action(:enter, self)
145
+ # new event before callback
146
+ event.call_action(:before, self)
147
147
 
148
+ new_state_name = event.fire(self, *args)
149
+
150
+ unless new_state_name.nil?
151
+ new_state = aasm_state_object_for_state(new_state_name)
152
+
153
+ # new before_ callbacks
154
+ old_state.call_action(:before_exit, self)
155
+ new_state.call_action(:before_enter, self)
156
+
157
+ new_state.call_action(:enter, self)
158
+
148
159
  persist_successful = true
149
160
  if persist
150
- persist_successful = set_aasm_current_state_with_persistence(new_state)
151
- self.class.aasm_events[name].execute_success_callback(self) if persist_successful
161
+ persist_successful = set_aasm_current_state_with_persistence(new_state_name)
162
+ event.execute_success_callback(self) if persist_successful
152
163
  else
153
- self.aasm_current_state = new_state
164
+ self.aasm_current_state = new_state_name
154
165
  end
155
166
 
156
- if persist_successful
157
- self.aasm_event_fired(name, old_state, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
167
+ if persist_successful
168
+ old_state.call_action(:after_exit, self)
169
+ new_state.call_action(:after_enter, self)
170
+ event.call_action(:after, self)
171
+
172
+ self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
158
173
  else
159
- self.aasm_event_failed(name, old_state) if self.respond_to?(:aasm_event_failed)
174
+ self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
160
175
  end
161
176
 
162
177
  persist_successful
163
178
  else
164
179
  if self.respond_to?(:aasm_event_failed)
165
- self.aasm_event_failed(name, old_state)
180
+ self.aasm_event_failed(name, old_state.name)
166
181
  end
167
182
 
168
183
  false
data/lib/event.rb CHANGED
@@ -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
7
-
6
+ attr_reader :name, :success, :options
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
 
@@ -32,6 +33,10 @@ module AASM
32
33
  @transitions.any? { |t| t.from == state }
33
34
  end
34
35
 
36
+ def transitions_from_state(state)
37
+ @transitions.select { |t| t.from == state }
38
+ end
39
+
35
40
  def execute_success_callback(obj, success = nil)
36
41
  callback = success || @success
37
42
  case(callback)
@@ -44,6 +49,22 @@ module AASM
44
49
  end
45
50
  end
46
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
+
47
68
  private
48
69
  def transitions(trans_opts)
49
70
  Array(trans_opts[:from]).each do |s|
data/lib/state.rb CHANGED
@@ -22,6 +22,8 @@ module AASM
22
22
  record.send(action)
23
23
  when Proc
24
24
  action.call(record)
25
+ when Array
26
+ action.each { |a| record.send(a) }
25
27
  end
26
28
  end
27
29
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ttilley-aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.7.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron