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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02f0e60904dcfc6c7baeaa3eb7c5961569324a29
4
- data.tar.gz: 581a8c1c10bb01243a3f7d6e8916d17143d5ed23
3
+ metadata.gz: 634bbd42b41f927730054c62e7e20dac29645295
4
+ data.tar.gz: e10721496f308ed987c709e098414860256a0d02
5
5
  SHA512:
6
- metadata.gz: fcee045378315213c2534675556e36fb2c656b705fac43364a473ba7c804406c802692310151a46ced25295846b34175348b12a178f8eff6c77fe4fc3317c7ee
7
- data.tar.gz: cc75bb53af3b0a8218383af1a033e5c3596a9d6e92962c8b9d1ab2eca9124bda3dd54e7867eb6634617c19ba6d98191e595a56ef1c0085ddc8a5d0d5d3963296
6
+ metadata.gz: eb61e37922dbf36f925bd08fa137f958b3592c3735e2a33925ae49d5e22672c07b2e8fc9d4a5456035d62ba5ca0499788192a79a2ac81a4ad681d562fbe2f310
7
+ data.tar.gz: c0c0b2de4616dd5dd009e1d2f64a87ee7a23ec0293d1d8da8b8f8e87231c1173c6e8e54d477130d623cd2ae4f85969dd75fea35601372d8bb7b957ac6db94844
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Operators::Service
2
2
 
3
- Operators::Service is a lightweight implementation of Service Object based on Either Monad. That gives an home of application business logic.
3
+ [![Gem Version](https://badge.fury.io/rb/operators-service.svg)](https://badge.fury.io/rb/operators-service)
4
+ [![CircleCI](https://circleci.com/gh/operators-rb/operators-service.svg?style=shield&circle-token=ce6107feab9a4989e9c8163c6d652bb1bad40cf7)](https://circleci.com/gh/operators-rb/operators-service)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/3fc7bafea686b44d257f/maintainability)](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
- TODO: Write usage instructions here
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
- ## Development
88
+ def calling
89
+ return failure('User already authed') if @options[:failure] # returns Dry::Monads::Left('User already authed')
34
90
 
35
- 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.
91
+ first_auth_transaction
92
+ second_auth_transaction
36
93
 
37
- 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).
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
-
@@ -8,6 +8,14 @@ module Operators
8
8
  failure(error.message)
9
9
  end
10
10
  end
11
+
12
+ define_method :failure_wrapper do |&block|
13
+ begin
14
+ block.call
15
+ rescue *exceptions => error
16
+ failure(error.message)
17
+ end
18
+ end
11
19
  end
12
20
 
13
21
  rescue_callbacks
@@ -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.0'
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 = 'http://operators-rb.org'
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.0
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: 2017-09-07 00:00:00.000000000 Z
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: http://operators-rb.org
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.5.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