-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.rb
236 lines (208 loc) · 5.16 KB
/
processor.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
235
236
# encoding: utf-8
module Substation
# Namespace for chain processors
module Processor
include Concord.new(:name, :handler, :config)
# FIXME: include this once the bug with deep freezing
# is resolved (currently this deep freezes and thus
# tries to incorrectly freeze observers too.
#
# include Adamantium::Flat
# Initialize a new instance
#
# @param [Symbol] name
# the processor's name
#
# @param [Config] config
# the processor's configuration
#
# @return [undefined]
#
# @api private
def initialize(_name, _handler, _config)
super
@executor = config.executor
@observers = config.observers
@failure_chain = config.failure_chain
end
# This processor's name
#
# @return [Symbol]
#
# @api private
public :name
# Return failure chain
#
# @return [Chain]
#
# @api private
attr_reader :failure_chain
private :failure_chain
# Return executor
#
# @return [Executor]
#
# @api private
attr_reader :executor
private :executor
# Return the observers
#
# @return [Enumerable<#call>]
#
# @api private
attr_reader :observers
private :observers
# Test wether chain processing should continue
#
# @param [Response] response
# the response returned from invoking the processor
#
# @return [true] for a successful response
# @return [false] otherwise
#
# @api private
def success?(response)
response.success?
end
# The response passed on to the next processor in a {Chain}
#
# @param [Response] response
# the response returned from invoking the processor
#
# @return [Response]
# the response passed on to the next processor in a {Chain}
#
# @api private
def result(response)
response
end
private
# Execute processor on state
#
# @param [Object] state
# the state to execute with
#
# @return [Object]
#
# @api private
def execute(state)
compose(state, invoke(decompose(state)))
end
# Transform response data into something else
#
# @param [Response] response
# the response to process
#
# @return [Response]
#
# @api private
def invoke(state)
notify_observers(handler.call(state))
end
# Decompose +input+ before processing
#
# @param [Request, Response] input
# the object to decompose
#
# @return [Object]
# the decomposed object
#
# @api private
def decompose(input)
executor.decompose(input)
end
# Compose a new object based on +input+ and +output+
#
# @param [Request, Response] input
# the input as it was before calling {#decompose}
#
# @param [Object] output
# the data used to compose a new object
#
# @return [Object]
# the composed object
#
# @api private
def compose(input, output)
executor.compose(input, output)
end
# Notify all observers
#
# @param [Response] response
# the response returned from {handler#call}
#
# @return [Response]
#
# @api private
def notify_observers(response)
observers.each { |observer| observer.call(response) }
response
end
# Supports {Processor} instances with a defined failure {Chain}
module Fallible
# Return a new processor with +chain+ as failure_chain
#
# @param [#call] chain
# the failure chain to use for the new processor
#
# @return [#call]
#
# @api private
def with_failure_chain(chain)
self.class.new(name, handler, config.with_failure_chain(chain))
end
end
# Supports incoming {Processor} instances
module Incoming
include Processor
include Fallible
# The request passed on to the next processor in a {Chain}
#
# @param [Response] _response
# the response returned from invoking this processor
#
# @return [Request]
# the request passed on to the next processor in a {Chain}
#
# @api private
def result(_response)
super.to_request
end
end
# Supports pivot {Processor} instances
module Pivot
include Processor
include Fallible
end
# Supports outgoing {Processor} instances
module Outgoing
include Processor
# Test wether chain processing should continue
#
# @param [Response] _response
# the response returned from invoking the processor
#
# @return [true]
#
# @api private
def success?(_response)
true
end
private
# Build a new {Response} based on +response+ and +output+
#
# @param [Response] response
# the original response
#
# @param [Object] output
# the data to be wrapped within the new {Response}
#
# @return [Response]
#
# @api private
def respond_with(response, output)
response.class.new(response.request, output)
end
end # module Outgoing
end # module Processor
end # module Substation