responsibility 0.1.0 → 0.1.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 +4 -4
- data/Gemfile.lock +1 -1
- data/LICENSE.txt +21 -0
- data/README.md +50 -3
- data/Rakefile +0 -4
- data/lib/responsibility.rb +4 -0
- data/lib/responsibility/class_methods.rb +34 -21
- data/lib/responsibility/failure_error.rb +1 -0
- data/lib/responsibility/initializer.rb +2 -1
- data/lib/responsibility/instance_methods.rb +2 -6
- data/lib/responsibility/version.rb +1 -1
- data/responsibility.gemspec +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8e4652bca8e4700672ee129c9a6af61a1f8119c4
|
|
4
|
+
data.tar.gz: 49a393345b8d3130d3914a78d69c5dba91aac172
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 56c619981d0d09d269c561dcc5f67d423505b859113819cc08ddcb1fa772ecb02918fe73b878613bff12350a4e6b13404e778c92faa72b564f4566846a9d7d25
|
|
7
|
+
data.tar.gz: 93c81363a141a066f12ae345186c9826dffef04f02d2a0a26ca204e726036d1b1243a036049c5fe207feddc40f37b71525e6ac9651a6d9d10a3f8d22a4ba48ab
|
data/Gemfile.lock
CHANGED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Erick Brower
|
|
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
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
# Responsibility
|
|
1
|
+
# Responsibility [](https://travis-ci.org/erickbrower/responsibility)
|
|
2
2
|
|
|
3
3
|
A Responsibility is a class that provides a single piece of functionality,
|
|
4
|
-
as per the Single Responsibility Principle
|
|
5
|
-
[https://en.wikipedia.org/wiki/Single_responsibility_principle]. This class
|
|
4
|
+
as per the [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single_responsibility_principle). This class
|
|
6
5
|
provides a single method called "perform", with optional "before" and
|
|
7
6
|
"after" hooks. It also keeps track of any errors set during execution.
|
|
8
7
|
|
|
@@ -24,6 +23,11 @@ Or install it yourself as:
|
|
|
24
23
|
|
|
25
24
|
## Usage
|
|
26
25
|
|
|
26
|
+
Define a class, `include Responsibility`, and define a `perform` method.
|
|
27
|
+
If `before` is defined and returns `false` or calls `fail!` then the `perform` or `after` methods will not be run. The same rule applies to `perform`, and in the case of failure `after` will not be called.
|
|
28
|
+
|
|
29
|
+
Any keyword arguments (or just a hash) passed to `perform` will become attributes of the resulting object
|
|
30
|
+
|
|
27
31
|
A simple example could be something like:
|
|
28
32
|
|
|
29
33
|
```ruby
|
|
@@ -50,6 +54,49 @@ user = User.new(
|
|
|
50
54
|
password: "Wubba Lubba Dub Dub!"
|
|
51
55
|
)
|
|
52
56
|
result = UserSignupService.perform(user: user)
|
|
57
|
+
result.success? #=> true
|
|
58
|
+
result.errors #=> []
|
|
59
|
+
result.user #=> Our newly persisted user
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
If errors are created during `before`, `perform`, or `after` then the service fails. In the example above, if the user isn't valid we add an error. In this case the result object would look like:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
user = User.new(
|
|
67
|
+
email: "",
|
|
68
|
+
password: ""
|
|
69
|
+
)
|
|
70
|
+
result = UserSignupService.perform(user: user)
|
|
71
|
+
result.success? #=> false
|
|
72
|
+
result.errors #=> ["User was not provided or is not valid"]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Another contrived example with an explicit failure, assuming the User object couldn't be persisted:
|
|
76
|
+
```ruby
|
|
77
|
+
class UserSignupService
|
|
78
|
+
include Responsibility
|
|
79
|
+
|
|
80
|
+
def before
|
|
81
|
+
unless user.present? && user.valid?
|
|
82
|
+
errors << "User was not provided or is not valid"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def perform
|
|
87
|
+
unless user.save
|
|
88
|
+
fail!(message: "User could not be saved")
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def after
|
|
93
|
+
UserConfirmationEmailService.perform(email: user.email)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
result = UserSignupService.perform(user: user)
|
|
98
|
+
result.success? #=> false
|
|
99
|
+
result.errors #=> ["User could not be saved"]
|
|
53
100
|
|
|
54
101
|
```
|
|
55
102
|
|
data/Rakefile
CHANGED
data/lib/responsibility.rb
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
# Author:: Erick Brower (mailto:cerickbrower@gmail.com)
|
|
2
|
+
# License:: MIT
|
|
3
|
+
|
|
1
4
|
require "responsibility/version"
|
|
2
5
|
require "responsibility/initializer"
|
|
3
6
|
require "responsibility/instance_methods"
|
|
4
7
|
require "responsibility/class_methods"
|
|
8
|
+
require "responsibility/failure_error"
|
|
5
9
|
|
|
6
10
|
module Responsibility
|
|
7
11
|
def self.included(base)
|
|
@@ -1,41 +1,54 @@
|
|
|
1
1
|
module Responsibility
|
|
2
2
|
module ClassMethods
|
|
3
|
-
def before(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
obj.instance_variable_set("@success", !!result)
|
|
3
|
+
def before(service)
|
|
4
|
+
begin
|
|
5
|
+
if service.respond_to?(:before)
|
|
6
|
+
if !service.before || service.errors.any?
|
|
7
|
+
handle_failure(service: service)
|
|
8
|
+
end
|
|
10
9
|
end
|
|
10
|
+
rescue FailureError => e
|
|
11
|
+
handle_failure(service: service, error: e)
|
|
11
12
|
end
|
|
13
|
+
service.success?
|
|
12
14
|
end
|
|
13
15
|
|
|
14
|
-
def after(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
obj.instance_variable_set("@success", !!result)
|
|
16
|
+
def after(service)
|
|
17
|
+
begin
|
|
18
|
+
if service.respond_to?(:after)
|
|
19
|
+
if !service.after || service.errors.any?
|
|
20
|
+
handle_failure(service: service)
|
|
21
|
+
end
|
|
21
22
|
end
|
|
23
|
+
rescue FailureError => e
|
|
24
|
+
handle_failure(service: service, error: e)
|
|
22
25
|
end
|
|
26
|
+
service.success?
|
|
23
27
|
end
|
|
24
28
|
|
|
25
29
|
def perform(*args)
|
|
26
30
|
args = {} if args.is_a?(Array)
|
|
27
31
|
service = new(args)
|
|
28
|
-
before(service)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
if before(service)
|
|
33
|
+
begin
|
|
34
|
+
service.perform
|
|
35
|
+
handle_failure(service: service) if service.errors.any?
|
|
36
|
+
rescue FailureError => e
|
|
37
|
+
handle_failure(service: service, error: e)
|
|
38
|
+
end
|
|
34
39
|
end
|
|
35
40
|
if service.success?
|
|
36
|
-
after(service)
|
|
41
|
+
if !after(service) || service.errors.any?
|
|
42
|
+
handle_failure(service: service)
|
|
43
|
+
end
|
|
37
44
|
end
|
|
38
45
|
service
|
|
39
46
|
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
def handle_failure(service:, error: nil)
|
|
50
|
+
service.instance_variable_set("@success", false)
|
|
51
|
+
service.errors << error.message if error
|
|
52
|
+
end
|
|
40
53
|
end
|
|
41
54
|
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class FailureError < StandardError; end;
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
module Responsibility
|
|
2
2
|
module Initializer
|
|
3
3
|
def initialize(args)
|
|
4
|
-
args[:errors] = []
|
|
5
4
|
args.each do |key, val|
|
|
6
5
|
self.class.send(:attr_accessor, key)
|
|
7
6
|
instance_variable_set("@#{key}", val)
|
|
8
7
|
end
|
|
9
8
|
self.class.send(:attr_reader, :success)
|
|
10
9
|
instance_variable_set("@success", true)
|
|
10
|
+
self.class.send(:attr_reader, :errors)
|
|
11
|
+
instance_variable_set("@errors", [])
|
|
11
12
|
end
|
|
12
13
|
end
|
|
13
14
|
end
|
data/responsibility.gemspec
CHANGED
|
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
|
17
17
|
provides a single method called "perform", with optional "before" and
|
|
18
18
|
"after" hooks.
|
|
19
19
|
HEREDOC
|
|
20
|
-
spec.homepage = "http://
|
|
20
|
+
spec.homepage = "http://github.com/erickbrower/responsibility"
|
|
21
21
|
|
|
22
22
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
23
23
|
f.match(%r{^(test|spec|features)/})
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: responsibility
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Erick Brower
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-10-
|
|
11
|
+
date: 2017-10-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -83,17 +83,19 @@ files:
|
|
|
83
83
|
- ".travis.yml"
|
|
84
84
|
- Gemfile
|
|
85
85
|
- Gemfile.lock
|
|
86
|
+
- LICENSE.txt
|
|
86
87
|
- README.md
|
|
87
88
|
- Rakefile
|
|
88
89
|
- bin/console
|
|
89
90
|
- bin/setup
|
|
90
91
|
- lib/responsibility.rb
|
|
91
92
|
- lib/responsibility/class_methods.rb
|
|
93
|
+
- lib/responsibility/failure_error.rb
|
|
92
94
|
- lib/responsibility/initializer.rb
|
|
93
95
|
- lib/responsibility/instance_methods.rb
|
|
94
96
|
- lib/responsibility/version.rb
|
|
95
97
|
- responsibility.gemspec
|
|
96
|
-
homepage: http://
|
|
98
|
+
homepage: http://github.com/erickbrower/responsibility
|
|
97
99
|
licenses: []
|
|
98
100
|
metadata: {}
|
|
99
101
|
post_install_message:
|