ttilley-aasm 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -34,7 +34,7 @@ rd = Rake::RDocTask.new(:rdoc) do |rdoc|
34
34
  rdoc.title = 'AASM'
35
35
  rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README.rdoc' << '--title' << 'AASM'
36
36
  rdoc.rdoc_files.include('README.rdoc', 'MIT-LICENSE', 'TODO', 'CHANGELOG')
37
- rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
37
+ rdoc.rdoc_files.include('lib/*.rb', 'lib/**/*.rb', 'doc/**/*.rdoc')
38
38
  end
39
39
 
40
40
  if !defined?(Gem)
@@ -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.authors = ['Scott Barron', 'Scott Petersen']
58
- s.email = 'petersen@dunedain289.com'
59
- s.homepage = 'http://github.com/dunedain289/aasm'
57
+ s.authors = ['Scott Barron', 'Scott Petersen', 'Travis Tilley']
58
+ s.email = 'ttilley@gmail.com'
59
+ s.homepage = 'http://github.com/ttilley/aasm'
60
60
  end
61
61
 
62
62
  package_task = Rake::GemPackageTask.new(spec) do |pkg|
data/lib/aasm.rb CHANGED
@@ -1,186 +1 @@
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.1.0'
9
- end
10
-
11
- class InvalidTransition < RuntimeError
12
- end
13
-
14
- class UndefinedState < RuntimeError
15
- end
16
-
17
- def self.included(base) #:nodoc:
18
- # TODO - need to ensure that a machine is being created because
19
- # AASM was either included or arrived at via inheritance. It
20
- # cannot be both.
21
- base.extend AASM::ClassMethods
22
- AASM::Persistence.set_persistence(base)
23
- AASM::StateMachine[base] = AASM::StateMachine.new('')
24
- end
25
-
26
- module ClassMethods
27
- def inherited(klass)
28
- AASM::StateMachine[klass] = AASM::StateMachine[self].clone
29
- super
30
- end
31
-
32
- def aasm_initial_state(set_state=nil)
33
- if set_state
34
- AASM::StateMachine[self].initial_state = set_state
35
- else
36
- AASM::StateMachine[self].initial_state
37
- end
38
- end
39
-
40
- def aasm_initial_state=(state)
41
- AASM::StateMachine[self].initial_state = state
42
- end
43
-
44
- def aasm_state(name, options={})
45
- sm = AASM::StateMachine[self]
46
- sm.create_state(name, options)
47
- sm.initial_state = name unless sm.initial_state
48
-
49
- define_method("#{name.to_s}?") do
50
- aasm_current_state == name
51
- end
52
- end
53
-
54
- def aasm_event(name, options = {}, &block)
55
- sm = AASM::StateMachine[self]
56
-
57
- unless sm.events.has_key?(name)
58
- sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
59
- end
60
-
61
- define_method("#{name.to_s}!") do |*args|
62
- aasm_fire_event(name, true, *args)
63
- end
64
-
65
- define_method("#{name.to_s}") do |*args|
66
- aasm_fire_event(name, false, *args)
67
- end
68
- end
69
-
70
- def aasm_states
71
- AASM::StateMachine[self].states
72
- end
73
-
74
- def aasm_events
75
- AASM::StateMachine[self].events
76
- end
77
-
78
- def aasm_states_for_select
79
- AASM::StateMachine[self].states.map { |state| state.for_select }
80
- end
81
-
82
- end
83
-
84
- # Instance methods
85
- def aasm_current_state
86
- return @aasm_current_state if @aasm_current_state
87
-
88
- if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
89
- @aasm_current_state = aasm_read_state
90
- end
91
- return @aasm_current_state if @aasm_current_state
92
- aasm_determine_state_name(self.class.aasm_initial_state)
93
- end
94
-
95
- def aasm_events_for_current_state
96
- aasm_events_for_state(aasm_current_state)
97
- end
98
-
99
- def aasm_events_for_state(state)
100
- events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
101
- events.map {|event| event.name}
102
- end
103
-
104
- private
105
- def set_aasm_current_state_with_persistence(state)
106
- save_success = true
107
- if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
108
- save_success = aasm_write_state(state)
109
- end
110
- self.aasm_current_state = state if save_success
111
-
112
- save_success
113
- end
114
-
115
- def aasm_current_state=(state)
116
- if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
117
- aasm_write_state_without_persistence(state)
118
- end
119
- @aasm_current_state = state
120
- end
121
-
122
- def aasm_determine_state_name(state)
123
- case state
124
- when Symbol, String
125
- state
126
- when Proc
127
- state.call(self)
128
- else
129
- raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
130
- end
131
- end
132
-
133
- def aasm_state_object_for_state(name)
134
- obj = self.class.aasm_states.find {|s| s == name}
135
- raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
136
- obj
137
- end
138
-
139
- def aasm_fire_event(name, persist, *args)
140
- old_state = aasm_state_object_for_state(aasm_current_state)
141
- event = self.class.aasm_events[name]
142
-
143
- old_state.call_action(:exit, self)
144
-
145
- # new event before callback
146
- event.call_action(:before, self)
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
-
159
- persist_successful = true
160
- if persist
161
- persist_successful = set_aasm_current_state_with_persistence(new_state_name)
162
- event.execute_success_callback(self) if persist_successful
163
- else
164
- self.aasm_current_state = new_state_name
165
- end
166
-
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)
173
- else
174
- self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
175
- end
176
-
177
- persist_successful
178
- else
179
- if self.respond_to?(:aasm_event_failed)
180
- self.aasm_event_failed(name, old_state.name)
181
- end
182
-
183
- false
184
- end
185
- end
186
- end
1
+ require File.join(File.dirname(__FILE__), 'aasm', 'aasm')
data/lib/aasm/aasm.rb ADDED
@@ -0,0 +1,185 @@
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.1.1'
9
+ end
10
+
11
+ class InvalidTransition < RuntimeError
12
+ end
13
+
14
+ class UndefinedState < RuntimeError
15
+ end
16
+
17
+ def self.included(base) #:nodoc:
18
+ base.extend AASM::ClassMethods
19
+ AASM::Persistence.set_persistence(base)
20
+ unless AASM::StateMachine[base]
21
+ AASM::StateMachine[base] = AASM::StateMachine.new('')
22
+ end
23
+ end
24
+
25
+ module ClassMethods
26
+ def inherited(klass)
27
+ AASM::StateMachine[klass] = AASM::StateMachine[self].clone
28
+ super
29
+ end
30
+
31
+ def aasm_initial_state(set_state=nil)
32
+ if set_state
33
+ AASM::StateMachine[self].initial_state = set_state
34
+ else
35
+ AASM::StateMachine[self].initial_state
36
+ end
37
+ end
38
+
39
+ def aasm_initial_state=(state)
40
+ AASM::StateMachine[self].initial_state = state
41
+ end
42
+
43
+ def aasm_state(name, options={})
44
+ sm = AASM::StateMachine[self]
45
+ sm.create_state(name, options)
46
+ sm.initial_state = name unless sm.initial_state
47
+
48
+ define_method("#{name.to_s}?") do
49
+ aasm_current_state == name
50
+ end
51
+ end
52
+
53
+ def aasm_event(name, options = {}, &block)
54
+ sm = AASM::StateMachine[self]
55
+
56
+ unless sm.events.has_key?(name)
57
+ sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
58
+ end
59
+
60
+ define_method("#{name.to_s}!") do |*args|
61
+ aasm_fire_event(name, true, *args)
62
+ end
63
+
64
+ define_method("#{name.to_s}") do |*args|
65
+ aasm_fire_event(name, false, *args)
66
+ end
67
+ end
68
+
69
+ def aasm_states
70
+ AASM::StateMachine[self].states
71
+ end
72
+
73
+ def aasm_events
74
+ AASM::StateMachine[self].events
75
+ end
76
+
77
+ def aasm_states_for_select
78
+ AASM::StateMachine[self].states.map { |state| state.for_select }
79
+ end
80
+
81
+ end
82
+
83
+ # Instance methods
84
+ def aasm_current_state
85
+ return @aasm_current_state if @aasm_current_state
86
+
87
+ if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
88
+ @aasm_current_state = aasm_read_state
89
+ end
90
+ return @aasm_current_state if @aasm_current_state
91
+ aasm_determine_state_name(self.class.aasm_initial_state)
92
+ end
93
+
94
+ def aasm_events_for_current_state
95
+ aasm_events_for_state(aasm_current_state)
96
+ end
97
+
98
+ def aasm_events_for_state(state)
99
+ events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
100
+ events.map {|event| event.name}
101
+ end
102
+
103
+ private
104
+ def set_aasm_current_state_with_persistence(state)
105
+ save_success = true
106
+ if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
107
+ save_success = aasm_write_state(state)
108
+ end
109
+ self.aasm_current_state = state if save_success
110
+
111
+ save_success
112
+ end
113
+
114
+ def aasm_current_state=(state)
115
+ if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
116
+ aasm_write_state_without_persistence(state)
117
+ end
118
+ @aasm_current_state = state
119
+ end
120
+
121
+ def aasm_determine_state_name(state)
122
+ case state
123
+ when Symbol, String
124
+ state
125
+ when Proc
126
+ state.call(self)
127
+ else
128
+ raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc."
129
+ end
130
+ end
131
+
132
+ def aasm_state_object_for_state(name)
133
+ obj = self.class.aasm_states.find {|s| s == name}
134
+ raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
135
+ obj
136
+ end
137
+
138
+ def aasm_fire_event(name, persist, *args)
139
+ old_state = aasm_state_object_for_state(aasm_current_state)
140
+ event = self.class.aasm_events[name]
141
+
142
+ old_state.call_action(:exit, self)
143
+
144
+ # new event before callback
145
+ event.call_action(:before, self)
146
+
147
+ new_state_name = event.fire(self, *args)
148
+
149
+ unless new_state_name.nil?
150
+ new_state = aasm_state_object_for_state(new_state_name)
151
+
152
+ # new before_ callbacks
153
+ old_state.call_action(:before_exit, self)
154
+ new_state.call_action(:before_enter, self)
155
+
156
+ new_state.call_action(:enter, self)
157
+
158
+ persist_successful = true
159
+ if persist
160
+ persist_successful = set_aasm_current_state_with_persistence(new_state_name)
161
+ event.execute_success_callback(self) if persist_successful
162
+ else
163
+ self.aasm_current_state = new_state_name
164
+ end
165
+
166
+ if persist_successful
167
+ old_state.call_action(:after_exit, self)
168
+ new_state.call_action(:after_enter, self)
169
+ event.call_action(:after, self)
170
+
171
+ self.aasm_event_fired(name, old_state.name, self.aasm_current_state) if self.respond_to?(:aasm_event_fired)
172
+ else
173
+ self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
174
+ end
175
+
176
+ persist_successful
177
+ else
178
+ if self.respond_to?(:aasm_event_failed)
179
+ self.aasm_event_failed(name, old_state.name)
180
+ end
181
+
182
+ false
183
+ end
184
+ end
185
+ end
File without changes
File without changes
File without changes
File without changes
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.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -31,15 +31,17 @@ files:
31
31
  - README.rdoc
32
32
  - TODO
33
33
  - lib/aasm.rb
34
- - lib/event.rb
35
- - lib/persistence/active_record_persistence.rb
36
- - lib/persistence.rb
37
- - lib/state.rb
38
- - lib/state_machine.rb
39
- - lib/state_transition.rb
34
+ - lib/aasm/aasm.rb
35
+ - lib/aasm/event.rb
36
+ - lib/aasm/persistence/active_record_persistence.rb
37
+ - lib/aasm/persistence.rb
38
+ - lib/aasm/state.rb
39
+ - lib/aasm/state_machine.rb
40
+ - lib/aasm/state_transition.rb
40
41
  - doc/jamis.rb
41
42
  has_rdoc: true
42
43
  homepage: http://github.com/rubyist/aasm
44
+ licenses:
43
45
  post_install_message:
44
46
  rdoc_options:
45
47
  - --line-numbers
@@ -65,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
67
  requirements: []
66
68
 
67
69
  rubyforge_project:
68
- rubygems_version: 1.2.0
70
+ rubygems_version: 1.3.5
69
71
  signing_key:
70
72
  specification_version: 2
71
73
  summary: State machine mixin for Ruby objects