operators-service 0.2.0 → 0.2.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/README.md +96 -7
- data/lib/operators/service.rb +8 -0
- data/operators-service.gemspec +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 634bbd42b41f927730054c62e7e20dac29645295
|
4
|
+
data.tar.gz: e10721496f308ed987c709e098414860256a0d02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb61e37922dbf36f925bd08fa137f958b3592c3735e2a33925ae49d5e22672c07b2e8fc9d4a5456035d62ba5ca0499788192a79a2ac81a4ad681d562fbe2f310
|
7
|
+
data.tar.gz: c0c0b2de4616dd5dd009e1d2f64a87ee7a23ec0293d1d8da8b8f8e87231c1173c6e8e54d477130d623cd2ae4f85969dd75fea35601372d8bb7b957ac6db94844
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Operators::Service
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/operators-service)
|
4
|
+
[](https://circleci.com/gh/operators-rb/operators-service)
|
5
|
+
[](https://codeclimate.com/github/operators-rb/operators-service/maintainability)
|
6
|
+
|
7
|
+
Operators::Service is a lightweight implementation of Service Object based on [Either Monad](https://github.com/dry-rb/dry-monads). That gives an home of application business logic.
|
4
8
|
|
5
9
|
Service Objects are created when an action:
|
6
10
|
|
@@ -28,20 +32,105 @@ Or install it yourself as:
|
|
28
32
|
|
29
33
|
## Usage
|
30
34
|
|
31
|
-
|
35
|
+
```ruby
|
36
|
+
class UserCreator < Operators::Service
|
37
|
+
attr_reader :params
|
38
|
+
|
39
|
+
def initialize(params)
|
40
|
+
@params = params
|
41
|
+
end
|
42
|
+
|
43
|
+
def call
|
44
|
+
user = User.create(params)
|
45
|
+
if !user.new_record?
|
46
|
+
success(user)
|
47
|
+
else
|
48
|
+
failure(user.errors)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
`success` - returns instance of `Dry::Monads::Either::Right`
|
55
|
+
|
56
|
+
`failure` - returns instance of `Dry::Monads::Either::Left`
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class UsersController < ApplicationController
|
60
|
+
def create
|
61
|
+
UserCreator.call(user_params).fmap do |user|
|
62
|
+
render json: user
|
63
|
+
end.or_fmap do |errors|
|
64
|
+
render json: errors.full_messages
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def user_params
|
71
|
+
params.require(:user).permit!
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
### More complicated usage
|
77
|
+
|
78
|
+
If you need `rescue_callbacks` then you should define `calling` instead of `call`.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Auth < Operators::Service
|
82
|
+
rescue_callbacks AuthError, CredentialsError
|
83
|
+
|
84
|
+
def initialize(options)
|
85
|
+
@options = options
|
86
|
+
end
|
32
87
|
|
33
|
-
|
88
|
+
def calling
|
89
|
+
return failure('User already authed') if @options[:failure] # returns Dry::Monads::Left('User already authed')
|
34
90
|
|
35
|
-
|
91
|
+
first_auth_transaction
|
92
|
+
second_auth_transaction
|
36
93
|
|
37
|
-
|
94
|
+
success('ok') # returns Dry::Monads::Right('ok')
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def first_auth_transaction
|
100
|
+
# do something...
|
101
|
+
end
|
102
|
+
|
103
|
+
def second_auth_transaction
|
104
|
+
raise AuthError, 'Auth error message' if @options[:auth_error]
|
105
|
+
end
|
106
|
+
|
107
|
+
# Wrapper for your error result
|
108
|
+
def error_wrap(error)
|
109
|
+
error # 'User already authed' || 'Auth error message'
|
110
|
+
end
|
111
|
+
|
112
|
+
# Wrapper for your success results
|
113
|
+
def success_wrap(success_result)
|
114
|
+
success_result # ok
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
success = Auth.call(email: 'email', password: 'password')
|
121
|
+
# Dry::Monads::Right('ok')
|
122
|
+
|
123
|
+
failure = Auth.call(email: 'email', password: 'password', failure: true)
|
124
|
+
# Dry::Monads::Left('User already authed')
|
125
|
+
|
126
|
+
raised_error = Auth.call(email: 'email', password: 'password', auth_error: true)
|
127
|
+
# Dry::Monads::Left('Auth error message')
|
128
|
+
```
|
38
129
|
|
39
130
|
## Contributing
|
40
131
|
|
41
132
|
Bug reports and pull requests are welcome on GitHub at https://github.com/operators-rb/operators-service.
|
42
133
|
|
43
|
-
|
44
134
|
## License
|
45
135
|
|
46
136
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
47
|
-
|
data/lib/operators/service.rb
CHANGED
data/operators-service.gemspec
CHANGED
@@ -5,13 +5,13 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'operators-service'
|
8
|
-
spec.version = '0.2.
|
8
|
+
spec.version = '0.2.1'
|
9
9
|
spec.authors = ['Yaroslav Bezrukavyi', 'Kirill Shevchenko']
|
10
10
|
spec.email = ['yaroslav.bezrukavyi@gmail.com', 'hello@kirillshevch.com']
|
11
11
|
|
12
12
|
spec.summary = 'You will always know the type of the result'
|
13
13
|
spec.description = 'Service Object is based on Either Monad'
|
14
|
-
spec.homepage = '
|
14
|
+
spec.homepage = 'https://github.com/operators-rb/operators-service'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: operators-service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yaroslav Bezrukavyi
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-08-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-monads
|
@@ -116,7 +116,7 @@ files:
|
|
116
116
|
- operators-service.gemspec
|
117
117
|
- spec/operators/service_spec.rb
|
118
118
|
- spec/spec_helper.rb
|
119
|
-
homepage:
|
119
|
+
homepage: https://github.com/operators-rb/operators-service
|
120
120
|
licenses:
|
121
121
|
- MIT
|
122
122
|
metadata: {}
|
@@ -136,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
136
|
version: '0'
|
137
137
|
requirements: []
|
138
138
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.6.13
|
140
140
|
signing_key:
|
141
141
|
specification_version: 4
|
142
142
|
summary: You will always know the type of the result
|