-
Notifications
You must be signed in to change notification settings - Fork 629
/
event.rb
121 lines (103 loc) · 3.5 KB
/
event.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module AASM
module SupportingClasses
class Event
attr_reader :name, :options
def initialize(name, options = {}, &block)
@name = name
@transitions = []
update(options, &block)
end
# a neutered version of fire - it doesn't actually fire the event, it just
# executes the transition guards to determine if a transition is even
# an option given current conditions.
def may_fire?(obj, to_state=nil, *args)
_fire(obj, true, to_state, *args) # true indicates test firing
end
def fire(obj, to_state=nil, *args)
_fire(obj, false, to_state, *args) # false indicates this is not a test (fire!)
end
def transitions_from_state?(state)
transitions_from_state(state).any?
end
def transitions_from_state(state)
@transitions.select { |t| t.from == state }
end
def transitions_to_state?(state)
transitions_to_state(state).any?
end
def transitions_to_state(state)
@transitions.select { |t| t.to == state }
end
def all_transitions
@transitions
end
def fire_callbacks(action, record, *args)
invoke_callbacks(@options[action], record, args)
end
def ==(event)
if event.is_a? Symbol
name == event
else
name == event.name
end
end
private
def update(options = {}, &block)
@options = options
if block then
instance_eval(&block)
end
self
end
# Execute if test? == false, otherwise return true/false depending on whether it would fire
def _fire(obj, test, to_state=nil, *args)
if @transitions.map(&:from).any?
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
return nil if transitions.size == 0
else
transitions = @transitions
end
result = test ? false : nil
transitions.each do |transition|
next if to_state and !Array(transition.to).include?(to_state)
if transition.perform(obj, *args)
if test
result = true
else
result = to_state || Array(transition.to).first
transition.execute(obj, *args)
end
break
end
end
result
end
def invoke_callbacks(code, record, args)
case code
when Symbol, String
record.send(code, *args)
when Proc
record.instance_exec(*args, &code)
when Array
code.each {|a| invoke_callbacks(a, record, args)}
end
end
## DSL interface
def transitions(trans_opts)
# Create a separate transition for each from state to the given state
Array(trans_opts[:from]).each do |s|
@transitions << AASM::SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
end
# Create a transition if to is specified without from (transitions from ANY state)
@transitions << AASM::SupportingClasses::StateTransition.new(trans_opts) if @transitions.empty? && trans_opts[:to]
end
[:after, :before, :error, :success].each do |callback_name|
define_method callback_name do |*args, &block|
options[callback_name] = Array(options[callback_name])
options[callback_name] << block if block
options[callback_name] += Array(args)
end
end
end
end # SupportingClasses
end # AASM