resource_manager 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca47a34574bff820fa5de5fffafd1922250db9fd
4
- data.tar.gz: 41ed5ad6d2252dc082001d89898b2296f2619e15
3
+ metadata.gz: 889d565e3adc71321786d04a1ee9ab5adb4a0f6d
4
+ data.tar.gz: f8aeb3332f592fea72114099f6519be4622b0626
5
5
  SHA512:
6
- metadata.gz: 7bd257353d994a808fec5bc9c3cc4ff31e803bff31c6c9a4d590d868f3bd03bee1b35aa1dbbc8f6cf80e74bf75659a1bf833d5176e45738a44ba20bcf796e9b6
7
- data.tar.gz: 6599659bdf811d9b6442e8aac1cd1cf3546f92873058356a9a16d5ec62ef7e6278452de01ceb40d8c74beb2a7c8ce502a5e89755c2512be89dac50ffeef4ef35
6
+ metadata.gz: e0a24a5aad021e72791cfdd6a7f0318fe8666e6811ef116c45f85c73b33f9ac4b1fee2f78ee948af7a4246aa45df2a3fc06a4253cc43f9f4a9b97d05b948ad9f
7
+ data.tar.gz: 795ef835e288504590ad1508c4930d9ca84d28372a1b4a99ca88295ecec4f4320162ab3138215cdd38e7d331d257965fe6b55d0e997793f057d4e79b08764554
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.1.1
8
+ - 2.1.2
9
+ - 2.1.3
10
+ - jruby-19mode
11
+ script: bundle exec rspec
12
+ cache: bundler
@@ -1,9 +1,7 @@
1
1
  require 'resource_manager/version'
2
- require 'resource_manager/transactionable'
3
2
 
4
3
  module ResourceManager
5
4
  class Base
6
- include ResourceManager::Transactionable
7
5
 
8
6
  class_attribute :model
9
7
  class_attribute :before_create_callback, :after_create_callback, :before_update_callback, :after_update_callback, :before_destroy_callback, :after_destroy_callback
@@ -35,55 +33,59 @@ module ResourceManager
35
33
  end
36
34
  end
37
35
 
38
- def create(params)
39
- transaction do
40
- run_callback(:before_create, params)
41
-
42
- result = if self.create_action.present?
43
- instance_exec(params, &self.create_action)
44
- else
45
- item = model.new(params)
46
- saved = item.save
47
- yield(item, saved) if block_given?
48
- end
49
-
50
- run_callback(:after_create, item) if saved
51
- result
36
+ ['', '!'].each do |name|
37
+ define_method "create#{name}" do |params|
38
+ transaction do
39
+ run_callback(:before_create, params)
40
+
41
+ if self.create_action.present?
42
+ instance_exec(params, &self.create_action)
43
+ else
44
+ item = model.new(params)
45
+ saved = item.send("save#{name}")
46
+ yield(item, saved) if block_given?
47
+ item
48
+ end
49
+
50
+ run_callback(:after_create, item, params) if saved
51
+ item
52
+ end
52
53
  end
53
- end
54
54
 
55
- def update(item_or_id, params)
56
- transaction do
57
- item = item(item_or_id)
58
- run_callback(:before_update, item, params)
59
-
60
- result = if self.update_action.present?
61
- instance_exec(item, params, &self.update_action)
62
- else
63
- item.assign_attributes(params)
64
- changed = item.changed
65
- saved = item.save
66
- yield(item, saved) if block_given?
67
- end
68
-
69
- run_callback(:after_update, item, params, changed) if saved
70
- result
55
+ define_method "update#{name}" do |item_or_id, params|
56
+ transaction do
57
+ item = item(item_or_id)
58
+ run_callback(:before_update, item, params)
59
+
60
+ if self.update_action.present?
61
+ instance_exec(item, params, &self.update_action)
62
+ else
63
+ item.assign_attributes(params)
64
+ changed = item.changed
65
+ saved = item.send("save#{name}")
66
+ yield(item, saved) if block_given?
67
+ item
68
+ end
69
+
70
+ run_callback(:after_update, item, params, changed) if saved
71
+ item
72
+ end
71
73
  end
72
- end
73
74
 
74
- def destroy(item_or_id)
75
- transaction do
76
- item = item(item_or_id)
77
- run_callback(:before_destroy, item)
75
+ define_method "destroy#{name}" do |item_or_id|
76
+ transaction do
77
+ item = item(item_or_id)
78
+ run_callback(:before_destroy, item)
78
79
 
79
- if self.destroy_action.present?
80
- instance_exec(item, &self.destroy_action)
81
- else
82
- item.destroy
83
- end
80
+ if self.destroy_action.present?
81
+ instance_exec(item, &self.destroy_action)
82
+ else
83
+ item.send("destroy#{name}")
84
+ end
84
85
 
85
- run_callback(:after_destroy, item)
86
- nil
86
+ run_callback(:after_destroy, item)
87
+ item
88
+ end
87
89
  end
88
90
  end
89
91
 
@@ -100,5 +102,9 @@ module ResourceManager
100
102
  model.find(item_or_id)
101
103
  end
102
104
  end
105
+
106
+ def transaction(&block)
107
+ ActiveRecord::Base.transaction &block
108
+ end
103
109
  end
104
110
  end
@@ -1,3 +1,3 @@
1
1
  module ResourceManager
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -19,4 +19,10 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency 'bundler', '~> 1.7'
21
21
  spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'coveralls', '~> 0.7'
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
+ spec.add_development_dependency 'simplecov-html', '~> 0.8'
25
+ spec.add_development_dependency 'mocha', '~> 1.1'
26
+
27
+ spec.add_runtime_dependency 'activesupport', '~> 4.1.6'
22
28
  end
@@ -0,0 +1,273 @@
1
+ require 'spec_helper'
2
+
3
+ describe ResourceManager::Base do
4
+
5
+ context 'with model as class' do
6
+
7
+ let!(:manager) {
8
+ Class.new(ResourceManager::Base) do
9
+ model Model
10
+ end.new
11
+ }
12
+
13
+ context 'with callbacks' do
14
+ describe '#create' do
15
+ it 'calls before create callback' do
16
+ result_params = nil
17
+ callback = Proc.new{ |params| result_params = params}
18
+
19
+ manager.class.before_create(&callback)
20
+ manager.create(test: 1)
21
+ expect(result_params).to eq({test: 1})
22
+ end
23
+
24
+ it 'calls after create callback' do
25
+ result_params = nil
26
+ result_item = nil
27
+ callback = Proc.new{ |item, params| result_item = item; result_params = params}
28
+ manager.class.after_create(&callback)
29
+
30
+ manager.create({test: 1})
31
+
32
+ expect(result_params).to eq({test: 1})
33
+ expect(result_item).to be_a_kind_of(Model)
34
+ end
35
+
36
+ it 'returns Model instance' do
37
+ expect(manager.create({test: 1})).to be_a_kind_of(Model)
38
+ end
39
+
40
+ it 'yields block' do
41
+ manager.create({test: 1}) do |item, result|
42
+ expect(item).to be_a_kind_of(Model)
43
+ expect(result).to be_truthy
44
+ end
45
+ end
46
+
47
+ context 'with failed saving' do
48
+ before do
49
+ Model.any_instance.stubs(:save).returns(false)
50
+ end
51
+
52
+ it 'does not call after_create' do
53
+ result_params = nil
54
+ result_item = nil
55
+ callback = Proc.new{ |item, params| result_item = item; result_params = params}
56
+ manager.class.after_create(&callback)
57
+
58
+ manager.create({test: 1})
59
+
60
+ expect(result_params).to be_nil
61
+ expect(result_item).to be_nil
62
+ end
63
+
64
+ it 'yields block' do
65
+ manager.create({test: 1}) do |item, result|
66
+ expect(item).to be_a_kind_of(Model)
67
+ expect(result).to be_falsey
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#create!' do
74
+ it 'returns Model instance' do
75
+ expect(manager.create!({test: 1})).to be_a_kind_of(Model)
76
+ end
77
+
78
+ context 'with failed saving' do
79
+ before do
80
+ Model.any_instance.stubs(:save!).raises(StandardError.new)
81
+ end
82
+
83
+ it 'returns Model instance' do
84
+ expect{manager.create!({test: 1})}.to raise_error(StandardError)
85
+ end
86
+
87
+ end
88
+ end
89
+
90
+ describe '#update' do
91
+ it 'calls before update callback' do
92
+ result_params = nil
93
+ result_item = nil
94
+ callback = Proc.new{ |item, params| result_item = item; result_params = params}
95
+ manager.class.before_update(&callback)
96
+
97
+ manager.update(1, test: 1)
98
+
99
+ expect(result_params).to eq({test: 1})
100
+ expect(result_item).to be_a_kind_of(Model)
101
+ end
102
+
103
+
104
+ it 'calls after update callback' do
105
+ result_params = nil
106
+ result_item = nil
107
+ result_changed = nil
108
+ callback = Proc.new{ |item, params, changed| result_item = item; result_params = params; result_changed = changed}
109
+ manager.class.after_update(&callback)
110
+
111
+ manager.update(1, test: 1)
112
+
113
+ expect(result_params).to eq({test: 1})
114
+ expect(result_item).to be_a_kind_of(Model)
115
+ expect(result_changed).to be_a_kind_of(Hash)
116
+ end
117
+
118
+ it 'returns Model instance' do
119
+ expect(manager.update(1, {test: 1})).to be_a_kind_of(Model)
120
+ end
121
+
122
+ it 'yields block' do
123
+ manager.update(1, {test: 1}) do |item, result|
124
+ expect(item).to be_a_kind_of(Model)
125
+ expect(result).to be_truthy
126
+ end
127
+ end
128
+
129
+ context 'with failed saving' do
130
+ before do
131
+ Model.any_instance.stubs(:save).returns(false)
132
+ end
133
+
134
+ it 'does not call after_update' do
135
+ result_params = nil
136
+ result_item = nil
137
+ result_changed= nil
138
+ callback = Proc.new{ |item, params, changed| result_item = item; result_params = params; result_changed = changed}
139
+ manager.class.after_update(&callback)
140
+
141
+ manager.update(1, {test: 1})
142
+
143
+ expect(result_params).to be_nil
144
+ expect(result_item).to be_nil
145
+ expect(result_changed).to be_nil
146
+ end
147
+
148
+ it 'yields block' do
149
+ manager.update(1, {test: 1}) do |item, result|
150
+ expect(item).to be_a_kind_of(Model)
151
+ expect(result).to be_falsey
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ describe '#update!' do
158
+ it 'returns Model instance' do
159
+ expect(manager.update!(1, {test: 1})).to be_a_kind_of(Model)
160
+ end
161
+
162
+ context 'with failed saving' do
163
+ before do
164
+ Model.any_instance.stubs(:save!).raises(StandardError.new)
165
+ end
166
+
167
+ it 'returns Model instance' do
168
+ expect{manager.update!(1, {test: 1})}.to raise_error(StandardError)
169
+ end
170
+
171
+ end
172
+ end
173
+
174
+
175
+ describe '#destroy' do
176
+ it 'calls before destroy callback' do
177
+ result_item = nil
178
+ callback = Proc.new{ |item| result_item = item}
179
+ manager.class.before_destroy(&callback)
180
+ manager.destroy(1)
181
+
182
+ expect(result_item).to be_a_kind_of(Model)
183
+ end
184
+
185
+
186
+ it 'calls after destroy callback' do
187
+ result_item = nil
188
+ callback = Proc.new{ |item| result_item = item}
189
+ manager.class.after_destroy(&callback)
190
+ manager.destroy(1)
191
+
192
+ expect(result_item).to be_a_kind_of(Model)
193
+ end
194
+
195
+ it 'returns Model instance' do
196
+ expect(manager.destroy(Model.new)).to be_a_kind_of(Model)
197
+ end
198
+
199
+
200
+ end
201
+
202
+
203
+ describe '#destroy!' do
204
+ it 'returns Model instance' do
205
+ expect(manager.destroy!(1)).to be_a_kind_of(Model)
206
+ end
207
+
208
+ context 'with failed saving' do
209
+ before do
210
+ Model.any_instance.stubs(:destroy!).raises(StandardError.new)
211
+ end
212
+
213
+ it 'returns Model instance' do
214
+ expect{manager.destroy!(1)}.to raise_error(StandardError)
215
+ end
216
+
217
+ end
218
+ end
219
+
220
+ end
221
+
222
+ context 'with actions' do
223
+ describe '#create' do
224
+ it 'calls before create callback' do
225
+ result_params = nil
226
+ callback = Proc.new{ |params| result_params = params}
227
+
228
+ manager.class.create(&callback)
229
+ manager.create(test: 1)
230
+ expect(result_params).to eq({test: 1})
231
+ end
232
+ end
233
+
234
+ describe '#update' do
235
+ it 'calls before create callback' do
236
+ result_params = nil
237
+ result_item = nil
238
+ callback = Proc.new{ |item, params| result_item = item; result_params = params}
239
+
240
+ manager.class.update(&callback)
241
+ manager.update(1, test: 1)
242
+ expect(result_params).to eq({test: 1})
243
+ expect(result_item).to be_a_kind_of(Model)
244
+ end
245
+ end
246
+
247
+ describe '#destroy' do
248
+ it 'calls before create callback' do
249
+ result_item = nil
250
+ callback = Proc.new{ |item| result_item = item}
251
+
252
+ manager.class.destroy(&callback)
253
+ manager.destroy(1)
254
+ expect(result_item).to be_a_kind_of(Model)
255
+ end
256
+ end
257
+ end
258
+ end
259
+
260
+ context 'with model as lambda' do
261
+ let!(:manager) {
262
+ Class.new(ResourceManager::Base) do
263
+ model ->{ Model }
264
+ end.new
265
+ }
266
+
267
+ describe '#create' do
268
+ it 'Model instance' do
269
+ expect(manager.create({test: 1})).to be_a_kind_of(Model)
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,22 @@
1
+ #require 'coveralls'
2
+ require 'simplecov'
3
+
4
+ #Coveralls.wear!
5
+ require 'support/mocks/model'
6
+ require 'support/mocks/active_record/base'
7
+
8
+ require 'active_support/core_ext/class/attribute'
9
+ require 'active_support/core_ext/object/blank'
10
+ require 'mocha'
11
+
12
+ SimpleCov.start
13
+
14
+
15
+ $LOAD_PATH << '../lib'
16
+
17
+ require 'resource_manager'
18
+
19
+ RSpec.configure do |config|
20
+ config.order = 'random'
21
+ config.mock_with :mocha
22
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ class Base
3
+ def self.transaction
4
+ yield
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ class Model
2
+ class << self
3
+ def find(_)
4
+ Model.new
5
+ end
6
+ end
7
+
8
+ def initialize(_ = {})
9
+
10
+ end
11
+
12
+ def destroy
13
+ end
14
+
15
+ def destroy!
16
+
17
+ end
18
+
19
+ def save
20
+ true
21
+ end
22
+
23
+ def save!
24
+ true
25
+ end
26
+
27
+ def assign_attributes(_)
28
+
29
+ end
30
+
31
+ def changed
32
+ {}
33
+ end
34
+ end
metadata CHANGED
@@ -1,43 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resource_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Sinyavsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-01 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
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
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov-html
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 4.1.6
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 4.1.6
41
111
  description: Base for service objects that encapsulates model actions and callbacks
42
112
  email:
43
113
  - zhulik.gleb@gmail.com
@@ -45,16 +115,21 @@ executables: []
45
115
  extensions: []
46
116
  extra_rdoc_files: []
47
117
  files:
48
- - ".gitignore"
118
+ - .coveralls.yml
119
+ - .gitignore
120
+ - .travis.yml
49
121
  - Gemfile
50
122
  - LICENSE
51
123
  - LICENSE.txt
52
124
  - README.md
53
125
  - Rakefile
54
126
  - lib/resource_manager.rb
55
- - lib/resource_manager/transactionable.rb
56
127
  - lib/resource_manager/version.rb
57
128
  - resource_manager.gemspec
129
+ - spec/lib/resource_manager_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/mocks/active_record/base.rb
132
+ - spec/support/mocks/model.rb
58
133
  homepage: https://github.com/zhulik/resource_manager
59
134
  licenses:
60
135
  - MIT
@@ -65,18 +140,22 @@ require_paths:
65
140
  - lib
66
141
  required_ruby_version: !ruby/object:Gem::Requirement
67
142
  requirements:
68
- - - ">="
143
+ - - '>='
69
144
  - !ruby/object:Gem::Version
70
145
  version: '0'
71
146
  required_rubygems_version: !ruby/object:Gem::Requirement
72
147
  requirements:
73
- - - ">="
148
+ - - '>='
74
149
  - !ruby/object:Gem::Version
75
150
  version: '0'
76
151
  requirements: []
77
152
  rubyforge_project:
78
- rubygems_version: 2.2.2
153
+ rubygems_version: 2.0.14
79
154
  signing_key:
80
155
  specification_version: 4
81
156
  summary: Base for service objects that encapsulates model actions and callbacks
82
- test_files: []
157
+ test_files:
158
+ - spec/lib/resource_manager_spec.rb
159
+ - spec/spec_helper.rb
160
+ - spec/support/mocks/active_record/base.rb
161
+ - spec/support/mocks/model.rb
@@ -1,7 +0,0 @@
1
- module ResourceManager
2
- module Transactionable
3
- def transaction(&block)
4
- ActiveRecord::Base.transaction &block
5
- end
6
- end
7
- end