gvaughn-aasm 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +33 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +118 -0
- data/Rakefile +94 -0
- data/TODO +9 -0
- data/doc/jamis.rb +591 -0
- data/lib/aasm.rb +150 -0
- data/lib/event.rb +45 -0
- data/lib/persistence.rb +16 -0
- data/lib/persistence/active_record_persistence.rb +238 -0
- data/lib/state.rb +33 -0
- data/lib/state_machine.rb +29 -0
- data/lib/state_transition.rb +36 -0
- data/lib/version.rb +5 -0
- metadata +1 -1
data/lib/state.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module AASM
|
2
|
+
module SupportingClasses
|
3
|
+
class State
|
4
|
+
attr_reader :name, :options
|
5
|
+
|
6
|
+
def initialize(name, options={})
|
7
|
+
@name, @options = name, options
|
8
|
+
end
|
9
|
+
|
10
|
+
def ==(state)
|
11
|
+
if state.is_a? Symbol
|
12
|
+
name == state
|
13
|
+
else
|
14
|
+
name == state.name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def call_action(action, record)
|
19
|
+
action = @options[action]
|
20
|
+
case action
|
21
|
+
when Symbol, String
|
22
|
+
record.send(action)
|
23
|
+
when Proc
|
24
|
+
action.call(record)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def for_select
|
29
|
+
[name.to_s.gsub(/_/, ' ').capitalize, name.to_s]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module AASM
|
4
|
+
class StateMachine
|
5
|
+
def self.[](*args)
|
6
|
+
(@machines ||= {})[args]
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.[]=(*args)
|
10
|
+
val = args.pop
|
11
|
+
(@machines ||= {})[args] = val
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_accessor :states, :events, :initial_state, :config
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
def initialize(name)
|
18
|
+
@name = name
|
19
|
+
@initial_state = nil
|
20
|
+
@states = []
|
21
|
+
@events = {}
|
22
|
+
@config = OpenStruct.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_state(name, options)
|
26
|
+
@states << AASM::SupportingClasses::State.new(name, options) unless @states.include?(name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module AASM
|
2
|
+
module SupportingClasses
|
3
|
+
class StateTransition
|
4
|
+
attr_reader :from, :to, :opts
|
5
|
+
|
6
|
+
def initialize(opts)
|
7
|
+
@from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
|
8
|
+
@opts = opts
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform(obj)
|
12
|
+
case @guard
|
13
|
+
when Symbol, String
|
14
|
+
obj.send(@guard)
|
15
|
+
when Proc
|
16
|
+
@guard.call(obj)
|
17
|
+
else
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def execute(obj, *args)
|
23
|
+
case @on_transition
|
24
|
+
when Symbol, String
|
25
|
+
obj.send(@on_transition, *args)
|
26
|
+
when Proc
|
27
|
+
@on_transition.call(obj, *args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def ==(obj)
|
32
|
+
@from == obj.from && @to == obj.to
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/version.rb
ADDED