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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzM4MTNmYTA0MWFkMjAzMzA0Mjg4OGNiYjE0MzlkYTlhMDc5ZWY1Yw==
4
+ ZGQ1YTRhNGNiM2NiZjgyMGQ2ZWEwZjQwYmJjYTQ1OWNjZTY3Njk0Mg==
5
5
  data.tar.gz: !binary |-
6
- YWZkOTQ4N2RiNDNkMmYwMjUwYmQxMmFlNTViNTM1NjQ4YjUwYWViMw==
6
+ NDJjZmFmMDUzY2VmODJkMGRjNDczZDFkMWI4ZTFkMDM0NTViNzc5YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDg0ZjNhMTYxZDM0NDhmNjQ5NTJhMjBiNTYwMGNjOGFiYThmZjIxMzQ1NDVh
10
- ODBmY2VhNWYzNmNjYjdhZDkzODk3OTdmYzJhNWE0YjQyNjVmNzU1YTJkNThh
11
- ZGFjMzY2YWJmOGI4YmQ1Mzg4OWY0NDhkMjlkOTM1M2NmN2I1ZmE=
9
+ MGI3YzY0ZDE3ODFhNzRjYWZiZDM1OTg0MTdiYjhhN2FkNjNmZTZlZTg0Y2M2
10
+ NjE5ODNhOWM1ODc5ZTc2OGRmMGY0YTYxOTAyZjM2OTg5ZTBlMzY4N2M5MzY0
11
+ NDUxZDIwNTJkMTE3ZjYyNjI3YjhkMWQ2MmFkNTEyZDFjOTFlNzg=
12
12
  data.tar.gz: !binary |-
13
- ZDRhZmRjYTU3MjRlMmEwMDM5MWMzY2ViMGM2NjI0MjBlYmVjODFiNjk1ZTA3
14
- NWQxMWZiZmZkMTk3OGYyMTIyY2NiNGM5ZjRhN2I2MTc3NGRkOWZkMjdhYTgz
15
- ZmQyYjdjYjJkYTdjOTU4MDRmNWE3MTc0NjdhM2FmMjg2M2U3NDM=
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 successfully.
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__
@@ -89,6 +89,7 @@ module Acfs
89
89
  undef_method :all
90
90
  undef_method :where
91
91
  undef_method :find_by
92
+ undef_method :find_by!
92
93
  end
93
94
  end
94
95
  end
@@ -163,37 +163,30 @@ describe Acfs::Model::QueryMethods do
163
163
  end
164
164
  end
165
165
 
166
- describe '.find_by' do
166
+ shared_examples 'find_by' do
167
167
  context 'standard resource' do
168
- let(:model){ MyUser }
169
-
170
- subject do
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 { model.find_by age: 24 }
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
- it 'should query the index action with params set' do
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
- model.find_by age: 24
188
- Acfs.run
189
-
190
- assert_requested :get, 'http://users.example.org/users?age=24'
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 do
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.find_by age: 24, &proc
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 do
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
- context 'singleton resource' do
238
- let(:model) { Single }
248
+ describe '.find_by!' do
249
+ let(:described_method) { :find_by! }
250
+ it_behaves_like 'find_by'
239
251
 
240
- it '.find_by should not be defined' do
241
- expect{ model.find_by }.to raise_error NoMethodError
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.b249
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-01 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport