rubyist-aasm 2.0.2 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ rescue Exception
14
14
  nil
15
15
  end
16
16
 
17
- if `ruby -Ilib -rversion -e "print AASM::VERSION::STRING"` =~ /([0-9.]+)$/
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,7 +4,14 @@ require File.join(File.dirname(__FILE__), 'state_machine')
4
4
  require File.join(File.dirname(__FILE__), 'persistence')
5
5
 
6
6
  module AASM
7
- class InvalidTransition < Exception
7
+ def self.Version
8
+ '2.0.4'
9
+ end
10
+
11
+ class InvalidTransition < RuntimeError
12
+ end
13
+
14
+ class UndefinedState < RuntimeError
8
15
  end
9
16
 
10
17
  def self.included(base) #:nodoc:
@@ -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 aasm_current_state_with_persistence=(state)
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,7 +120,9 @@ 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)
@@ -122,18 +133,21 @@ module AASM
122
133
  unless new_state.nil?
123
134
  aasm_state_object_for_state(new_state).call_action(:enter, self)
124
135
 
125
- if self.respond_to?(:aasm_event_fired)
126
- self.aasm_event_fired(self.aasm_current_state, new_state)
127
- end
128
-
136
+ persist_successful = true
129
137
  if persist
130
- self.aasm_current_state_with_persistence = new_state
131
- self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
138
+ persist_successful = set_aasm_current_state_with_persistence(new_state)
139
+ self.class.aasm_events[name].execute_success_callback(self) if persist_successful
132
140
  else
133
141
  self.aasm_current_state = new_state
134
142
  end
135
143
 
136
- true
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
137
151
  else
138
152
  if self.respond_to?(:aasm_event_failed)
139
153
  self.aasm_event_failed(name)
data/lib/event.rb CHANGED
@@ -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)
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
+
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
- update_attribute(self.class.aasm_column, state.to_s)
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_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
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.0.2
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-07-08 00:00:00 -07:00
12
+ date: 2008-12-22 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
data/lib/version.rb DELETED
@@ -1,5 +0,0 @@
1
- module AASM
2
- module VERSION
3
- STRING = '2.0.2'
4
- end
5
- end