acfs 0.27.0.1.b244 → 0.27.0.1.b248

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
- MzQ4NGI5OTIwYTVkYTdiNGUyNGMzYzlmNDBiMzQ4YmNhYmIzZmFmMA==
4
+ ZTgwOTRmNjQ2MGNjY2M4MjBmYTBlMmVmOGIyYTAwNWRhMTlkNzFmOQ==
5
5
  data.tar.gz: !binary |-
6
- MTNlZmVmMGExOTI4MzcyNjA3ODZhYWI3MDNkODkwODk5MjNlYzNlNA==
6
+ YjBkZTQ0ZWMzY2Q3OWQ4N2U1MjczZTdkYjhkMmI3NGMwMmExNWQ3ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjVlOWNhMDE2MGFkZTcxODM2YWU4NWJlMDMyNjJmZmZmNzhkNjM5ZWUyMWI5
10
- OGY2MWMzMDJiNTA3OWVlZGRmN2I0OTZhNmMxNDAzZGNjYTFiODhhODQxOGY1
11
- ZWEwMjhmMzc1ZWIzOTdhODlhMjNiZjIwZDc3ZDM5YWZlNTA2ZGU=
9
+ ZWQ1NTgwOTE0MTMxY2RkN2ExMjEzN2M0MmUzZjkwODQ4MWMwODY0NzkyMDBm
10
+ NmNiZTNhZDRkOGJiM2Q1OGFjZGJlMWQxMGFkODA2MTg5NTg4YTQwNThhNTEx
11
+ Yjc5YTNlMzBlOWFlZDFjNzU3NDQxZDU2OWRlYTg3MTI5ZmExMzg=
12
12
  data.tar.gz: !binary |-
13
- MmI5MGZlYWJlZDg4NzBlYjEyYzk3Nzg4ZGM0YjlmMTRkZjRjYWZiODY4NGNm
14
- YmFhMWJiOTMzM2IxNmY3MGFlNzUxNmFkMTczNzQ5ZjgwYmY3YzgyNjA4YzMx
15
- ZjVlYzEzYTc4MThmMWIyMDM2NzU3OWRiNmMyODkzYTc2OGZiNGU=
13
+ NDIwM2ZkYWZkNTZiZjJkNjVjZmY4NTZjN2VkYTI3NTY1YzhlYWZmYmY0YzI3
14
+ NjYwYzliNTgwMDM4NGIxZjhmMmJlMDQzYzJjNmIwZmVlN2I4ZWFmMWRkN2My
15
+ MzY2Y2IwMGQwM2Q4NDc5YmQ0OTYzYjg1Y2VjOWJjMmQwYTcyZjI=
data/README.md CHANGED
@@ -122,6 +122,15 @@ Acfs.run # This call will fire all request as parallel as possible.
122
122
  @friends[0].name # => "Miraculix"
123
123
  ```
124
124
 
125
+ Use `.find_by` to get first element only. `.find_by` will call the `index`-Action and return the first resource. Optionally passed params will be sent as `GET` parameters and can be used for filtering in the service's controller.
126
+ ```ruby
127
+ @user = User.find_by age: 24
128
+
129
+ Acfs.run # Will request `http://users.myapp.org/users?age=24`
130
+
131
+ @user # Contains the first user object returned by the index action
132
+ ```
133
+
125
134
  Acfs has basic update support using `PUT` requests:
126
135
 
127
136
  ```ruby
@@ -86,9 +86,35 @@ module Acfs::Model
86
86
  end
87
87
  alias :where :all
88
88
 
89
+ # @api public
90
+ #
91
+ # Try to load first resource.
92
+ #
93
+ # @param [ Hash ] params Request parameters that will be send to remote service.
94
+ #
95
+ # @yield [ resource ] Callback block to be executed after resource was fetched successfully.
96
+ # @yieldparam resource [ self ] Fetched resource, nil if empty list is returned
97
+ #
98
+ # @return [ self ] Resource object, nil if empty list is returned
99
+ #
100
+ def find_by(params, &block)
101
+ model = ResourceDelegator.new self.new
102
+
103
+ operation :list, params: params do |data|
104
+ unless data.empty?
105
+ model.__setobj__ create_resource data.first, origin: model.__getobj__
106
+ else
107
+ model.__setobj__ nil
108
+ end
109
+ block.call model unless block.nil?
110
+ end
111
+
112
+ model
113
+ end
114
+
89
115
  # TODO: Replace delegator with promise or future for the long run.
90
116
  class ResourceDelegator < SimpleDelegator
91
- delegate :class, :is_a?, :kind_of?, to: :__getobj__
117
+ delegate :class, :is_a?, :kind_of?, :nil?, to: :__getobj__
92
118
  end
93
119
 
94
120
  private
@@ -84,9 +84,11 @@ module Acfs
84
84
  # Undefined, raises NoMethodError.
85
85
  # A singleton always only returns one object, therefore the
86
86
  # methods :all and :where are not defined.
87
+ # :find_by is not defined on singletons, use :find instead
87
88
  #
88
89
  undef_method :all
89
90
  undef_method :where
91
+ undef_method :find_by
90
92
  end
91
93
  end
92
94
  end
@@ -162,4 +162,84 @@ describe Acfs::Model::QueryMethods do
162
162
  end
163
163
  end
164
164
  end
165
+
166
+ describe '.find_by' do
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
175
+
176
+ context 'return value' do
177
+ subject { model.find_by age: 24 }
178
+
179
+ it { should be_a MyUser }
180
+
181
+ it { should_not be_loaded }
182
+ end
183
+
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([])
186
+
187
+ model.find_by age: 24
188
+ Acfs.run
189
+
190
+ assert_requested :get, 'http://users.example.org/users?age=24'
191
+ end
192
+
193
+ 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
197
+
198
+ it 'should invoke callback after model is loaded' do
199
+ proc = Proc.new { }
200
+
201
+ expect(proc).to receive(:call) do |user|
202
+ expect(user).to eql @user.__getobj__
203
+ expect(user).to be_a MyUser
204
+ expect(user).to be_loaded
205
+ end
206
+
207
+ @user = model.find_by age: 24, &proc
208
+ Acfs.run
209
+ end
210
+
211
+ it 'should load a single MyUser object' do
212
+ expect(subject).to be_a MyUser
213
+ end
214
+ end
215
+
216
+ context 'with empty response' do
217
+ before do
218
+ stub_request(:get, 'http://users.example.org/users?age=24').to_return response([])
219
+ end
220
+
221
+ it { should be_nil }
222
+
223
+ it 'should invoke callback after model is loaded' do
224
+ proc = Proc.new { }
225
+
226
+ expect(proc).to receive(:call) do |user|
227
+ expect(user).to eql @user.__getobj__
228
+ expect(user).to be_a NilClass
229
+ end
230
+
231
+ @user = model.find_by age: 24, &proc
232
+ Acfs.run
233
+ end
234
+ end
235
+ end
236
+
237
+ context 'singleton resource' do
238
+ let(:model) { Single }
239
+
240
+ it '.find_by should not be defined' do
241
+ expect{ model.find_by }.to raise_error NoMethodError
242
+ end
243
+ end
244
+ end
165
245
  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.27.0.1.b244
4
+ version: 0.27.0.1.b248
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-01-23 00:00:00.000000000 Z
11
+ date: 2014-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport