ruby_valve 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/LICENSE.txt +22 -0
- data/README.md +361 -0
- data/Rakefile +1 -0
- data/lib/ruby_valve/base.rb +130 -0
- data/lib/ruby_valve/errors.rb +4 -0
- data/lib/ruby_valve/version.rb +3 -0
- data/lib/ruby_valve.rb +4 -0
- data/ruby_valve.gemspec +26 -0
- data/spec/lib/base_spec.rb +298 -0
- data/spec/lib/ruby_valve/base_spec.rb +43 -0
- data/spec/spec_helper.rb +11 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7b2f4de1638c09da9c95d2c18ffe1f84e0c0ff5
|
4
|
+
data.tar.gz: ec03254b2ccb35abc87a5f147a0583f3ad7cdce6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a21595eeb9fd52ff4ca46017adcbf5c018c52188f34a9dbea04b256e20785e8693255625c620d1dee55cf59a047ba54402f669266195a788619b28eb420f712
|
7
|
+
data.tar.gz: 2ac42dbffe38ae921ef4e79d58d278f36ff6c0c2bf797b6d82582771a56bea55f96f94e314c7582e88801aeb1c1e2abe3748e01fa8fb0cf35441d02affb3fd5a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Capybara features specs
|
10
|
+
# watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
11
|
+
|
12
|
+
# Turnip features and steps
|
13
|
+
# watch(%r{^spec/acceptance/(.+)\.feature$})
|
14
|
+
# watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
15
|
+
end
|
16
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Juan Leal
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,361 @@
|
|
1
|
+
# RubyValve
|
2
|
+
|
3
|
+
This gem provide a mechanism for doing easy flow type code pattern. Similar to what's done in
|
4
|
+
the template design pattern.
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ruby_valve'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ruby_valve
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
To use RubyValve you need to subclass the base class:
|
25
|
+
|
26
|
+
require 'ruby_valve'
|
27
|
+
|
28
|
+
class Foo < RubyValve::Base
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
####\#step_n methods
|
33
|
+
Next you define a number steps using the naming convention of #step_n for the method name.
|
34
|
+
|
35
|
+
class Foo < RubyValve::Base
|
36
|
+
|
37
|
+
def step_1
|
38
|
+
puts "A"
|
39
|
+
end
|
40
|
+
|
41
|
+
def step_2
|
42
|
+
puts "B"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
After defining #step_n methods you can execute then all by running the #execute method.
|
47
|
+
|
48
|
+
Foo.new.execute
|
49
|
+
|
50
|
+
A
|
51
|
+
B
|
52
|
+
|
53
|
+
####\#skip
|
54
|
+
You can skip a step by using the #skip method
|
55
|
+
|
56
|
+
def step_1
|
57
|
+
puts "A"
|
58
|
+
skip :step_2, :step_3
|
59
|
+
end
|
60
|
+
|
61
|
+
def step_2
|
62
|
+
puts "B"
|
63
|
+
end
|
64
|
+
|
65
|
+
def step_3
|
66
|
+
puts "C"
|
67
|
+
end
|
68
|
+
|
69
|
+
def step_4
|
70
|
+
puts "D"
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
Foo.new.execute
|
75
|
+
|
76
|
+
A
|
77
|
+
D
|
78
|
+
|
79
|
+
####\#abort
|
80
|
+
You can have it abort at a certain step and it will not execute the remainder of the steps.
|
81
|
+
|
82
|
+
def step_1
|
83
|
+
puts "A"
|
84
|
+
end
|
85
|
+
|
86
|
+
def step_2
|
87
|
+
abort "Ug, again?"
|
88
|
+
end
|
89
|
+
|
90
|
+
def step_3
|
91
|
+
puts "C"
|
92
|
+
end
|
93
|
+
|
94
|
+
def step_4
|
95
|
+
puts "D"
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
Foo.new.execute
|
100
|
+
|
101
|
+
A
|
102
|
+
|
103
|
+
You can also have #abort raise an error as well.
|
104
|
+
|
105
|
+
def step_1
|
106
|
+
puts "A"
|
107
|
+
end
|
108
|
+
|
109
|
+
def step_2
|
110
|
+
abort "Ug, again?", raise: true
|
111
|
+
end
|
112
|
+
|
113
|
+
def step_3
|
114
|
+
puts "C"
|
115
|
+
end
|
116
|
+
|
117
|
+
def step_4
|
118
|
+
puts "D"
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
Foo.new.execute
|
123
|
+
|
124
|
+
A
|
125
|
+
RubyValve::AbortError:
|
126
|
+
Ug
|
127
|
+
|
128
|
+
####\#step_n_result
|
129
|
+
The result of each #step_n method can be accessed by calling #step_n_result. This allows the sharing of data between methods.
|
130
|
+
|
131
|
+
def step_1
|
132
|
+
"A"
|
133
|
+
end
|
134
|
+
|
135
|
+
def step_2
|
136
|
+
puts "step 1's result was: #{step_1_result}"
|
137
|
+
end
|
138
|
+
|
139
|
+
Foo.new.execute
|
140
|
+
|
141
|
+
step 1's result was: A
|
142
|
+
|
143
|
+
|
144
|
+
####\#response
|
145
|
+
The response for each step is recorded in a hash that can be accessed by this method
|
146
|
+
|
147
|
+
def step_1
|
148
|
+
"A"
|
149
|
+
end
|
150
|
+
|
151
|
+
def step_2
|
152
|
+
"B"
|
153
|
+
end
|
154
|
+
|
155
|
+
foo = Foo.new
|
156
|
+
foo.execute
|
157
|
+
foo.result
|
158
|
+
|
159
|
+
{:step_1_result=>"A", :step_2_result=>"B"}
|
160
|
+
|
161
|
+
|
162
|
+
###Callbacks
|
163
|
+
RubyValve provides a number of callbacks.
|
164
|
+
|
165
|
+
####\#before_all
|
166
|
+
|
167
|
+
Executes code once before all the steps.
|
168
|
+
|
169
|
+
def before_all
|
170
|
+
puts "..BA"
|
171
|
+
end
|
172
|
+
|
173
|
+
def step_1
|
174
|
+
"A"
|
175
|
+
end
|
176
|
+
|
177
|
+
def step_2
|
178
|
+
"B"
|
179
|
+
end
|
180
|
+
|
181
|
+
Foo.new.execute
|
182
|
+
|
183
|
+
..BA
|
184
|
+
A
|
185
|
+
B
|
186
|
+
|
187
|
+
####\#before_each
|
188
|
+
Executes code before each step method
|
189
|
+
|
190
|
+
def before_each
|
191
|
+
puts "..BE"
|
192
|
+
end
|
193
|
+
|
194
|
+
def step_1
|
195
|
+
"A"
|
196
|
+
end
|
197
|
+
|
198
|
+
def step_2
|
199
|
+
"B"
|
200
|
+
end
|
201
|
+
|
202
|
+
Foo.new.execute
|
203
|
+
|
204
|
+
..BE
|
205
|
+
A
|
206
|
+
..BE
|
207
|
+
B
|
208
|
+
|
209
|
+
####\#after_each
|
210
|
+
Executes code after each step method.
|
211
|
+
|
212
|
+
def after_each
|
213
|
+
puts "..AE"
|
214
|
+
end
|
215
|
+
|
216
|
+
def step_1
|
217
|
+
"A"
|
218
|
+
end
|
219
|
+
|
220
|
+
def step_2
|
221
|
+
"B"
|
222
|
+
end
|
223
|
+
|
224
|
+
Foo.new.execute
|
225
|
+
|
226
|
+
A
|
227
|
+
..AE
|
228
|
+
B
|
229
|
+
..AE
|
230
|
+
|
231
|
+
####\#after_success
|
232
|
+
Executes if no abort was triggered or exceptions raised.
|
233
|
+
|
234
|
+
def step_1
|
235
|
+
puts "E"
|
236
|
+
end
|
237
|
+
|
238
|
+
def after_success
|
239
|
+
puts "Yay!"
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
Foo.new.execute
|
244
|
+
|
245
|
+
E
|
246
|
+
Yay!
|
247
|
+
|
248
|
+
####\#after_abort
|
249
|
+
Executes if an abort, without a raise, was triggered.
|
250
|
+
|
251
|
+
def step_1
|
252
|
+
abort "call it off"
|
253
|
+
end
|
254
|
+
|
255
|
+
def step_2
|
256
|
+
puts "E"
|
257
|
+
end
|
258
|
+
|
259
|
+
def after_abort
|
260
|
+
puts "aborted!"
|
261
|
+
end
|
262
|
+
|
263
|
+
Foo.new.execute
|
264
|
+
|
265
|
+
aborted!
|
266
|
+
|
267
|
+
####\#after_raise and #exeception
|
268
|
+
Creating an after_raise method will trigger an automatic rescue when an error is raised. The exception is stored in the **exception** method.
|
269
|
+
|
270
|
+
def step_1
|
271
|
+
abort "call it off", raise: true
|
272
|
+
end
|
273
|
+
|
274
|
+
def step_2
|
275
|
+
puts "E"
|
276
|
+
end
|
277
|
+
|
278
|
+
def after_raise
|
279
|
+
puts exception.message
|
280
|
+
end
|
281
|
+
|
282
|
+
Foo.new.execute
|
283
|
+
|
284
|
+
call it off
|
285
|
+
|
286
|
+
###execution logs
|
287
|
+
There are a couple of methods that can be used to look at what was executed.
|
288
|
+
|
289
|
+
####\#executed_steps
|
290
|
+
|
291
|
+
This will display each #step_n method that was actually executed.
|
292
|
+
|
293
|
+
def step_1
|
294
|
+
"A"
|
295
|
+
end
|
296
|
+
|
297
|
+
def step_2
|
298
|
+
"B"
|
299
|
+
end
|
300
|
+
|
301
|
+
def after_each
|
302
|
+
puts "..AE"
|
303
|
+
end
|
304
|
+
|
305
|
+
foo = Foo.new
|
306
|
+
foo.execute
|
307
|
+
foo.executed_steps
|
308
|
+
|
309
|
+
[:step_1, :step_2]
|
310
|
+
|
311
|
+
####\#executed
|
312
|
+
This will display each step and callback method that was executed.
|
313
|
+
|
314
|
+
def step_1
|
315
|
+
"A"
|
316
|
+
end
|
317
|
+
|
318
|
+
def step_2
|
319
|
+
"B"
|
320
|
+
end
|
321
|
+
|
322
|
+
def after_each
|
323
|
+
puts "..AE"
|
324
|
+
end
|
325
|
+
|
326
|
+
foo = Foo.new
|
327
|
+
foo.execute
|
328
|
+
foo.executed_steps
|
329
|
+
|
330
|
+
[:step_1, :after_each, :step_2, :after_each]
|
331
|
+
|
332
|
+
##Suggestions
|
333
|
+
|
334
|
+
I would recommend encapsulating the logic of what is to be done into methods with names that clearly state the intention of the code. The calling these methods within the steps.
|
335
|
+
|
336
|
+
####Example
|
337
|
+
|
338
|
+
def step_1
|
339
|
+
post_paypal_transaction
|
340
|
+
end
|
341
|
+
|
342
|
+
def step_2
|
343
|
+
store_paypal_result(step_1_result)
|
344
|
+
end
|
345
|
+
|
346
|
+
def step_3
|
347
|
+
update_transaction_records
|
348
|
+
end
|
349
|
+
|
350
|
+
#=> ACTIONS
|
351
|
+
def post_paypal_transaction
|
352
|
+
#code
|
353
|
+
end
|
354
|
+
|
355
|
+
def store_paypal_result(paypal_results)
|
356
|
+
#code
|
357
|
+
end
|
358
|
+
|
359
|
+
def post_paypal_transaction
|
360
|
+
#code
|
361
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'ruby_valve/errors'
|
2
|
+
|
3
|
+
module RubyValve
|
4
|
+
class Base
|
5
|
+
attr_reader :executed_steps, :executed, :exception
|
6
|
+
|
7
|
+
def init
|
8
|
+
@skip_list = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
init
|
13
|
+
|
14
|
+
if respond_to?(:after_exception)
|
15
|
+
begin
|
16
|
+
execute_methods
|
17
|
+
rescue => e
|
18
|
+
@exception = e
|
19
|
+
send(:after_exception)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
execute_methods
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def response
|
28
|
+
@response
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def execute_methods
|
34
|
+
# begin
|
35
|
+
@response = {}
|
36
|
+
|
37
|
+
if respond_to?(:before_all)
|
38
|
+
send(:before_all)
|
39
|
+
log_execution(:before_all)
|
40
|
+
end
|
41
|
+
|
42
|
+
execution_order.each do |_method|
|
43
|
+
if !self.skip?(_method)
|
44
|
+
|
45
|
+
if respond_to?(:before_each)
|
46
|
+
send(:before_each)
|
47
|
+
log_execution(:before_each)
|
48
|
+
end
|
49
|
+
|
50
|
+
#create method to store step results
|
51
|
+
self.class.class_eval {attr_accessor :"#{_method}_result"}
|
52
|
+
result = send(_method)
|
53
|
+
|
54
|
+
#assign step result information
|
55
|
+
@response[:"#{_method}_result"] = result
|
56
|
+
send(:"#{_method}_result=", result)
|
57
|
+
|
58
|
+
#log step exec
|
59
|
+
log_step_execution(_method)
|
60
|
+
|
61
|
+
if respond_to?(:after_each)
|
62
|
+
send(:after_each)
|
63
|
+
log_execution(:after_each)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if respond_to?(:after_success) && !abort_triggered?
|
69
|
+
send(:after_success)
|
70
|
+
log_execution(:after_success)
|
71
|
+
end
|
72
|
+
|
73
|
+
if respond_to?(:after_abort) && abort_triggered?
|
74
|
+
send(:after_abort)
|
75
|
+
log_execution(:after_abort)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
#=> logging methods
|
80
|
+
def log_step_execution(_method)
|
81
|
+
(@executed_steps ||= []) << _method
|
82
|
+
log_execution(_method)
|
83
|
+
end
|
84
|
+
|
85
|
+
def log_execution(_method)
|
86
|
+
(@executed ||= []) << _method
|
87
|
+
end
|
88
|
+
|
89
|
+
def abort(message, options = {})
|
90
|
+
@abort_triggered = true
|
91
|
+
@abort_message = message
|
92
|
+
raise(AbortError, @abort_message) if options[:raise]
|
93
|
+
end
|
94
|
+
|
95
|
+
#=> skip methods
|
96
|
+
def skip_all_steps?
|
97
|
+
abort_triggered?
|
98
|
+
end
|
99
|
+
|
100
|
+
def skip(*step_names)
|
101
|
+
skip_list.push *step_names
|
102
|
+
end
|
103
|
+
|
104
|
+
def skip_list
|
105
|
+
@skip_list
|
106
|
+
end
|
107
|
+
|
108
|
+
def abort_triggered?
|
109
|
+
@abort_triggered
|
110
|
+
end
|
111
|
+
|
112
|
+
def skip?(method_name)
|
113
|
+
return true if skip_all_steps?
|
114
|
+
|
115
|
+
skip_list.include?(method_name)
|
116
|
+
end
|
117
|
+
|
118
|
+
def execution_order
|
119
|
+
step_methods = methods.select {|meth| meth.to_s.match(/^step_[0-9]*$/)}
|
120
|
+
|
121
|
+
step_methods.sort do |x,y|
|
122
|
+
ordinal_x = x.to_s.split("_").last.to_i
|
123
|
+
ordinal_y = y.to_s.split("_").last.to_i
|
124
|
+
|
125
|
+
ordinal_x <=> ordinal_y
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
end
|
data/lib/ruby_valve.rb
ADDED
data/ruby_valve.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruby_valve/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ruby_valve"
|
8
|
+
spec.version = RubyValve::VERSION
|
9
|
+
spec.authors = ["monochromicorn"]
|
10
|
+
spec.email = ["necrocommit@gmail.com"]
|
11
|
+
spec.description = %q{This gem provide a mechanism for doing easy flow type code pattern}
|
12
|
+
spec.summary = %q{Programming execution flow control}
|
13
|
+
spec.homepage = "http://github.com/monochromicorn/ruby_valve#step_n-methods"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "guard-rspec"
|
25
|
+
spec.add_development_dependency "pry-debugger"
|
26
|
+
end
|
@@ -0,0 +1,298 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
DummyValve = Class.new(RubyValve::Base) do
|
4
|
+
define_method(:step_1) {}
|
5
|
+
define_method(:step_2) {}
|
6
|
+
define_method(:step_3) {}
|
7
|
+
end
|
8
|
+
|
9
|
+
describe RubyValve::Base do
|
10
|
+
describe "#execute" do
|
11
|
+
before(:each) do
|
12
|
+
@valve = DummyValve.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should execute all step methods in order" do
|
16
|
+
@valve.execute
|
17
|
+
@valve.executed_steps.should eql([:step_1, :step_2, :step_3])
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not execute steps after an abort" do
|
21
|
+
def @valve.step_0
|
22
|
+
abort "Ug, I give up"
|
23
|
+
end
|
24
|
+
|
25
|
+
@valve.execute
|
26
|
+
@valve.executed_steps.should eql([:step_0])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#skip" do
|
31
|
+
before(:each) do
|
32
|
+
|
33
|
+
class SkipValve < RubyValve::Base
|
34
|
+
define_method(:step_1) {skip :step_2}
|
35
|
+
define_method(:step_2) {}
|
36
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
37
|
+
define_method(:step_4) {}
|
38
|
+
define_method(:step_5) {}
|
39
|
+
end
|
40
|
+
|
41
|
+
@skip_valve = SkipValve.new
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not execute skipped steps" do
|
45
|
+
@skip_valve.execute
|
46
|
+
@skip_valve.executed_steps.should eql([:step_1, :step_3])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#response" do
|
51
|
+
before(:each) do
|
52
|
+
|
53
|
+
class Res < RubyValve::Base
|
54
|
+
define_method(:step_1) {"apple"}
|
55
|
+
define_method(:step_2) {"banana"}
|
56
|
+
end
|
57
|
+
|
58
|
+
@res = Res.new
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should contain the method results" do
|
62
|
+
@res.execute
|
63
|
+
@res.response.should eql({:step_1_result=>"apple", :step_2_result=>"banana"})
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#step_x_result" do
|
68
|
+
before(:each) do
|
69
|
+
|
70
|
+
class Res1 < RubyValve::Base
|
71
|
+
define_method(:step_1) {"apple"}
|
72
|
+
define_method(:step_2) {"banana"}
|
73
|
+
end
|
74
|
+
|
75
|
+
@res1 = Res1.new
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should equal the result of #step_x" do
|
79
|
+
@res1.execute
|
80
|
+
@res1.step_1_result.should eql "apple"
|
81
|
+
@res1.step_2_result.should eql "banana"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#abort" do
|
86
|
+
before(:each) do
|
87
|
+
|
88
|
+
class AfterAbort < RubyValve::Base
|
89
|
+
define_method(:after_abort) {}
|
90
|
+
define_method(:step_1) {skip :step_2}
|
91
|
+
define_method(:step_2) {}
|
92
|
+
define_method(:step_3) {abort "Ug", :raise => true}
|
93
|
+
define_method(:step_4) {}
|
94
|
+
define_method(:step_5) {}
|
95
|
+
end
|
96
|
+
|
97
|
+
@after_abort = AfterAbort.new
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should raise AbortError when :raise => true" do
|
101
|
+
expect {@after_abort.execute}.to raise_error(RubyValve::AbortError, "Ug")
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
#==> BEFORE HOOKS
|
106
|
+
describe "#before_each" do
|
107
|
+
before(:each) do
|
108
|
+
|
109
|
+
class BeforeEach < RubyValve::Base
|
110
|
+
define_method(:before_each) {}
|
111
|
+
define_method(:step_1) {skip :step_2}
|
112
|
+
define_method(:step_2) {}
|
113
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
114
|
+
define_method(:step_4) {}
|
115
|
+
define_method(:step_5) {}
|
116
|
+
end
|
117
|
+
|
118
|
+
@before_each = BeforeEach.new
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should execute when defined" do
|
122
|
+
@before_each.should_receive(:before_each).exactly(2)
|
123
|
+
@before_each.execute
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should execute before each step_x" do
|
127
|
+
@before_each.execute
|
128
|
+
@before_each.executed.should eql([:before_each, :step_1, :before_each, :step_3])
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should not show up in executed_steps" do
|
132
|
+
@before_each.execute
|
133
|
+
@before_each.executed_steps.should eql([:step_1, :step_3])
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "#before_all" do
|
139
|
+
before(:each) do
|
140
|
+
|
141
|
+
class BeforeAll < RubyValve::Base
|
142
|
+
define_method(:before_all) {}
|
143
|
+
define_method(:step_1) {skip :step_2}
|
144
|
+
define_method(:step_2) {}
|
145
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
146
|
+
define_method(:step_4) {}
|
147
|
+
define_method(:step_5) {}
|
148
|
+
end
|
149
|
+
|
150
|
+
@before_all = BeforeAll.new
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should execute when defined" do
|
154
|
+
@before_all.should_receive(:before_all).exactly(1)
|
155
|
+
@before_all.execute
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should execute before all steps" do
|
159
|
+
@before_all.execute
|
160
|
+
@before_all.executed.should eql([:before_all, :step_1, :step_3])
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should not show up in executed_steps" do
|
164
|
+
@before_all.execute
|
165
|
+
@before_all.executed_steps.should eql([:step_1, :step_3])
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
#==> AFTER HOOKS
|
171
|
+
describe "#after_each" do
|
172
|
+
before(:each) do
|
173
|
+
|
174
|
+
class AfterEach < RubyValve::Base
|
175
|
+
define_method(:after_each) {}
|
176
|
+
define_method(:step_1) {skip :step_2}
|
177
|
+
define_method(:step_2) {}
|
178
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
179
|
+
define_method(:step_4) {}
|
180
|
+
define_method(:step_5) {}
|
181
|
+
end
|
182
|
+
|
183
|
+
@after_each = AfterEach.new
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should execute when defined" do
|
187
|
+
@after_each.should_receive(:after_each).exactly(2)
|
188
|
+
@after_each.execute
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should execute after each step_x" do
|
192
|
+
@after_each.execute
|
193
|
+
@after_each.executed.should eql([:step_1, :after_each, :step_3, :after_each])
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should not show up in executed_steps" do
|
197
|
+
@after_each.execute
|
198
|
+
@after_each.executed_steps.should eql([:step_1, :step_3])
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
describe "#after_success" do
|
204
|
+
before(:each) do
|
205
|
+
|
206
|
+
class AfterSuccess < RubyValve::Base
|
207
|
+
define_method(:after_success) {}
|
208
|
+
define_method(:step_1) {skip :step_2}
|
209
|
+
define_method(:step_2) {}
|
210
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
211
|
+
define_method(:step_4) {}
|
212
|
+
define_method(:step_5) {}
|
213
|
+
end
|
214
|
+
|
215
|
+
@after_success = AfterSuccess.new
|
216
|
+
end
|
217
|
+
|
218
|
+
context "when no abort is triggered" do
|
219
|
+
|
220
|
+
it "should execute when defined" do
|
221
|
+
@after_success.should_receive(:after_success).exactly(1)
|
222
|
+
@after_success.execute
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should execute after all steps" do
|
226
|
+
@after_success.execute
|
227
|
+
@after_success.executed.should eql([:step_1, :step_3, :after_success])
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should not execute after an abort" do
|
231
|
+
def @after_success.step_1
|
232
|
+
abort "Ug"
|
233
|
+
end
|
234
|
+
|
235
|
+
@after_success.execute
|
236
|
+
@after_success.executed.should eql([:step_1])
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should not show up in executed_steps" do
|
240
|
+
@after_success.execute
|
241
|
+
@after_success.executed_steps.should eql([:step_1, :step_3])
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
context "when an abort is triggered" do
|
247
|
+
it "should not execute when defined" do
|
248
|
+
def @after_success.step_1
|
249
|
+
abort "Ug"
|
250
|
+
end
|
251
|
+
|
252
|
+
@after_success.should_receive(:after_success).exactly(0)
|
253
|
+
@after_success.execute
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
describe "#after_abort" do
|
259
|
+
context "after an abort is triggered" do
|
260
|
+
before(:each) do
|
261
|
+
|
262
|
+
class AfterAbort < RubyValve::Base
|
263
|
+
define_method(:after_abort) {}
|
264
|
+
define_method(:step_1) {skip :step_2}
|
265
|
+
define_method(:step_2) {}
|
266
|
+
define_method(:step_3) {abort "Ug"}
|
267
|
+
define_method(:step_4) {}
|
268
|
+
define_method(:step_5) {}
|
269
|
+
end
|
270
|
+
|
271
|
+
@after_abort = AfterAbort.new
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should execute when defined" do
|
275
|
+
@after_abort.should_receive(:after_abort).exactly(1)
|
276
|
+
@after_abort.execute
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should execute after all the steps" do
|
280
|
+
@after_abort.execute
|
281
|
+
@after_abort.executed.should eql([:step_1, :step_3, :after_abort])
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should not execute steps after an abort" do
|
285
|
+
@after_abort.execute
|
286
|
+
@after_abort.executed_steps.should eql([:step_1, :step_3])
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should not show up in executed_steps" do
|
290
|
+
@after_abort.execute
|
291
|
+
@after_abort.executed_steps.should eql([:step_1, :step_3])
|
292
|
+
end
|
293
|
+
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
|
298
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
DummyValve = Class.new(RubyValve::Base) do
|
4
|
+
define_method(:step_1) {}
|
5
|
+
define_method(:step_2) {}
|
6
|
+
define_method(:step_3) {}
|
7
|
+
end
|
8
|
+
|
9
|
+
describe RubyValve::Base do
|
10
|
+
before(:each) do
|
11
|
+
@valve = DummyValve.new
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should execute all step methods in order" do
|
15
|
+
@valve.execute
|
16
|
+
@valve.executed_steps.should eql([:step_1, :step_2, :step_3])
|
17
|
+
end
|
18
|
+
|
19
|
+
context "#skip should make it skip a step" do
|
20
|
+
before(:each) do
|
21
|
+
|
22
|
+
class SkipValve < RubyValve::Base
|
23
|
+
define_method(:step_1) {skip :step_2}
|
24
|
+
define_method(:step_2) {}
|
25
|
+
define_method(:step_3) {skip :step_4, :step_5}
|
26
|
+
define_method(:step_4) {}
|
27
|
+
define_method(:step_5) {}
|
28
|
+
end
|
29
|
+
|
30
|
+
@skip_valve = SkipValve.new
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should not execute skipped steps" do
|
34
|
+
@skip_valve.execute
|
35
|
+
@skip_valve.executed_steps.should eql([])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
# context ".executed_steps" do
|
39
|
+
# it "should re"
|
40
|
+
# end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_valve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- monochromicorn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-debugger
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: This gem provide a mechanism for doing easy flow type code pattern
|
84
|
+
email:
|
85
|
+
- necrocommit@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- Guardfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/ruby_valve.rb
|
98
|
+
- lib/ruby_valve/base.rb
|
99
|
+
- lib/ruby_valve/errors.rb
|
100
|
+
- lib/ruby_valve/version.rb
|
101
|
+
- ruby_valve.gemspec
|
102
|
+
- spec/lib/base_spec.rb
|
103
|
+
- spec/lib/ruby_valve/base_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: http://github.com/monochromicorn/ruby_valve#step_n-methods
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.1.11
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Programming execution flow control
|
129
|
+
test_files:
|
130
|
+
- spec/lib/base_spec.rb
|
131
|
+
- spec/lib/ruby_valve/base_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
has_rdoc:
|