-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
dsl.rb
234 lines (211 loc) · 5.49 KB
/
dsl.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# frozen_string_literal: true
require_relative "choice_merger"
require_relative "safety"
require_relative "transition_builder"
module FiniteMachine
# A generic DSL for describing the state machine
class GenericDSL
# Initialize a generic DSL
#
# @api public
def initialize(machine, attrs)
@machine = machine
@attrs = attrs
end
# Expose any state constant
# @api public
def any_state
ANY_STATE
end
# Expose any event constant
# @api public
def any_event
ANY_EVENT
end
# Delegate attributes to machine instance
#
# @api private
def method_missing(method_name, *args, &block)
if @machine.respond_to?(method_name)
@machine.send(method_name, *args, &block)
else
super
end
end
# Check if message can be handled by this DSL
#
# @api private
def respond_to_missing?(method_name, include_private = false)
@machine.respond_to?(method_name) || super
end
# Configure state machine properties
#
# @api private
def call(&block)
instance_eval(&block)
end
end # GenericDSL
# A class responsible for adding state machine specific dsl
class DSL < GenericDSL
include Safety
# Initialize top level DSL
#
# @api public
def initialize(machine, attrs)
super(machine, attrs)
@machine.state = FiniteMachine::DEFAULT_STATE
@defer_initial = true
@silent_initial = true
initial(@attrs[:initial]) if @attrs[:initial]
terminal(@attrs[:terminal]) if @attrs[:terminal]
log_transitions(@attrs.fetch(:log_transitions, false))
end
# Add aliases for the target object
#
# @example
# FiniteMachine.define do
# target_alias :engine
#
# on_transition do |event|
# engine.state = event.to
# end
# end
#
# @param [Array<Symbol>] aliases
# the names for target alias
#
# @api public
def alias_target(*aliases)
aliases.each do |alias_name|
next if env.aliases.include?(alias_name)
env.aliases << alias_name
end
end
# Define initial state
#
# @param [Symbol] value
# The initial state name.
# @param [Hash[Symbol]] options
# @option options [Symbol] :event
# The event name.
# @option options [Symbol] :defer
# Set to true to defer initial state transition.
# Default false.
# @option options [Symbol] :silent
# Set to true to disable callbacks.
# Default true.
#
# @example
# initial :green
#
# @example Defer initial event
# initial state: green, defer: true
#
# @example Trigger callbacks
# initial :green, silent: false
#
# @example Redefine event name
# initial :green, event: :start
#
# @param [String, Hash] value
#
# @return [StateMachine]
#
# @api public
def initial(value, options = {})
state = (value && !value.is_a?(Hash)) ? value : raise_missing_state
name, @defer_initial, @silent_initial = *parse_initial(options)
@initial_event = name
event(name, FiniteMachine::DEFAULT_STATE => state, silent: @silent_initial)
end
# Trigger initial event
#
# @return [nil]
#
# @api private
def trigger_init
method = @silent_initial ? :transition : :trigger
@machine.public_send(method, :"#{@initial_event}") unless @defer_initial
end
# Define terminal state
#
# @example
# terminal :red
#
# @return [FiniteMachine::StateMachine]
#
# @api public
def terminal(*values)
self.terminal_states = values
end
# Create event and associate transition
#
# @example
# event :go, :green => :yellow
# event :go, :green => :yellow, if: :lights_on?
#
# @param [Symbol] name
# the event name
# @param [Hash] transitions
# the event transitions and conditions
#
# @return [Transition]
#
# @api public
def event(name, transitions = {}, &block)
detect_event_conflict!(name) if machine.auto_methods?
if block_given?
merger = ChoiceMerger.new(machine, name, transitions)
merger.instance_eval(&block)
else
transition_builder = TransitionBuilder.new(machine, name, transitions)
transition_builder.call(transitions)
end
end
# Add error handler
#
# @param [Array] exceptions
#
# @example
# handle InvalidStateError, with: :log_errors
#
# @return [Array[Exception]]
#
# @api public
def handle(*exceptions, &block)
@machine.handle(*exceptions, &block)
end
# Decide whether to log transitions
#
# @api public
def log_transitions(value)
self.log_transitions = value
end
private
# Parse initial options
#
# @param [Hash] options
# the options to extract for initial state setup
#
# @return [Array[Symbol,String]]
#
# @api private
def parse_initial(options)
[options.fetch(:event) { FiniteMachine::DEFAULT_EVENT_NAME },
options.fetch(:defer) { false },
options.fetch(:silent) { true }]
end
# Raises missing state error
#
# @raise [MissingInitialStateError]
# Raised when state name is not provided for initial.
#
# @return [nil]
#
# @api private
def raise_missing_state
raise MissingInitialStateError,
"Provide state to transition :to for the initial event"
end
end # DSL
end # FiniteMachine