acfs 0.28.0.1.b249 → 0.28.0.1.b253
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +1 -0
- data/lib/acfs/model/query_methods.rb +20 -2
- data/lib/acfs/singleton_resource.rb +1 -0
- data/spec/acfs/model/query_methods_spec.rb +58 -27
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGQ1YTRhNGNiM2NiZjgyMGQ2ZWEwZjQwYmJjYTQ1OWNjZTY3Njk0Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDJjZmFmMDUzY2VmODJkMGRjNDczZDFkMWI4ZTFkMDM0NTViNzc5YQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MGI3YzY0ZDE3ODFhNzRjYWZiZDM1OTg0MTdiYjhhN2FkNjNmZTZlZTg0Y2M2
|
10
|
+
NjE5ODNhOWM1ODc5ZTc2OGRmMGY0YTYxOTAyZjM2OTg5ZTBlMzY4N2M5MzY0
|
11
|
+
NDUxZDIwNTJkMTE3ZjYyNjI3YjhkMWQ2MmFkNTEyZDFjOTFlNzg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YmRmNTIwYzg1MTBiM2E0NTYwYjIwNzk4OTY1ZjY4ZTQ1YWFiMDMxZWQ4ZTNk
|
14
|
+
YWJiOTY2ZGY3N2I4NmI3Y2NkNmE2ZjUxYTkwNGZlMzRmOGI3NWE2M2E2NDhi
|
15
|
+
YjBiNDg5OWIxMjZjZjdmYWUyY2EwYWVkNDBiNTY0MGQzYzdhYjQ=
|
data/README.md
CHANGED
@@ -130,6 +130,7 @@ Acfs.run # Will request `http://users.myapp.org/users?age=24`
|
|
130
130
|
|
131
131
|
@user # Contains the first user object returned by the index action
|
132
132
|
```
|
133
|
+
If no object can be found, `.find_by` will return `nil`. The optional callback will then be called with `nil` as parameter. Use `.find_by!` to raise an `Acfs::ResourceNotFound` exception if no object can be found. `.find_by!` will only invoke the optional callback if an object was successfully loaded.
|
133
134
|
|
134
135
|
Acfs has basic update support using `PUT` requests:
|
135
136
|
|
@@ -88,11 +88,11 @@ module Acfs::Model
|
|
88
88
|
|
89
89
|
# @api public
|
90
90
|
#
|
91
|
-
# Try to load first resource.
|
91
|
+
# Try to load first resource. Return nil if no object can be loaded.
|
92
92
|
#
|
93
93
|
# @param [ Hash ] params Request parameters that will be send to remote service.
|
94
94
|
#
|
95
|
-
# @yield [ resource ] Callback block to be executed after resource was fetched
|
95
|
+
# @yield [ resource ] Callback block to be executed after resource was fetched (even if nil).
|
96
96
|
# @yieldparam resource [ self ] Fetched resource, nil if empty list is returned
|
97
97
|
#
|
98
98
|
# @return [ self ] Resource object, nil if empty list is returned
|
@@ -112,6 +112,24 @@ module Acfs::Model
|
|
112
112
|
model
|
113
113
|
end
|
114
114
|
|
115
|
+
# @api public
|
116
|
+
#
|
117
|
+
# Try to load first resource. Raise Acfs::ResourceNotFound exception if no object can be loaded.
|
118
|
+
#
|
119
|
+
# @param [ Hash ] params Request parameters that will be send to remote service.
|
120
|
+
#
|
121
|
+
# @yield [ resource ] Callback block to be executed after resource was fetched successfully.
|
122
|
+
# @yieldparam resource [ self ] Fetched resource, nil if empty list is returned
|
123
|
+
#
|
124
|
+
# @return [ self ] Resource object, nil if empty list is returned
|
125
|
+
#
|
126
|
+
def find_by!(params, &block)
|
127
|
+
find_by params do |m|
|
128
|
+
raise Acfs::ResourceNotFound.new message: "Recieved erronious response: no `#{self.name}` with params #{params.to_s} found" if m.nil?
|
129
|
+
block.call m unless block.nil?
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
115
133
|
# TODO: Replace delegator with promise or future for the long run.
|
116
134
|
class ResourceDelegator < SimpleDelegator
|
117
135
|
delegate :class, :is_a?, :kind_of?, :nil?, to: :__getobj__
|
@@ -163,37 +163,30 @@ describe Acfs::Model::QueryMethods do
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
|
166
|
+
shared_examples 'find_by' do
|
167
167
|
context 'standard resource' do
|
168
|
-
let(:model){ MyUser }
|
169
|
-
|
170
|
-
subject
|
171
|
-
user = model.find_by age: 24
|
172
|
-
Acfs.run
|
173
|
-
user
|
174
|
-
end
|
168
|
+
let(:model) { MyUser }
|
169
|
+
let!(:user) { model.send described_method, age: 24 }
|
170
|
+
subject { Acfs.run; user }
|
175
171
|
|
176
172
|
context 'return value' do
|
177
|
-
subject {
|
173
|
+
subject { user }
|
178
174
|
|
179
175
|
it { should be_a MyUser }
|
180
|
-
|
181
176
|
it { should_not be_loaded }
|
182
177
|
end
|
183
178
|
|
184
|
-
|
185
|
-
stub_request(:get, 'http://users.example.org/users?age=24').to_return response([])
|
179
|
+
context 'with params' do
|
180
|
+
let!(:request) { stub_request(:get, 'http://users.example.org/users?age=24').to_return response([{id: 1, name: 'Mike', age: 24}]) }
|
186
181
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
182
|
+
it 'should include params in URI to index action' do
|
183
|
+
subject
|
184
|
+
expect(request).to have_been_requested
|
185
|
+
end
|
191
186
|
end
|
192
187
|
|
193
188
|
context 'with non-empty response' do
|
194
|
-
before
|
195
|
-
stub_request(:get, 'http://users.example.org/users?age=24').to_return response([{ id: 1, name: 'Mike', age: 24 }, { id: 4, type: 'Maria', age: 24 }, { id: 7, type: 'James', age: 24 }])
|
196
|
-
end
|
189
|
+
before { stub_request(:get, 'http://users.example.org/users?age=24').to_return response([{id: 1, name: 'Mike', age: 24}, {id: 4, type: 'Maria', age: 24}, {id: 7, type: 'James', age: 24}]) }
|
197
190
|
|
198
191
|
it 'should invoke callback after model is loaded' do
|
199
192
|
proc = Proc.new { }
|
@@ -204,7 +197,7 @@ describe Acfs::Model::QueryMethods do
|
|
204
197
|
expect(user).to be_loaded
|
205
198
|
end
|
206
199
|
|
207
|
-
@user = model.
|
200
|
+
@user = model.send described_method, age: 24, &proc
|
208
201
|
Acfs.run
|
209
202
|
end
|
210
203
|
|
@@ -212,11 +205,28 @@ describe Acfs::Model::QueryMethods do
|
|
212
205
|
expect(subject).to be_a MyUser
|
213
206
|
end
|
214
207
|
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'singleton resource' do
|
211
|
+
let(:model) { Single }
|
212
|
+
|
213
|
+
it '.find_by should not be defined' do
|
214
|
+
expect{ model.find_by }.to raise_error NoMethodError
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe '.find_by' do
|
220
|
+
let(:described_method) { :find_by }
|
221
|
+
it_behaves_like 'find_by'
|
222
|
+
|
223
|
+
context 'standard resource' do
|
224
|
+
let(:model){ MyUser }
|
225
|
+
let!(:user) { model.send described_method, age: 24 }
|
226
|
+
subject { Acfs.run; user }
|
215
227
|
|
216
228
|
context 'with empty response' do
|
217
|
-
before
|
218
|
-
stub_request(:get, 'http://users.example.org/users?age=24').to_return response([])
|
219
|
-
end
|
229
|
+
before { stub_request(:get, 'http://users.example.org/users?age=24').to_return response([]) }
|
220
230
|
|
221
231
|
it { should be_nil }
|
222
232
|
|
@@ -233,12 +243,33 @@ describe Acfs::Model::QueryMethods do
|
|
233
243
|
end
|
234
244
|
end
|
235
245
|
end
|
246
|
+
end
|
236
247
|
|
237
|
-
|
238
|
-
|
248
|
+
describe '.find_by!' do
|
249
|
+
let(:described_method) { :find_by! }
|
250
|
+
it_behaves_like 'find_by'
|
239
251
|
|
240
|
-
|
241
|
-
|
252
|
+
context 'standard resource' do
|
253
|
+
let(:model){ MyUser }
|
254
|
+
let!(:user) { model.send described_method, age: 24 }
|
255
|
+
subject { Acfs.run; user }
|
256
|
+
|
257
|
+
context 'with empty response' do
|
258
|
+
before { stub_request(:get, 'http://users.example.org/users?age=24').to_return response([]) }
|
259
|
+
|
260
|
+
it 'should raise an ResourceNotFound error' do
|
261
|
+
model.find_by! age: 24
|
262
|
+
expect{ Acfs.run }.to raise_error Acfs::ResourceNotFound
|
263
|
+
end
|
264
|
+
|
265
|
+
it 'should not invoke callback after model could not be loaded' do
|
266
|
+
proc = Proc.new { }
|
267
|
+
|
268
|
+
expect(proc).not_to receive(:call)
|
269
|
+
|
270
|
+
model.find_by! age: 24, &proc
|
271
|
+
expect{ Acfs.run }.to raise_error
|
272
|
+
end
|
242
273
|
end
|
243
274
|
end
|
244
275
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acfs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.28.0.1.
|
4
|
+
version: 0.28.0.1.b253
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Graichen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|