workout 0.0.1
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 +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +208 -0
- data/Rakefile +2 -0
- data/lib/workout.rb +135 -0
- data/lib/workout/version.rb +3 -0
- data/workout.gemspec +26 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4dd785717f0d7e8ecdd15a9cfd0b654281324335
|
4
|
+
data.tar.gz: c611ae01b7fea3fa364e53f71db641600f833b5a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5b51601c6b4e8ac03a0710853af0009c598a044120714677ccc39a293d1b8d7de0bd9b3f244a5c81d6065f796022ccb3b292b5b96f2cb9464c1676768e19a45d
|
7
|
+
data.tar.gz: 28020d6b73f05160302b335f94d3beebceeb3676fb22242ad98f40efb8cb7815674ab74868e20c6b4e7ffcb025a188cad2c8d55da0f5f1a855e3d745d93d4108
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 myobie
|
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,208 @@
|
|
1
|
+
# Workout
|
2
|
+
|
3
|
+
Build simple workflows by enumerating the steps to execute. Know if it
|
4
|
+
succeeds or not and get at the failures. A nice way to write service
|
5
|
+
objects.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'workout'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install workout
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### The basics
|
26
|
+
|
27
|
+
Make a simple service object that does one thing:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class SayHello
|
31
|
+
include Workout
|
32
|
+
|
33
|
+
work do
|
34
|
+
puts "Hello"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
SayHello.new.() # => # prints Hello
|
39
|
+
```
|
40
|
+
|
41
|
+
Calling the class method `::work` will define an instance method
|
42
|
+
`#work` and define it as the only step to execute when `#call` is
|
43
|
+
called.
|
44
|
+
|
45
|
+
Or if you need to do three things:
|
46
|
+
_(Steps are executing in the order they are defined.)_
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class SayStuff
|
50
|
+
include Workout
|
51
|
+
|
52
|
+
step :one do
|
53
|
+
puts "one"
|
54
|
+
end
|
55
|
+
|
56
|
+
step :two do
|
57
|
+
puts "two"
|
58
|
+
end
|
59
|
+
|
60
|
+
step :three do
|
61
|
+
puts "three"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
SayStuff.new.() # => # prints one, then two, then three
|
66
|
+
```
|
67
|
+
|
68
|
+
`::step` will define a method named the same as the symbol argument
|
69
|
+
passed. In this example, instances of `SayStuff` will have the methods
|
70
|
+
`#one`, `#two`, and `#three`. Those method names are stored in an array
|
71
|
+
so they are executed in the correct order when `#call` is called.
|
72
|
+
|
73
|
+
If any step `#fail`s or `#raise`s, then the execution stops at that point:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class SayStuff
|
77
|
+
include Workout
|
78
|
+
|
79
|
+
step :one do
|
80
|
+
puts "one"
|
81
|
+
end
|
82
|
+
|
83
|
+
step :two do
|
84
|
+
raise "What is going on here?"
|
85
|
+
end
|
86
|
+
|
87
|
+
step :three do
|
88
|
+
puts "three"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
workflow = SayStuff.new.() # => # prints one
|
93
|
+
|
94
|
+
workflow.errors.first.to_a # => [:two, { message: "What is going on here?", ... }]
|
95
|
+
|
96
|
+
workflow.complete? # => true
|
97
|
+
workflow.valid? # => false
|
98
|
+
workflow.success? # => false
|
99
|
+
```
|
100
|
+
|
101
|
+
### Validations
|
102
|
+
|
103
|
+
Including `Workout` also means including
|
104
|
+
[`ActiveModel::Validations`](http://www.rubydoc.info/gems/activemodel/ActiveModel/Validations).
|
105
|
+
An example of how to use it is:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class EmailReceipt
|
109
|
+
include Workout
|
110
|
+
|
111
|
+
attr_reader :email
|
112
|
+
|
113
|
+
validates :email, format: { with: /.+@.+\..+/ }
|
114
|
+
|
115
|
+
def initialize(email, receipt)
|
116
|
+
@email = email
|
117
|
+
@receipt = receipt
|
118
|
+
end
|
119
|
+
|
120
|
+
step :pdf do
|
121
|
+
@receipt.prepare_pdf
|
122
|
+
end
|
123
|
+
|
124
|
+
step :send_email do
|
125
|
+
ReceiptMailer.email(email, receipt).deliver_later
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
### A real example
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class ChargeStripeCard
|
134
|
+
include Workout
|
135
|
+
|
136
|
+
attr_reader :payment, :payable
|
137
|
+
|
138
|
+
def initialize(stripe_account:, card_token:, payable:, current_user:)
|
139
|
+
super
|
140
|
+
|
141
|
+
@stripe_account = stripe_account
|
142
|
+
@card_token = card_token
|
143
|
+
@payable = payable
|
144
|
+
@current_user = current_user
|
145
|
+
end
|
146
|
+
|
147
|
+
def description
|
148
|
+
"Charge for #{@current_user.email} for #{@payable.description}"
|
149
|
+
end
|
150
|
+
|
151
|
+
def application_percentage
|
152
|
+
0.005
|
153
|
+
end
|
154
|
+
|
155
|
+
def amount
|
156
|
+
@payable.amount
|
157
|
+
end
|
158
|
+
|
159
|
+
def application_fee
|
160
|
+
(amount * application_percentage).to_i
|
161
|
+
end
|
162
|
+
|
163
|
+
validates :amount, numericality: { only_integer: true, greater_than_or_equal_to: 100 }
|
164
|
+
|
165
|
+
step :stripe_charge do
|
166
|
+
Stripe::Charge.create({
|
167
|
+
amount: @payable.amount,
|
168
|
+
currency: @payable.currency,
|
169
|
+
card: @card_token,
|
170
|
+
description: description,
|
171
|
+
application_fee: application_fee
|
172
|
+
}, @stripe_account.access_token)
|
173
|
+
end
|
174
|
+
|
175
|
+
# The return value of the previous step is optionally passed in as an
|
176
|
+
# argument to the current step. The first step would be passed nil.
|
177
|
+
step :build_payment do |charge|
|
178
|
+
charge_response = StripeChargeResponse.new(body: charge.to_hash)
|
179
|
+
Payment.new({
|
180
|
+
stripe_charge_response: charge_response,
|
181
|
+
payable: @payable,
|
182
|
+
stripe_charge_id: charge.id,
|
183
|
+
amount: charge.amount,
|
184
|
+
currency: charge.currency,
|
185
|
+
application_fee: application_fee # TODO: retreive this from the stripe api?
|
186
|
+
})
|
187
|
+
end
|
188
|
+
|
189
|
+
step :save_and_complete do |payment|
|
190
|
+
@payable.payment = payment
|
191
|
+
|
192
|
+
Payable.transaction do
|
193
|
+
payment.save!
|
194
|
+
@payable.pay!
|
195
|
+
@payment = payment
|
196
|
+
complete! # one can force the workflow to be complete at anytime
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
```
|
201
|
+
|
202
|
+
## Contributing
|
203
|
+
|
204
|
+
1. Fork it ( https://github.com/myobie/workout/fork )
|
205
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
206
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
207
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
208
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/workout.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require "workout/version"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "active_model/validations"
|
4
|
+
|
5
|
+
module Workout
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def steps
|
11
|
+
@_steps ||= []
|
12
|
+
end
|
13
|
+
|
14
|
+
def step(name, &blk)
|
15
|
+
define_method name, &blk
|
16
|
+
self.steps << name
|
17
|
+
end
|
18
|
+
|
19
|
+
def work(&blk)
|
20
|
+
define_method :work, &blk
|
21
|
+
self.steps.replace([:work])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
included do
|
26
|
+
attr_accessor :current_step
|
27
|
+
private :current_step=
|
28
|
+
attr_reader :fsm
|
29
|
+
validate :copy_failures_to_errors
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(**opts)
|
33
|
+
@_fsm = MicroMachine.new(:pending).tap do |fsm|
|
34
|
+
fsm.when(:complete, :pending => :completed)
|
35
|
+
fsm.when(:fail, :pending => :failed)
|
36
|
+
end
|
37
|
+
@_failures = []
|
38
|
+
end
|
39
|
+
|
40
|
+
def complete?
|
41
|
+
@_fsm.state == :completed
|
42
|
+
end
|
43
|
+
|
44
|
+
def complete
|
45
|
+
@_fsm.trigger(:complete)
|
46
|
+
end
|
47
|
+
|
48
|
+
def complete!
|
49
|
+
@_fsm.trigger!(:complete)
|
50
|
+
end
|
51
|
+
|
52
|
+
def fail(**args)
|
53
|
+
should_throw = args.delete(:throw) != false
|
54
|
+
@_failures.push({ step: current_step, args: args })
|
55
|
+
@_fsm.trigger(:fail)
|
56
|
+
validate
|
57
|
+
throw :failure if should_throw
|
58
|
+
end
|
59
|
+
|
60
|
+
private def copy_failures_to_errors
|
61
|
+
@_failures.each do |info|
|
62
|
+
errors.add info[:step], info[:args]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def success?
|
67
|
+
complete? && valid?
|
68
|
+
end
|
69
|
+
|
70
|
+
private def rescuing(&blk)
|
71
|
+
if defined?(ActiveRecord::ActiveRecordError)
|
72
|
+
rescuing_with_active_record(&blk)
|
73
|
+
else
|
74
|
+
rescuing_without_active_record(&blk)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private def rescuing_with_active_record(&blk)
|
79
|
+
rescuing_without_active_record do
|
80
|
+
blk.call
|
81
|
+
end
|
82
|
+
rescue ActiveRecord::ActiveRecordError => e
|
83
|
+
fail({
|
84
|
+
message: e.message,
|
85
|
+
subject: e.record,
|
86
|
+
exception: e,
|
87
|
+
throw: false
|
88
|
+
})
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
private def rescuing_without_active_record(&blk)
|
93
|
+
blk.call
|
94
|
+
self
|
95
|
+
rescue StandardError => e
|
96
|
+
fail({
|
97
|
+
message: e.message,
|
98
|
+
subject: e,
|
99
|
+
exception: e,
|
100
|
+
throw: false
|
101
|
+
})
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
def call
|
106
|
+
# don't even try if we are already invalid
|
107
|
+
return self if invalid?
|
108
|
+
|
109
|
+
rescuing do
|
110
|
+
# run each step, possibly feeding the last result to the next
|
111
|
+
self.class.steps.reduce(nil) do |last, name|
|
112
|
+
self.current_step = name
|
113
|
+
|
114
|
+
# #fail will throw :failure instead of raising
|
115
|
+
result = catch :failure do
|
116
|
+
if method(name).arity == 1
|
117
|
+
send name, last
|
118
|
+
else
|
119
|
+
send name
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
if invalid?
|
124
|
+
break
|
125
|
+
else
|
126
|
+
result
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
complete if valid?
|
131
|
+
end
|
132
|
+
ensure
|
133
|
+
self.current_step = nil
|
134
|
+
end
|
135
|
+
end
|
data/workout.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 'workout/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "workout"
|
8
|
+
spec.version = Workout::VERSION
|
9
|
+
spec.authors = ["Nathan Herald"]
|
10
|
+
spec.email = ["me@nathanherald.com"]
|
11
|
+
spec.summary = %q{Build simple workflow service objects.}
|
12
|
+
spec.description = %q{Make objects to represent and execute operations as steps, one by one, and know if it succeeded.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "activesupport"
|
22
|
+
spec.add_dependency "activemodel"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: workout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Herald
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Make objects to represent and execute operations as steps, one by one,
|
70
|
+
and know if it succeeded.
|
71
|
+
email:
|
72
|
+
- me@nathanherald.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/workout.rb
|
83
|
+
- lib/workout/version.rb
|
84
|
+
- workout.gemspec
|
85
|
+
homepage: ''
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.5
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Build simple workflow service objects.
|
109
|
+
test_files: []
|