light_operations 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +2 -1
- data/README.md +5 -0
- data/lib/light_operations/core.rb +2 -1
- data/lib/light_operations/crud_core.rb +124 -0
- data/lib/light_operations/version.rb +1 -1
- data/light_operations.gemspec +1 -1
- data/spec/lib/core_spec.rb +4 -4
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f75107bd444857e0c5b66bbdcf916088c4cc7e8
|
4
|
+
data.tar.gz: 9b69c0b871bb46dd6ecc7cfe059be02f7a1e6d42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14c25bcd15e1267126a3fe264fa49ac509b44750b1c069a38998c3e9b69d5004f01fcd24278f21dcee64c38315693a9eacb07509f5dba83d693dd7521cc8c24a
|
7
|
+
data.tar.gz: ce1aa5e31c123dc2cb836feddc310a4a1baca9ec6f31470760eee3314cafae64aead6a3f32e2c916344133437463ba676be24440416eaabc9c10d4cbcd291a31
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# LightOperations
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/light_operations.svg)](http://badge.fury.io/rb/light_operations)
|
4
|
+
[![Build Status](https://travis-ci.org/pniemczyk/light_operations.svg)](https://travis-ci.org/pniemczyk/light_operations)
|
5
|
+
[![Dependency Status](https://gemnasium.com/pniemczyk/light_operations.svg)](https://gemnasium.com/pniemczyk/light_operations)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/pniemczyk/light_operations/badges/gpa.svg)](https://codeclimate.com/github/pniemczyk/light_operations)
|
7
|
+
|
3
8
|
When you want have slim controllers or some logic with several operations
|
4
9
|
this gem could help you to have nice separated and clan code. CAN HELP YOU! :D
|
5
10
|
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module CrudOperations
|
4
|
+
class Core
|
5
|
+
include ::ActiveSupport::Rescuable
|
6
|
+
MissingDependency = Class.new(StandardError)
|
7
|
+
|
8
|
+
attr_reader :dependencies, :bind_object, :subject
|
9
|
+
|
10
|
+
def initialize(dependencies = {})
|
11
|
+
@dependencies = dependencies
|
12
|
+
end
|
13
|
+
|
14
|
+
# do no.t override this method
|
15
|
+
def run(params = {})
|
16
|
+
clear_subject_with_errors!
|
17
|
+
@subject = execute(params)
|
18
|
+
execute_actions
|
19
|
+
self
|
20
|
+
rescue => exception
|
21
|
+
rescue_with_handler(exception) || raise
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def on_success(binded_method = nil, &block)
|
26
|
+
actions[:success] = binded_method || block
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def on_fail(binded_method = nil, &block)
|
31
|
+
actions[:fail] = binded_method || block
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
def on(actions_with_responses = {})
|
36
|
+
actions_assign(actions_with_responses, :success, :fail)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear!
|
41
|
+
clear_actions!
|
42
|
+
unbind!
|
43
|
+
clear_subject_with_errors!
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def unbind!
|
48
|
+
@bind_object = nil
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def clear_subject_with_errors!
|
53
|
+
@subject, @fail_errors, @errors = nil, nil, nil
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear_actions!
|
58
|
+
@actions = {}
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def bind_with(bind_object)
|
63
|
+
@bind_object = bind_object
|
64
|
+
self
|
65
|
+
end
|
66
|
+
|
67
|
+
def errors
|
68
|
+
@errors ||= fail_errors || (subject.respond_to?(:errors) ? subject.errors : [])
|
69
|
+
end
|
70
|
+
|
71
|
+
def fail?
|
72
|
+
!success?
|
73
|
+
end
|
74
|
+
|
75
|
+
def success?
|
76
|
+
errors.respond_to?(:empty?) ? errors.empty? : !errors
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
attr_reader :fail_errors
|
82
|
+
|
83
|
+
def actions_assign(hash, *keys)
|
84
|
+
keys.each { |key| actions[key] = hash[key] if hash.key?(key) }
|
85
|
+
end
|
86
|
+
|
87
|
+
def execute_actions
|
88
|
+
success? ? execute_success_action : execute_fail_action
|
89
|
+
end
|
90
|
+
|
91
|
+
def execute_success_action
|
92
|
+
return unless actions.key?(:success)
|
93
|
+
action = actions[:success]
|
94
|
+
bind_object.send(action, subject) if action.is_a?(Symbol) && bind_object
|
95
|
+
action.call(subject) if action.is_a?(Proc)
|
96
|
+
end
|
97
|
+
|
98
|
+
def execute_fail_action
|
99
|
+
return unless actions.key?(:fail)
|
100
|
+
action = actions[:fail]
|
101
|
+
bind_object.send(action, subject, errors) if action.is_a?(Symbol) && bind_object
|
102
|
+
action.call(subject, errors) if action.is_a?(Proc)
|
103
|
+
end
|
104
|
+
|
105
|
+
def fail!(errors = [])
|
106
|
+
@errors = nil
|
107
|
+
@fail_errors = errors
|
108
|
+
end
|
109
|
+
|
110
|
+
def actions
|
111
|
+
@actions ||= {}
|
112
|
+
end
|
113
|
+
|
114
|
+
def execute(_params = {})
|
115
|
+
fail 'Not implemented yet'
|
116
|
+
end
|
117
|
+
|
118
|
+
def dependency(name)
|
119
|
+
dependencies.fetch(name)
|
120
|
+
rescue KeyError => e
|
121
|
+
raise MissingDependency, e.message
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/light_operations.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'activesupport', '>= 3.0.2'
|
22
|
-
spec.add_development_dependency 'bundler', '
|
22
|
+
spec.add_development_dependency 'bundler', '>= 1.6'
|
23
23
|
spec.add_development_dependency 'rake', '~> 10.4'
|
24
24
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
25
|
spec.add_development_dependency 'guard', '~> 2.12'
|
data/spec/lib/core_spec.rb
CHANGED
@@ -39,7 +39,7 @@ describe LightOperations::Core do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
before { subject.on(success:
|
42
|
+
before { subject.on(success: lambda { |_subject| }) }
|
43
43
|
|
44
44
|
it 'is allowed when is initialized correctly' do
|
45
45
|
expect { subject.run }.not_to raise_error
|
@@ -58,7 +58,7 @@ describe LightOperations::Core do
|
|
58
58
|
context '.rescue_from specific error' do
|
59
59
|
TestError = Class.new(StandardError)
|
60
60
|
|
61
|
-
before { subject.on(success:
|
61
|
+
before { subject.on(success: lambda { |_subject| }) }
|
62
62
|
|
63
63
|
context 'by block' do
|
64
64
|
subject do
|
@@ -128,7 +128,7 @@ describe LightOperations::Core do
|
|
128
128
|
end
|
129
129
|
|
130
130
|
it 'when block is used' do
|
131
|
-
block_to_exec =
|
131
|
+
block_to_exec = lambda {}
|
132
132
|
expect(block_to_exec).not_to receive(:call)
|
133
133
|
subject.on_fail(&block_to_exec).run
|
134
134
|
end
|
@@ -164,7 +164,7 @@ describe LightOperations::Core do
|
|
164
164
|
end
|
165
165
|
|
166
166
|
it 'when block is used' do
|
167
|
-
block_to_exec =
|
167
|
+
block_to_exec = lambda {}
|
168
168
|
expect(block_to_exec).not_to receive(:call)
|
169
169
|
subject.on_success(&block_to_exec).run
|
170
170
|
end
|
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: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pawel Niemczyk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
33
|
+
version: '1.6'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
40
|
+
version: '1.6'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- Rakefile
|
155
155
|
- lib/light_operations.rb
|
156
156
|
- lib/light_operations/core.rb
|
157
|
+
- lib/light_operations/crud_core.rb
|
157
158
|
- lib/light_operations/version.rb
|
158
159
|
- light_operations.gemspec
|
159
160
|
- spec/lib/core_spec.rb
|
@@ -178,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
179
|
version: '0'
|
179
180
|
requirements: []
|
180
181
|
rubyforge_project:
|
181
|
-
rubygems_version: 2.4.
|
182
|
+
rubygems_version: 2.4.5
|
182
183
|
signing_key:
|
183
184
|
specification_version: 4
|
184
185
|
summary: Light operations
|