rubyist-aasm 2.0.6 → 2.0.7

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)
data/lib/aasm.rb CHANGED
@@ -1,158 +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.0.6'
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
- 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_state_object_for_state(name)
122
- obj = self.class.aasm_states.find {|s| s == name}
123
- raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
124
- obj
125
- end
126
-
127
- def aasm_fire_event(name, persist, *args)
128
- aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
129
-
130
- new_state = self.class.aasm_events[name].fire(self, *args)
131
-
132
- unless new_state.nil?
133
- aasm_state_object_for_state(new_state).call_action(:enter, self)
134
-
135
- persist_successful = true
136
- if persist
137
- persist_successful = set_aasm_current_state_with_persistence(new_state)
138
- self.class.aasm_events[name].execute_success_callback(self) if persist_successful
139
- else
140
- self.aasm_current_state = new_state
141
- end
142
-
143
- if persist_successful
144
- self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
145
- else
146
- self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
147
- end
148
-
149
- persist_successful
150
- else
151
- if self.respond_to?(:aasm_event_failed)
152
- self.aasm_event_failed(name)
153
- end
154
-
155
- false
156
- end
157
- end
158
- end
1
+ require File.join(File.dirname(__FILE__), 'aasm', 'aasm')
data/lib/aasm/aasm.rb ADDED
@@ -0,0 +1,158 @@
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.0.7'
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
+ 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_state_object_for_state(name)
122
+ obj = self.class.aasm_states.find {|s| s == name}
123
+ raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
124
+ obj
125
+ end
126
+
127
+ def aasm_fire_event(name, persist, *args)
128
+ aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
129
+
130
+ new_state = self.class.aasm_events[name].fire(self, *args)
131
+
132
+ unless new_state.nil?
133
+ aasm_state_object_for_state(new_state).call_action(:enter, self)
134
+
135
+ persist_successful = true
136
+ if persist
137
+ persist_successful = set_aasm_current_state_with_persistence(new_state)
138
+ self.class.aasm_events[name].execute_success_callback(self) if persist_successful
139
+ else
140
+ self.aasm_current_state = new_state
141
+ end
142
+
143
+ if persist_successful
144
+ self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
145
+ else
146
+ self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
147
+ end
148
+
149
+ persist_successful
150
+ else
151
+ if self.respond_to?(:aasm_event_failed)
152
+ self.aasm_event_failed(name)
153
+ end
154
+
155
+ false
156
+ end
157
+ end
158
+ 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: rubyist-aasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Barron
@@ -31,12 +31,13 @@ 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