hopscotch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +330 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hopscotch.gemspec +33 -0
- data/lib/hopscotch.rb +32 -0
- data/lib/hopscotch/error.rb +39 -0
- data/lib/hopscotch/runners/default.rb +53 -0
- data/lib/hopscotch/step.rb +13 -0
- data/lib/hopscotch/step_composers/default.rb +46 -0
- data/lib/hopscotch/version.rb +3 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11e34e73cb8b90c514f46582abec823f62c9e117
|
4
|
+
data.tar.gz: 553d488f944437ba0372bd26a252a7500490df60
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ed0160c9985a117ba55c9ebaf2ec4e50f41482705c7cf78649a931e3dcc8aec3073b75c3f99b723055a523e0a48235e3d168a22464cb296bc0b855565ce7ddd
|
7
|
+
data.tar.gz: 98f4c2dd2437e25b59acd31b380aff30784a4de0b8939c0d13145e9386ee7e719cc637a609e7379cbffa39a44ca1191e835c1d2e3c9510396671089369a413c6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Garrett Heinlen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,330 @@
|
|
1
|
+
# Hopscotch
|
2
|
+
|
3
|
+
Hopscotch allows us to chain together complex logic and ensure if any specific part of the chain fails, everything is rolled back to its original state.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hopscotch'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hopscotch
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The Hopscotch gem is made up out of 2 essential parts. Runners and Steps.
|
24
|
+
|
25
|
+
Some simple usage examples. Detailed explanations below.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# - simple lambdas steps
|
29
|
+
# - compose steps into 1 function
|
30
|
+
# - runner call function with success/failure callbacks
|
31
|
+
success_step = -> { Hopscotch::Step.success! }
|
32
|
+
fail_step = -> { Hopscotch::Step.failure!("bad") }
|
33
|
+
|
34
|
+
reduced_fn = Hopscotch::StepComposer.compose_with_error_handling(success_step, success_step, success_step)
|
35
|
+
Hopscotch::Runner.call(reduced_fn, success: -> { "success" }, failure: -> (x) { "failure: #{x}" })
|
36
|
+
# => "success"
|
37
|
+
|
38
|
+
error_reduced_fn = Hopscotch::StepComposer.compose_with_error_handling(success_step, fail_step, success_step)
|
39
|
+
Hopscotch::Runner.call(error_reduced_fn, success: -> { "success" }, failure: -> (x) { "failure: #{x}" })
|
40
|
+
# => "failure: bad"
|
41
|
+
|
42
|
+
|
43
|
+
# - simple lambdas steps
|
44
|
+
# - runner call function + compose steps inline with success/failure callbacks
|
45
|
+
success_step_1 = -> { Hopscotch::Step.success! }
|
46
|
+
success_step_2 = -> { Hopscotch::Step.success! }
|
47
|
+
|
48
|
+
Hopscotch::Runner.call(
|
49
|
+
Hopscotch::StepComposer.call_each(success_step_1, success_step_2),
|
50
|
+
success: -> { "success" },
|
51
|
+
failure: -> (x) { "failure: #{x}" },
|
52
|
+
)
|
53
|
+
# => "success"
|
54
|
+
|
55
|
+
|
56
|
+
# Module method to compose multiple steps into 1 step
|
57
|
+
# - runner call composed function with success/failure callbacks
|
58
|
+
module ChainSteps
|
59
|
+
extend self
|
60
|
+
def call
|
61
|
+
Hopscotch::StepComposer.call_each(
|
62
|
+
-> { Hopscotch::Step.success! },
|
63
|
+
-> do
|
64
|
+
if 2.even?
|
65
|
+
Hopscotch::Step.success!
|
66
|
+
else
|
67
|
+
Hopscotch::Step.failure!
|
68
|
+
end
|
69
|
+
end
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
Hopscotch::Runner.call_each(
|
75
|
+
ChainSteps,
|
76
|
+
success: -> { "success" },
|
77
|
+
failure: -> (x) { "failure: #{x}" },
|
78
|
+
)
|
79
|
+
# => "success"
|
80
|
+
```
|
81
|
+
|
82
|
+
### Top level architecture
|
83
|
+
|
84
|
+
![hopscotch](https://cloud.githubusercontent.com/assets/873687/9781348/8c23c872-57d6-11e5-8903-991838f4b3c6.png)
|
85
|
+
|
86
|
+
### Runners
|
87
|
+
A runner is a pipeline to run steps and handle the success or failure of the group of them.
|
88
|
+
|
89
|
+
Runners are not meant to be the point of reuse or shared behavior. They are simply a way to run steps.
|
90
|
+
|
91
|
+
Runners can call an array of steps and compose them under the hood.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
Hopscotch::Runner.call_each(
|
95
|
+
-> { Hopscotch::Step.success! },
|
96
|
+
-> { Hopscotch::Step.success! },
|
97
|
+
success: -> { success.call("The step was successful!", Time.now.to_i) },
|
98
|
+
failure: failure
|
99
|
+
)
|
100
|
+
```
|
101
|
+
|
102
|
+
You can optionally compose the steps manually (great for reuse) and just make use of the `Runner#call` method.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
success_step_1 = -> { Hopscotch::Step.success! }
|
106
|
+
success_step_2 = -> { Hopscotch::Step.success! }
|
107
|
+
|
108
|
+
Hopscotch::Runner.call(
|
109
|
+
Hopscotch::StepComposer.call_each(success_step_1, success_step_2),
|
110
|
+
success: -> { success.call("The step was successful!", Time.now.to_i) },
|
111
|
+
failure: failure
|
112
|
+
)
|
113
|
+
```
|
114
|
+
|
115
|
+
### Steps
|
116
|
+
|
117
|
+
A step is a function type. It can be plugged into any module/class as long as it conforms to returning `Hopscotch::Step.success!` or `Hopscotch::Step.failure!`
|
118
|
+
|
119
|
+
These two functions wrap the return value to let the runner know if the step was successful or not.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
module Service
|
123
|
+
module AddItemToCart
|
124
|
+
extend self
|
125
|
+
|
126
|
+
def call(item, cart)
|
127
|
+
if cart.add(item)
|
128
|
+
Hopscotch::Step.success!
|
129
|
+
else
|
130
|
+
Hopscotch::Step.failure!
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
**note** You can optionally pass in values to `success!` and `failure!` to be used outside of the step. ie: `failure!(cart.errors)`
|
138
|
+
|
139
|
+
### A typical use-case
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
class UsersController < ApplicationController
|
143
|
+
def create
|
144
|
+
success = -> (response, time) { redirect_to root_path, notice: "#{response} - at: #{time}" }
|
145
|
+
failure = -> { render :new }
|
146
|
+
Workflow::CreateUser.call(params[:name], success: success, failure: failure)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
module Workflow
|
151
|
+
module CreateUser
|
152
|
+
extend self
|
153
|
+
|
154
|
+
def call(name, success:, failure:)
|
155
|
+
Hopscotch::Runner.call_each(
|
156
|
+
-> { Service::CreateUser.call(name) },
|
157
|
+
success: -> { success.call("Workflow::CreateUser worked!", Time.now.to_i) },
|
158
|
+
failure: failure
|
159
|
+
)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
module Service
|
165
|
+
module CreateUser
|
166
|
+
extend self
|
167
|
+
|
168
|
+
def call(name)
|
169
|
+
if User.create(name: name)
|
170
|
+
Hopscotch::Steps.success!
|
171
|
+
else
|
172
|
+
Hopscotch::Steps.failure!
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
### Reusing steps
|
180
|
+
|
181
|
+
A common problem you might run into when dealing with multiple runners and steps is the need to copy 90% of a previous runner but just change 1 or 2 step calls. Let's make it happen.
|
182
|
+
|
183
|
+
Let's take an example of `Signup` runner which creates a student, and sends them an email.
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
module Workflow
|
187
|
+
module Signup
|
188
|
+
def call(student_params, success:, failure:)
|
189
|
+
# We make heavy use of form objects, so this is a common pattern for us
|
190
|
+
# but you can really do what ever you want in here..
|
191
|
+
form = Form::NewStudent.new(student_params)
|
192
|
+
|
193
|
+
Hopscotch::Runner.call_each(
|
194
|
+
-> { Service::CreateStudent.call(form) }, # these return `Hopscotch::Step.success!` or `Hopscotch::Step.failure!`
|
195
|
+
-> { Service::NotifyStudent.call(form) }
|
196
|
+
success: success,
|
197
|
+
failure: failure
|
198
|
+
)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
Here's a brief example of what the services might look like.
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
module Service
|
208
|
+
module CreateStudent
|
209
|
+
extend self
|
210
|
+
|
211
|
+
def call(student_form)
|
212
|
+
if student_form.valid? && student_form.save
|
213
|
+
Hopscotch::Steps.success!
|
214
|
+
else
|
215
|
+
Hopscotch::Steps.failure!(student_form.errors)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
module Service
|
222
|
+
module NotifyStudent
|
223
|
+
extend self
|
224
|
+
|
225
|
+
def call(student_form)
|
226
|
+
if Notify::SendMail.new(student_form).deliver
|
227
|
+
Hopscotch::Steps.success!
|
228
|
+
else
|
229
|
+
Hopscotch::Steps.failure!
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
Here we just ensure the student is valid and persisted and then send a mailer.
|
237
|
+
|
238
|
+
All is well in love and steps. Let's assume that something in the system has to change (as it rarely does..), and we need to add a new step to the process to the runner, but only for a segmented part of our user base. Phew..
|
239
|
+
|
240
|
+
The new feature request comes in and we want to give free points to a student when they signup if they are home schooled. While we could chunk some lovely if statements into our runner or steps to do this, I prefer to create a new runner only for this particular interaction in the system. Let's start.
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
module Workflow
|
244
|
+
module SignupWithFreePoints
|
245
|
+
def call(student_params, success:, failure:)
|
246
|
+
form = Form::NewStudent.new(student_params)
|
247
|
+
|
248
|
+
Hopscotch::Runner.call_each(
|
249
|
+
-> { Service::CreateStudent.call(form) }, # this is duplication.. :(
|
250
|
+
-> { Service::NotifyStudent.call(form) }, # this is duplication.. :(
|
251
|
+
-> { Service::GiveFreePointsToStudent.call(form) }, # this sucker is the new one
|
252
|
+
success: success,
|
253
|
+
failure: failure
|
254
|
+
)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
While this example _could_ work, we're already duplicating code and things could easily get out of sync with the previous Signup runner. Let's say steps get removed or changed and we forget to update in both places.. bug reports will soon roll in. Let's fix this.
|
261
|
+
|
262
|
+
Here is where Steps shine. Steps can be composed to nest other steps. Let's see how we can clean up our code with a new Step that utilizes this.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
module Service
|
266
|
+
module CreateStudentAndNotify
|
267
|
+
extend self
|
268
|
+
|
269
|
+
def call(student_form)
|
270
|
+
# This will bubble up the success or failure of both of these nested steps
|
271
|
+
# and return a success! or failure! depending on the collected results.
|
272
|
+
Hopscotch::StepComposer.call_each(
|
273
|
+
-> { Service::CreateStudent.call(student_form) },
|
274
|
+
-> { Service::NotifyStudent.call(student_form) }
|
275
|
+
)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
```
|
280
|
+
|
281
|
+
What does this mean for our runner? They get simpler and allow for quick reuse! Let's see.
|
282
|
+
|
283
|
+
```ruby
|
284
|
+
module Workflow
|
285
|
+
module Signup
|
286
|
+
def call(student_params, success:, failure:)
|
287
|
+
form = Form::NewStudent.new(student_params)
|
288
|
+
|
289
|
+
Hopscotch::Runner.call_each(
|
290
|
+
-> { Service::CreateStudentAndNotify.call(form) },
|
291
|
+
success: success,
|
292
|
+
failure: failure
|
293
|
+
)
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
module Workflow
|
299
|
+
module SignupWithFreePoints
|
300
|
+
def call(student_params, success:, failure:)
|
301
|
+
form = Form::NewStudent.new(student_params)
|
302
|
+
|
303
|
+
Hopscotch::Runner.call_each(
|
304
|
+
-> { Service::CreateStudentAndNotify.call(form) }, # re-use steps
|
305
|
+
-> { Service::GiveFreePointsToStudent.call(form) }, # the new step
|
306
|
+
success: success,
|
307
|
+
failure: failure
|
308
|
+
)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
```
|
313
|
+
|
314
|
+
2 runners, different behavior - no duplication. It's a beauty.
|
315
|
+
|
316
|
+
## Development
|
317
|
+
|
318
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
319
|
+
|
320
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
321
|
+
|
322
|
+
## Contributing
|
323
|
+
|
324
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/blake-education/hopscotch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
325
|
+
|
326
|
+
|
327
|
+
## License
|
328
|
+
|
329
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
330
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hopscotch"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/hopscotch.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hopscotch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hopscotch"
|
8
|
+
spec.version = Hopscotch::VERSION
|
9
|
+
spec.authors = ["Garrett Heinlen"]
|
10
|
+
spec.email = ["heinleng@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{simplify complex business logic.}
|
13
|
+
spec.description = %q{Hopscotch allows us to chain together complex logic and ensure if any specific part of the chain fails, everything is rolled back to its original state.}
|
14
|
+
spec.homepage = "https://github.com/blake-education/hopscotch"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
33
|
+
end
|
data/lib/hopscotch.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "hopscotch/version"
|
2
|
+
|
3
|
+
require "hopscotch/error"
|
4
|
+
require "hopscotch/step"
|
5
|
+
require "hopscotch/step_composers/default"
|
6
|
+
require "hopscotch/runners/default"
|
7
|
+
|
8
|
+
module Hopscotch
|
9
|
+
module Runner
|
10
|
+
extend self
|
11
|
+
|
12
|
+
def call(fn, failure:, success:)
|
13
|
+
Hopscotch::Runners::Default.call(fn, failure: failure, success: success)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call_each(*fns, failure:, success:)
|
17
|
+
Hopscotch::Runners::Default.call_each(*fns, failure: failure, success: success)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module StepComposer
|
22
|
+
extend self
|
23
|
+
|
24
|
+
def call_each(*fns)
|
25
|
+
Hopscotch::StepComposers::Default.call_each(fns)
|
26
|
+
end
|
27
|
+
|
28
|
+
def compose_with_error_handling(*fns)
|
29
|
+
Hopscotch::StepComposers::Default.compose_with_error_handling(fns)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Hopscotch
|
2
|
+
# `ErrorValue` denotes an error.
|
3
|
+
#
|
4
|
+
# ```
|
5
|
+
# data ReturnValue = ErrorValue value | value
|
6
|
+
# ```
|
7
|
+
#
|
8
|
+
# In the context of Ruby and `Workflows` this means:
|
9
|
+
# - a value represents an error iff it is an `ErrorValue`
|
10
|
+
# - otherwise the value represents success
|
11
|
+
#
|
12
|
+
class ErrorValue < Struct.new(:value)
|
13
|
+
end
|
14
|
+
|
15
|
+
# `Error` provides helper functions for working with ErrorValue (and thus ReturnValue)
|
16
|
+
module Error
|
17
|
+
extend self
|
18
|
+
|
19
|
+
# Returns `true` if `e` is an error
|
20
|
+
def error?(e)
|
21
|
+
ErrorValue === e
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns `false` if `e` is an error
|
25
|
+
def success?(e)
|
26
|
+
! error?(e)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Convert `e` into a value representing an error, if required.
|
30
|
+
def to_error(e)
|
31
|
+
case e
|
32
|
+
when ErrorValue
|
33
|
+
e
|
34
|
+
else
|
35
|
+
ErrorValue.new(e)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Hopscotch
|
2
|
+
module Runners
|
3
|
+
module Default
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# Execute `fn` within an `ActiveRecord` transaction.
|
7
|
+
#
|
8
|
+
# Rough types:
|
9
|
+
# ```
|
10
|
+
# fn :: void -> ReturnValue
|
11
|
+
# failure :: value -> NilClass
|
12
|
+
#
|
13
|
+
# success :: value -> NilClass
|
14
|
+
# # or
|
15
|
+
# success :: void -> NilClass
|
16
|
+
# ```
|
17
|
+
#
|
18
|
+
# If `fn` returns an `ErrorValue`, roll back the transaction and call `failure` with the unwrapped error `value`.
|
19
|
+
# Otherwise commit the transaction and call success.
|
20
|
+
def call(fn, failure:, success:)
|
21
|
+
error = nil
|
22
|
+
result = nil
|
23
|
+
|
24
|
+
ActiveRecord::Base.transaction do
|
25
|
+
result = fn.call
|
26
|
+
|
27
|
+
if Hopscotch::Error.error?(result)
|
28
|
+
error = result
|
29
|
+
raise ActiveRecord::Rollback
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if error
|
34
|
+
failure.call(error.value)
|
35
|
+
else
|
36
|
+
success.arity == 1 ? success.call(result) : success.call
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# `call_each` composes a list of functions into a single function
|
41
|
+
# which it then passes along to a `call` to perform the workflow logic.
|
42
|
+
#
|
43
|
+
# Each fn should have the rough type:
|
44
|
+
# ```
|
45
|
+
# fn :: void -> ReturnValue
|
46
|
+
# ```
|
47
|
+
#
|
48
|
+
def call_each(*fns, failure:, success:)
|
49
|
+
call ::Hopscotch::StepComposers::Default.compose_with_error_handling(*fns), failure: failure, success: success
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Hopscotch
|
2
|
+
module StepComposers
|
3
|
+
module Default
|
4
|
+
extend self
|
5
|
+
|
6
|
+
# `call_each` composes a list of functions into a single function
|
7
|
+
# which it thens calls, returning the result of the composition
|
8
|
+
#
|
9
|
+
# Each fn should have the rough type:
|
10
|
+
# ```
|
11
|
+
# fn :: void -> ReturnValue
|
12
|
+
# ```
|
13
|
+
#
|
14
|
+
def call_each(*fns)
|
15
|
+
compose_with_error_handling(fns).call
|
16
|
+
end
|
17
|
+
|
18
|
+
# Composes a list of functions into a single function.
|
19
|
+
#
|
20
|
+
# *Note* that this isn't a general purpose composition.
|
21
|
+
# *Note* a function here is anything that responds to `call` i.e. lambda or a singleton module.
|
22
|
+
#
|
23
|
+
# The composed function and each constituent function have the same signature, roughly:
|
24
|
+
# ```
|
25
|
+
# fn :: void -> ReturnValue
|
26
|
+
# ```
|
27
|
+
#
|
28
|
+
# *Note* that `void` is used here to mean "takes no arguments".
|
29
|
+
# This is a dead giveaway that functions of this type signature will have side effects.
|
30
|
+
#
|
31
|
+
def compose_with_error_handling(*fns)
|
32
|
+
redcued_fn = fns.flatten.compact.inject do |composed, fn|
|
33
|
+
-> do
|
34
|
+
last_return = composed.call
|
35
|
+
if Hopscotch::Error.error?(last_return)
|
36
|
+
last_return
|
37
|
+
else
|
38
|
+
fn.call
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
redcued_fn || -> { Hopscotch::Step.success! }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hopscotch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Garrett Heinlen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-14 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: 3.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
description: Hopscotch allows us to chain together complex logic and ensure if any
|
56
|
+
specific part of the chain fails, everything is rolled back to its original state.
|
57
|
+
email:
|
58
|
+
- heinleng@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- hopscotch.gemspec
|
73
|
+
- lib/hopscotch.rb
|
74
|
+
- lib/hopscotch/error.rb
|
75
|
+
- lib/hopscotch/runners/default.rb
|
76
|
+
- lib/hopscotch/step.rb
|
77
|
+
- lib/hopscotch/step_composers/default.rb
|
78
|
+
- lib/hopscotch/version.rb
|
79
|
+
homepage: https://github.com/blake-education/hopscotch
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.7
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: simplify complex business logic.
|
103
|
+
test_files: []
|