active_remote-cached 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0c37d8fe5adf8a1227c97dddf83db2eb5d59fc7
4
- data.tar.gz: 49690fa347012f5dd569dd1ce175f864adf83b6d
3
+ metadata.gz: d09ee17c4bc381af4901fdff4db406009ad2c027
4
+ data.tar.gz: 38b5d38c0ac4ff0dbd0852b155a6dfb5347f756b
5
5
  SHA512:
6
- metadata.gz: 553ee8882228bb293410908ef21f4c40b7decc3fa2d0a4f40adfe42d83191c2c0649266b3e7db69782f42943cdeb42dead439ebeeed9461fac2f7da02da1ebfd
7
- data.tar.gz: 42ca67feb1eddc71a2f679e4ab04453811c12d63810486adb0290d78245b4c2ad0e88ef31fa81ebab5dd7ff4afc7456e4754a9e4aee53cd2131c52a05de06c8f
6
+ metadata.gz: faa37b47e9d4df79ac735501d72e562b42e10ef5c4302305d080243382b48420417d56317aade1a7c866bf0275fcf6fc2c31ef0c619804efbf8ad6f58e99e464
7
+ data.tar.gz: b205d8e1b41052fa887efcc28ede71fb3ebaef907d05d4a40c18e4f9848713937e3b47d526e32eb5d057d06a3a93b6b464bece96bddd08720074231366ca1484
@@ -5,6 +5,7 @@ require "active_support/core_ext/array/extract_options"
5
5
 
6
6
  require "active_remote/cached/cache"
7
7
  require "active_remote/cached/version"
8
+ require "active_remote/errors"
8
9
 
9
10
  module ActiveRemote
10
11
  module Cached
@@ -81,6 +82,7 @@ module ActiveRemote
81
82
  exist_search_method_name = _cached_exist_search_method_name(arguments)
82
83
  find_method_name = _cached_find_method_name(arguments)
83
84
  search_method_name = _cached_search_method_name(arguments)
85
+ search_bang_method_name = "#{search_method_name}!"
84
86
 
85
87
  unless self.respond_to?(delete_method_name)
86
88
  _define_cached_delete_method(delete_method_name, arguments, options)
@@ -98,6 +100,10 @@ module ActiveRemote
98
100
  _define_cached_find_method(find_method_name, arguments, options)
99
101
  end
100
102
 
103
+ unless self.respond_to?(search_bang_method_name)
104
+ _define_cached_search_bang_method(search_bang_method_name, arguments, options)
105
+ end
106
+
101
107
  unless self.respond_to?(search_method_name)
102
108
  _define_cached_search_method(search_method_name, arguments, options)
103
109
  end
@@ -240,7 +246,11 @@ module ActiveRemote
240
246
  # options = ::ActiveRemote::Cached.default_options.merge({}).merge(options)
241
247
  #
242
248
  # ::ActiveRemote::Cached.cache.fetch([namespace, name, "#search", user_guid], options) do
243
- # self.search(:user_guid => user_guid)
249
+ # if block_given?
250
+ # yield
251
+ # else
252
+ # self.search(:user_guid => user_guid)
253
+ # end
244
254
  # end
245
255
  # end
246
256
  #
@@ -262,6 +272,58 @@ module ActiveRemote
262
272
  end
263
273
  RUBY
264
274
  end
275
+
276
+ def _define_cached_search_bang_method(method_name, *method_arguments, cached_finder_options)
277
+ method_arguments.flatten!
278
+ expanded_method_args = method_arguments.join(",")
279
+ sorted_method_args = method_arguments.sort.join(",")
280
+
281
+ expanded_search_args = ""
282
+ method_arguments.each do |method_argument|
283
+ expanded_search_args << ":#{method_argument} => #{method_argument},"
284
+ end
285
+
286
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
287
+ # def self.cached_search_by_user_guid!(user_guid, options = {})
288
+ # options = ::ActiveRemote::Cached.default_options.merge({}).merge(options)
289
+ #
290
+ # ::ActiveRemote::Cached.cache.fetch([namespace, name, "#search", user_guid], options) do
291
+ # results = []
292
+ #
293
+ # if block_given?
294
+ # results = yield
295
+ # else
296
+ # results = self.search(:user_guid => user_guid)
297
+ # end
298
+ #
299
+ # raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.size <= 0
300
+ # results
301
+ # end
302
+ # end
303
+ #
304
+ # If a block is given, it is incumbent on the caller to make sure the expectation
305
+ # of the result object is maintained for requests/responses
306
+ #
307
+ def self.#{method_name}(#{expanded_method_args}, options = {})
308
+ options = ::ActiveRemote::Cached.default_options.merge(#{cached_finder_options}).merge(options)
309
+ namespace = options.delete(:namespace)
310
+ cache_key = [namespace, name, "#search", #{sorted_method_args}].compact
311
+
312
+ ::ActiveRemote::Cached.cache.fetch(cache_key, options) do
313
+ results = []
314
+
315
+ if block_given?
316
+ results = yield
317
+ else
318
+ results = self.search(#{expanded_search_args})
319
+ end
320
+
321
+ raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.first.nil?
322
+ results
323
+ end
324
+ end
325
+ RUBY
326
+ end
265
327
  end
266
328
 
267
329
  # Initialize the cache provider with a MemoryStore cache
@@ -1,5 +1,5 @@
1
1
  module ActiveRemote
2
2
  module Cached
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -20,6 +20,10 @@ describe SearchMethodClass do
20
20
  SearchMethodClass.must_respond_to("cached_search_by_foo")
21
21
  end
22
22
 
23
+ it "creates 'cached_search_by_foo!'" do
24
+ SearchMethodClass.must_respond_to("cached_search_by_foo!")
25
+ end
26
+
23
27
  it "creates 'cached_search_by_guid'" do
24
28
  SearchMethodClass.must_respond_to("cached_search_by_guid")
25
29
  end
@@ -47,6 +51,10 @@ describe SearchMethodClass do
47
51
  it "creates 'cached_search_by_client_guid_and_user_guid_and_derp'" do
48
52
  SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp")
49
53
  end
54
+
55
+ it "creates 'cached_search_by_client_guid_and_user_guid_and_derp!'" do
56
+ SearchMethodClass.must_respond_to("cached_search_by_client_guid_and_user_guid_and_derp!")
57
+ end
50
58
  end
51
59
 
52
60
  describe "#cached_search_by_guid" do
@@ -176,4 +184,27 @@ describe SearchMethodClass do
176
184
  end
177
185
  end
178
186
  end
187
+
188
+ describe "#cached_search_by_foo!" do
189
+ before do
190
+ ::ActiveRemote::Cached.cache(HashCache.new)
191
+ ::ActiveRemote::Cached.default_options(:expires_in => 100)
192
+ end
193
+
194
+ after do
195
+ ::ActiveRemote::Cached.default_options({})
196
+ end
197
+
198
+ it "returns results when present" do
199
+ SearchMethodClass.stub(:search, [:hello]) do
200
+ SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200).must_equal([:hello])
201
+ end
202
+ end
203
+
204
+ it "raises ActiveRemote::RemoteRecordNotFound when not found" do
205
+ SearchMethodClass.stub(:search, []) do
206
+ -> { SearchMethodClass.cached_search_by_foo!(:foo, :expires_in => 200) }.must_raise ::ActiveRemote::RemoteRecordNotFound
207
+ end
208
+ end
209
+ end
179
210
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote-cached
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Dewitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-09 00:00:00.000000000 Z
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_remote
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
- rubygems_version: 2.2.2
141
+ rubygems_version: 2.5.1
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: Provides a configuration for caching mechanisms and finders on ActiveRemote