light_operations 1.2.3 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -1
- data/.travis.yml +2 -2
- data/README.md +21 -26
- data/lib/light_operations/core.rb +5 -3
- data/lib/light_operations/flow.rb +1 -1
- data/lib/light_operations/version.rb +1 -1
- data/light_operations.gemspec +1 -1
- data/spec/lib/core_spec.rb +22 -5
- data/spec/lib/flow_spec.rb +0 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6eb1ef4942cfe0f54c0ffbd0eed62d760985938e
|
4
|
+
data.tar.gz: b7abe127ef43fda487f71433bd819a4f88d99975
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 489534160e4dcdf6b79fa88bfd512cf863cf93eaae3098a3df0901a7d53a918e89cef871298eabf6f1636a8ee9165e9afd428f8094c48196aec8f0a5639aa50b
|
7
|
+
data.tar.gz: d3d817d97798ceaaefea0d55cb4e422e6e841f4de20736a11595f9d59d973521ef8944de1e02d6c106875fc0db321837832f78fad1df43df8389e2e92c593b7c
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -5,8 +5,8 @@
|
|
5
5
|
[![Dependency Status](https://gemnasium.com/pniemczyk/light_operations.svg)](https://gemnasium.com/pniemczyk/light_operations)
|
6
6
|
[![Code Climate](https://codeclimate.com/github/pniemczyk/light_operations/badges/gpa.svg)](https://codeclimate.com/github/pniemczyk/light_operations)
|
7
7
|
|
8
|
-
When you want have slim controllers or some logic with several operations
|
9
|
-
this gem could help you to have nice separated and
|
8
|
+
When you want to have slim controllers or some logic with several operations
|
9
|
+
this gem could help you to have nice separated and clean code. CAN HELP YOU! :D
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -28,17 +28,17 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
## How it works
|
30
30
|
|
31
|
-
|
31
|
+
Basically, this is a Container for business logic.
|
32
32
|
|
33
33
|
You can define dependencies during initialization and run with custom parameters.
|
34
34
|
When you define deferred actions on `success` and `fail` before operation execution is finished,
|
35
|
-
after execution one of those
|
36
|
-
Actions could be a block (Proc) or you could
|
37
|
-
by binding operation with specific object with those methods.
|
35
|
+
after execution one of those actions depend on for execution result will be executed.
|
36
|
+
Actions could be a block (Proc) or you could delegate execution to method another object,
|
37
|
+
by binding operation with the specific object with those methods.
|
38
38
|
You also could use operation as simple execution and check status by `success?` or `fail?` method
|
39
39
|
and then by using `subject` and `errors` method build your own logic to finish your result.
|
40
|
-
There
|
41
|
-
You can build
|
40
|
+
There are many possible use-cases where and how you could use operations.
|
41
|
+
You can build cascade of operations, use them one after the other,
|
42
42
|
use them recursively and a lot more.
|
43
43
|
|
44
44
|
|
@@ -132,7 +132,7 @@ MyOperation.new.on_success { |operation| render :done, locals: { model: operatio
|
|
132
132
|
MyOperation.new.on(success: -> () { |operation| render :done, locals: { model: operation.subject } )
|
133
133
|
```
|
134
134
|
|
135
|
-
When you bind operation with
|
135
|
+
When you bind operation with another object you could delegate actions to bound object methods
|
136
136
|
|
137
137
|
```ruby
|
138
138
|
# 1
|
@@ -207,13 +207,12 @@ Operation
|
|
207
207
|
|
208
208
|
```ruby
|
209
209
|
class ArticleVoteBumperOperation < LightOperations::Core
|
210
|
-
rescue_from ActiveRecord::
|
210
|
+
rescue_from ActiveRecord::RecordInvalid, with: :on_ar_error
|
211
211
|
|
212
212
|
def execute(_params = nil)
|
213
|
-
dependency(:article_model)
|
214
|
-
|
215
|
-
|
216
|
-
end
|
213
|
+
article = dependency(:article_model)
|
214
|
+
article.vote = article.vote.next
|
215
|
+
article.save!
|
217
216
|
{ success: true }
|
218
217
|
end
|
219
218
|
|
@@ -228,23 +227,19 @@ Controller
|
|
228
227
|
```ruby
|
229
228
|
class ArticleVotesController < ApplicationController
|
230
229
|
def up
|
230
|
+
operation = ArticleVoteBumperOperation.new(article_model: article)
|
231
231
|
response = operation.run.success? ? response.subject : response.errors
|
232
232
|
render :up, json: response
|
233
233
|
end
|
234
234
|
|
235
235
|
private
|
236
|
-
|
237
|
-
def operation
|
238
|
-
@operation ||= ArticleVoteBumperOperation.new(article_model: article)
|
239
|
-
end
|
240
|
-
|
241
236
|
def article
|
242
237
|
Article.find(params.require(:id))
|
243
238
|
end
|
244
239
|
end
|
245
240
|
```
|
246
241
|
|
247
|
-
#### Basic recursive execution to collect
|
242
|
+
#### Basic recursive execution to collect news feeds from 2 sources
|
248
243
|
|
249
244
|
Operation
|
250
245
|
|
@@ -486,7 +481,7 @@ class AuthController < ApplicationController
|
|
486
481
|
end
|
487
482
|
```
|
488
483
|
|
489
|
-
Register success and fails action is
|
484
|
+
Register success and fails action is available by `#on` like:
|
490
485
|
|
491
486
|
```ruby
|
492
487
|
def create
|
@@ -500,16 +495,16 @@ Operation have some helper methods (to improve recursive execution)
|
|
500
495
|
- `#unbind!` => unbind binded object
|
501
496
|
- `#clear_subject_with_errors!` => clear subject and errors
|
502
497
|
|
503
|
-
When operation status is most
|
498
|
+
When operation status is most important we can simply use `#success?` or `#fail?` on the executed operation
|
504
499
|
|
505
500
|
Errors are available by `#errors` after operation is executed
|
506
501
|
|
507
502
|
### Whats new in 1.2.x
|
508
|
-
New module LightOperations::Flow which gives very simple and easy way to create operation per action in controller (tested on rails).
|
503
|
+
New module LightOperations::Flow which gives very simple and easy way to create operation per action in the controller (tested on rails).
|
509
504
|
|
510
505
|
#### How it works:
|
511
506
|
|
512
|
-
include module in controller like this
|
507
|
+
include the module in a controller like this
|
513
508
|
```ruby
|
514
509
|
class AccountsController < VersionController
|
515
510
|
include LightOperations::Flow
|
@@ -560,7 +555,7 @@ But it is not all :D (operation params gives you a lot more)
|
|
560
555
|
class AccountsController < VersionController
|
561
556
|
include LightOperations::Flow
|
562
557
|
operation(
|
563
|
-
:accounts, # top
|
558
|
+
:accounts, # top-level namespace
|
564
559
|
namespace: Operations, # Base namespace by default is Kernel
|
565
560
|
actions: [:create, :show], # those are operations executed by router
|
566
561
|
default_view: nil, # By changing this option you can have one method for render all successful operations for all actions.
|
@@ -570,7 +565,7 @@ class AccountsController < VersionController
|
|
570
565
|
end
|
571
566
|
```
|
572
567
|
|
573
|
-
This simple module should give you power to create something like this:
|
568
|
+
This simple module should give you the power to create something like this:
|
574
569
|
|
575
570
|
```ruby
|
576
571
|
module Api
|
@@ -8,7 +8,7 @@ module LightOperations
|
|
8
8
|
attr_reader :subject
|
9
9
|
|
10
10
|
def self.subject_name(method_name)
|
11
|
-
send(:define_method, method_name, proc {
|
11
|
+
send(:define_method, method_name, proc { subject })
|
12
12
|
end
|
13
13
|
|
14
14
|
def initialize(dependencies = {})
|
@@ -18,7 +18,7 @@ module LightOperations
|
|
18
18
|
# do no.t override this method
|
19
19
|
def run(params = {})
|
20
20
|
clear_subject_with_errors!
|
21
|
-
@subject = execute(params)
|
21
|
+
@subject = method(:execute).arity == 0 ? execute : execute(params)
|
22
22
|
execute_actions
|
23
23
|
self
|
24
24
|
rescue => exception
|
@@ -55,7 +55,9 @@ module LightOperations
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def clear_subject_with_errors!
|
58
|
-
@subject
|
58
|
+
@subject = nil
|
59
|
+
@fail_errors = nil
|
60
|
+
@errors = nil
|
59
61
|
self
|
60
62
|
end
|
61
63
|
|
@@ -7,7 +7,7 @@ module LightOperations
|
|
7
7
|
attr_reader :operation_opts, :operation_dependencies
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
-
def operation(operation_name, namespace: Kernel, actions: [], default_view: nil, view_prefix: 'render_', default_fail_view: nil, fail_view_prefix: 'render_fail_')
|
10
|
+
def operation(operation_name, namespace: Kernel, actions: [], default_view: nil, view_prefix: 'render_', default_fail_view: nil, fail_view_prefix: 'render_fail_') # rubocop:disable all
|
11
11
|
actions.each do |action_name|
|
12
12
|
operation_method = "#{action_name}_op"
|
13
13
|
|
data/light_operations.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = 'https://github.com/pniemczyk/light_operations'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split(
|
16
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
data/spec/lib/core_spec.rb
CHANGED
@@ -50,7 +50,7 @@ describe LightOperations::Core do
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
before { subject.on(success:
|
53
|
+
before { subject.on(success: ->(_subject) {}) }
|
54
54
|
|
55
55
|
it 'is allowed when is initialized correctly' do
|
56
56
|
expect { subject.run }.not_to raise_error
|
@@ -69,7 +69,7 @@ describe LightOperations::Core do
|
|
69
69
|
context '.rescue_from specific error' do
|
70
70
|
TestError = Class.new(StandardError)
|
71
71
|
|
72
|
-
before { subject.on(success:
|
72
|
+
before { subject.on(success: ->(_subject) {}) }
|
73
73
|
|
74
74
|
context 'by block' do
|
75
75
|
subject do
|
@@ -139,7 +139,7 @@ describe LightOperations::Core do
|
|
139
139
|
end
|
140
140
|
|
141
141
|
it 'when block is used' do
|
142
|
-
block_to_exec =
|
142
|
+
block_to_exec = -> {}
|
143
143
|
expect(block_to_exec).not_to receive(:call)
|
144
144
|
subject.on_fail(&block_to_exec).run
|
145
145
|
end
|
@@ -175,7 +175,7 @@ describe LightOperations::Core do
|
|
175
175
|
end
|
176
176
|
|
177
177
|
it 'when block is used' do
|
178
|
-
block_to_exec =
|
178
|
+
block_to_exec = -> {}
|
179
179
|
expect(block_to_exec).not_to receive(:call)
|
180
180
|
subject.on_success(&block_to_exec).run
|
181
181
|
end
|
@@ -226,7 +226,7 @@ describe LightOperations::Core do
|
|
226
226
|
end
|
227
227
|
|
228
228
|
it '#clear_subject_with_errors!' do
|
229
|
-
%w{
|
229
|
+
%w{subject fail_errors errors}.each do |variable|
|
230
230
|
subject.instance_variable_set("@#{variable}", variable)
|
231
231
|
end
|
232
232
|
expect(subject.subject).to eq('subject')
|
@@ -288,4 +288,21 @@ describe LightOperations::Core do
|
|
288
288
|
subject.run(result: :success)
|
289
289
|
end
|
290
290
|
end
|
291
|
+
|
292
|
+
context 'Operation execution without params' do
|
293
|
+
subject do
|
294
|
+
subject_factory do
|
295
|
+
def execute
|
296
|
+
'hello world!'
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
it 'is allowed' do
|
302
|
+
subject
|
303
|
+
.bind_with(binding_object)
|
304
|
+
.on(success: :success_action, fail: :error_action)
|
305
|
+
expect(subject.run.success?).to eq(true)
|
306
|
+
end
|
307
|
+
end
|
291
308
|
end
|
data/spec/lib/flow_spec.rb
CHANGED
@@ -5,8 +5,6 @@ require 'rspec/rails'
|
|
5
5
|
# prevent Test::Unit's AutoRunner from executing during RSpec's rake task
|
6
6
|
Test::Unit.run = true if defined?(Test::Unit) && Test::Unit.respond_to?(:run=)
|
7
7
|
|
8
|
-
|
9
|
-
|
10
8
|
describe 'LightOperations::Flow', type: :controller do
|
11
9
|
RailsApp = Class.new(Rails::Application)
|
12
10
|
RailsApp.config.secret_key_base = '5308dcbbb7dea1b44e3d1d55ea7656f9'
|
@@ -67,7 +65,6 @@ describe 'LightOperations::Flow', type: :controller do
|
|
67
65
|
end
|
68
66
|
end
|
69
67
|
|
70
|
-
|
71
68
|
it '#render_create as success' do
|
72
69
|
post :create, correct: true
|
73
70
|
expect(response.body).to eq('Create OK')
|
@@ -94,7 +91,6 @@ describe 'LightOperations::Flow', type: :controller do
|
|
94
91
|
end
|
95
92
|
end
|
96
93
|
|
97
|
-
|
98
94
|
it '#view_create as success' do
|
99
95
|
post :create, correct: true
|
100
96
|
expect(response.body).to eq('Create OK')
|
@@ -118,7 +114,6 @@ describe 'LightOperations::Flow', type: :controller do
|
|
118
114
|
end
|
119
115
|
end
|
120
116
|
|
121
|
-
|
122
117
|
it '#render_create as success' do
|
123
118
|
post :create, correct: true
|
124
119
|
expect(response.body).to eq('Create OK')
|
@@ -147,7 +142,6 @@ describe 'LightOperations::Flow', type: :controller do
|
|
147
142
|
end
|
148
143
|
end
|
149
144
|
|
150
|
-
|
151
145
|
it '#render_view as success' do
|
152
146
|
post :create, correct: true
|
153
147
|
expect(response.body).to eq('Create OK')
|
@@ -186,7 +180,6 @@ describe 'LightOperations::Flow', type: :controller do
|
|
186
180
|
end
|
187
181
|
end
|
188
182
|
|
189
|
-
|
190
183
|
it '#render_view as success' do
|
191
184
|
post :update, correct: true, id: 1
|
192
185
|
expect(response.body).to eq('Update OK')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: light_operations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pawel Niemczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
208
208
|
version: '0'
|
209
209
|
requirements: []
|
210
210
|
rubyforge_project:
|
211
|
-
rubygems_version: 2.4.
|
211
|
+
rubygems_version: 2.4.8
|
212
212
|
signing_key:
|
213
213
|
specification_version: 4
|
214
214
|
summary: Light operations
|