light_operations 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/README.md +138 -2
- data/lib/light_operations/core.rb +20 -7
- data/lib/light_operations/version.rb +1 -1
- data/spec/lib/core_spec.rb +51 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db2c6c8440e6a48a6ef168af4a7b545132437356
|
4
|
+
data.tar.gz: 575327c90f66617d35a9954efe5870403695faf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84d0758c75019a861344f0ad49ffc2283af17c21aa8c385e84b54eaee2d15f6d746897f65e364527dbf7c8a055261f58aa294212124e30917cd795baab2ab895
|
7
|
+
data.tar.gz: b78d2ae0b6f0776491cc0725c24f543e142e11e2d39814dbcf21fb83beaa5768793ad3408d3b8c4704c6e828aa840b3c3284476bd8e0bd6a360698350355d075
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# LightOperations
|
2
2
|
|
3
|
-
|
3
|
+
When you want have slim controllers or some logic with several operations
|
4
|
+
this gem could help you to have nice separated and clan code. CAN HELP YOU! :D
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -20,7 +21,142 @@ Or install it yourself as:
|
|
20
21
|
|
21
22
|
## Usage
|
22
23
|
|
23
|
-
|
24
|
+
|
25
|
+
### Uses cases
|
26
|
+
|
27
|
+
#### Simple case when you want have user authorization
|
28
|
+
|
29
|
+
Operation
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class AuthOperation < LightOperations::Core
|
33
|
+
rescue_from AuthFail, with: :on_auth_error
|
34
|
+
|
35
|
+
def execute
|
36
|
+
dependency(:auth_service).login(login: login, password: password)
|
37
|
+
end
|
38
|
+
|
39
|
+
def on_auth_error(_exception)
|
40
|
+
fail!([login: 'unknown']) # or subject.errors.add(login: 'unknown')
|
41
|
+
end
|
42
|
+
|
43
|
+
def login
|
44
|
+
params.fetch(:login)
|
45
|
+
end
|
46
|
+
|
47
|
+
def password
|
48
|
+
params.fetch(:password)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Controller way #1
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class AuthController < ApplicationController
|
57
|
+
def new
|
58
|
+
render :new, locals: { account: Account.new }
|
59
|
+
end
|
60
|
+
|
61
|
+
def create
|
62
|
+
auth_op
|
63
|
+
.run
|
64
|
+
.bind_with(self)
|
65
|
+
.on_success(:create_session_with_dashbord_redirection)
|
66
|
+
.on_fail(:render_account_with_errors)
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def create_session_with_dashbord_redirection(account)
|
72
|
+
session_create_for(account)
|
73
|
+
redirect_to :dashboard
|
74
|
+
end
|
75
|
+
|
76
|
+
def render_account_with_errors(account, _errors)
|
77
|
+
render :new, locals: { account: account }
|
78
|
+
end
|
79
|
+
|
80
|
+
def auth_op
|
81
|
+
@auth_op ||= AuthOperation.new(params, auth_service: auth_service)
|
82
|
+
end
|
83
|
+
|
84
|
+
def auth_service
|
85
|
+
@auth_service ||= AuthService.new
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Controller way #2
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class AuthController < ApplicationController
|
94
|
+
def new
|
95
|
+
render :new, locals: { account: Account.new }
|
96
|
+
end
|
97
|
+
|
98
|
+
def create
|
99
|
+
auth_op
|
100
|
+
.run
|
101
|
+
.on_success{ |account| create_session_with_dashbord_redirection(account) }
|
102
|
+
.on_fail { |account, _errors| render :new, locals: { account: account } }
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def create_session_with_dashbord_redirection(account)
|
108
|
+
session_create_for(account)
|
109
|
+
redirect_to :dashboard
|
110
|
+
end
|
111
|
+
|
112
|
+
def auth_op
|
113
|
+
@auth_op ||= AuthOperation.new(params, auth_service: auth_service)
|
114
|
+
end
|
115
|
+
|
116
|
+
def auth_service
|
117
|
+
@auth_service ||= AuthService.new
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
Controller way #3
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
class AuthController < ApplicationController
|
126
|
+
def new
|
127
|
+
render :new, locals: { account: Account.new }
|
128
|
+
end
|
129
|
+
|
130
|
+
def create
|
131
|
+
auth_op.run.on_success(&go_to_dashboard).on_fail(&go_to_login)
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def go_to_dashboard
|
137
|
+
-> (account) do
|
138
|
+
session_create_for(account)
|
139
|
+
redirect_to :dashboard
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def go_to_login
|
144
|
+
-> (account, _errors) { render :new, locals: { account: account } }
|
145
|
+
end
|
146
|
+
|
147
|
+
def auth_op
|
148
|
+
@auth_op ||= AuthOperation.new(params, auth_service: auth_service)
|
149
|
+
end
|
150
|
+
|
151
|
+
def auth_service
|
152
|
+
@auth_service ||= AuthService.new
|
153
|
+
end
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
When operation status is most importent we can simply use `#success?` or `#fail?` on the executed operation
|
158
|
+
|
159
|
+
Errors are available by `#errors` after operation is executed
|
24
160
|
|
25
161
|
## Contributing
|
26
162
|
|
@@ -3,10 +3,9 @@ require 'active_support'
|
|
3
3
|
module LightOperations
|
4
4
|
class Core
|
5
5
|
include ::ActiveSupport::Rescuable
|
6
|
-
include ::ActiveSupport
|
7
6
|
MissingDependency = Class.new(StandardError)
|
8
7
|
|
9
|
-
attr_reader :dependencies, :params, :subject
|
8
|
+
attr_reader :dependencies, :params, :subject, :bind_oject
|
10
9
|
|
11
10
|
def initialize(params = {}, dependencies = {})
|
12
11
|
@params, @dependencies = params, dependencies
|
@@ -18,17 +17,27 @@ module LightOperations
|
|
18
17
|
self
|
19
18
|
rescue => exception
|
20
19
|
rescue_with_handler(exception) || raise
|
21
|
-
ensure
|
22
20
|
self
|
23
21
|
end
|
24
22
|
|
25
|
-
def
|
26
|
-
|
23
|
+
def bind_with(binding_obj)
|
24
|
+
@bind_oject = binding_obj
|
27
25
|
self
|
28
26
|
end
|
29
27
|
|
30
|
-
def
|
31
|
-
|
28
|
+
def on_success(binded_method = nil, &block)
|
29
|
+
if success?
|
30
|
+
bind_oject.send(binded_method, subject) if can_use_binding_method?(binded_method)
|
31
|
+
block.call(subject) if block_given?
|
32
|
+
end
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def on_fail(binded_method = nil, &block)
|
37
|
+
unless success?
|
38
|
+
bind_oject.send(binded_method, subject, errors) if can_use_binding_method?(binded_method)
|
39
|
+
block.call(subject, errors) if block_given?
|
40
|
+
end
|
32
41
|
self
|
33
42
|
end
|
34
43
|
|
@@ -46,6 +55,10 @@ module LightOperations
|
|
46
55
|
|
47
56
|
protected
|
48
57
|
|
58
|
+
def can_use_binding_method?(method_name)
|
59
|
+
method_name && bind_oject && bind_oject.respond_to?(method_name)
|
60
|
+
end
|
61
|
+
|
49
62
|
def execute
|
50
63
|
fail 'Not implemented yet'
|
51
64
|
end
|
data/spec/lib/core_spec.rb
CHANGED
@@ -18,6 +18,15 @@ describe LightOperations::Core do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
context 'use cases' do
|
21
|
+
let(:binding_object) do
|
22
|
+
Class.new(Object).tap do |klass|
|
23
|
+
klass.class_eval do
|
24
|
+
def success_action(_subject); end
|
25
|
+
|
26
|
+
def error_action(_subject, _errors); end
|
27
|
+
end
|
28
|
+
end.new
|
29
|
+
end
|
21
30
|
|
22
31
|
# dependency using
|
23
32
|
|
@@ -93,16 +102,30 @@ describe LightOperations::Core do
|
|
93
102
|
end
|
94
103
|
end
|
95
104
|
|
96
|
-
|
97
|
-
|
98
|
-
expect(
|
105
|
+
context '#on_success' do
|
106
|
+
it 'when bind_with and send_method is used' do
|
107
|
+
expect(binding_object).to receive(:success_action).with(:success)
|
108
|
+
subject.run.bind_with(binding_object).on_success(:success_action)
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'when block is userd' do
|
112
|
+
subject.run.on_success do |result|
|
113
|
+
expect(result).to eq(:success)
|
114
|
+
end
|
99
115
|
end
|
100
116
|
end
|
101
117
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
118
|
+
context '#on_fail' do
|
119
|
+
it 'when bind_with and send_method is used' do
|
120
|
+
expect(binding_object).not_to receive(:error_action)
|
121
|
+
subject.run.bind_with(binding_object).on_fail(:error_action)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'when block is userd' do
|
125
|
+
block_to_exec = -> () {}
|
126
|
+
expect(block_to_exec).not_to receive(:call)
|
127
|
+
subject.run.on_fail(&block_to_exec)
|
128
|
+
end
|
106
129
|
end
|
107
130
|
|
108
131
|
it '#errors' do
|
@@ -128,17 +151,30 @@ describe LightOperations::Core do
|
|
128
151
|
end
|
129
152
|
end
|
130
153
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
154
|
+
context '#on_success' do
|
155
|
+
it 'when bind_with and send_method is used' do
|
156
|
+
expect(binding_object).not_to receive(:success_action)
|
157
|
+
subject.run.bind_with(binding_object).on_success(:success_action)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'when block is userd' do
|
161
|
+
block_to_exec = -> () {}
|
162
|
+
expect(block_to_exec).not_to receive(:call)
|
163
|
+
subject.run.on_success(&block_to_exec)
|
164
|
+
end
|
135
165
|
end
|
136
166
|
|
137
|
-
|
167
|
+
context '#on_fail' do
|
168
|
+
it 'when bind_with and send_method is used' do
|
169
|
+
expect(binding_object).to receive(:error_action).with(:fail, [email: :unknown])
|
170
|
+
subject.run.bind_with(binding_object).on_fail(:error_action)
|
171
|
+
end
|
138
172
|
|
139
|
-
|
140
|
-
|
141
|
-
|
173
|
+
it 'when block is userd' do
|
174
|
+
subject.run.on_fail do |result, errors|
|
175
|
+
expect(result).to eq(:fail)
|
176
|
+
expect(errors).to eq([email: :unknown])
|
177
|
+
end
|
142
178
|
end
|
143
179
|
end
|
144
180
|
|