cached_resource 5.0.1 → 5.1.0

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: c76de5ea977a9387d2c796a1935aa80aa33cc559
4
- data.tar.gz: 747fb41b327ea8117ff01cce745f9ee0b0d4e468
3
+ metadata.gz: 649cd7b534f3afc2628e4400f3b08e05769ed68b
4
+ data.tar.gz: 4e47e85d1ae399953ae9544bfe8b0ae0de9c99c7
5
5
  SHA512:
6
- metadata.gz: 8068e71dc7aefadec576363763c94499d7fe88d7fd75f37480db7453ca72fae889716ae897e86905040a25c26b4a206e89e0d2948dbf4618a4f367cdb45f51a4
7
- data.tar.gz: 37efb08196caa091b6d8cb1f56e0134f6d5c4b5662135d1fce5c000a1d61c85fbb234fb8793050c7e8ddbd794cdee34bb2b415007ac50cbf2d9165f8c5003b3c
6
+ metadata.gz: dabb024419aeb48a493fe0d4b1bdffdf7177b17a6c4fbcd573f8fbe99ee756acbbfb809daf8dd8a314843ab32bde126b9e5609b941864e5bc95f99949d6e00fe
7
+ data.tar.gz: 6008d2ed179cd97d1187cfe4a0f15922a48e1b5ae88af77f857643ac05e76eb8d2326675ecaeca2b78b4d3df75d6fb3c294a5492d65458a6d4bf0a7453fb36e5
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2017 Morgan Brown
1
+ Copyright (c) 2018 Morgan Brown
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -121,19 +121,15 @@ Since resources are cached with an argument based key, you may pass in extra dat
121
121
  ## Testing
122
122
  rake
123
123
 
124
+ or to test all supported environments
125
+
126
+ bundle exec appraisal rake
127
+
128
+ For more details, head over to the [appraisal](https://github.com/thoughtbot/appraisal) documentation.
129
+
124
130
  ## Credit/Inspiration
125
131
  * quamen and [this gist](http://gist.github.com/947734)
126
132
  * latimes and [this plugin](http://github.com/latimes/cached_resource)
127
133
 
128
134
  ## Feedback/Problems
129
135
  Feedback is greatly appreciated! Check out this project's [issue tracker](https://github.com/Ahsizara/cached_resource/issues) if you've got anything to say.
130
-
131
- ## Future Considerations
132
- This may change at any time.
133
-
134
- * Callbacks on before and after reload
135
- * Consider checksums to improve the determination of freshness/changédness
136
-
137
-
138
-
139
-
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- cached_resource (5.0.0)
4
+ cached_resource (5.1.0)
5
5
  activeresource (>= 4.0)
6
6
  activesupport (>= 4.0)
7
7
  nilio (>= 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- cached_resource (5.0.0)
4
+ cached_resource (5.1.0)
5
5
  activeresource (>= 4.0)
6
6
  activesupport (>= 4.0)
7
7
  nilio (>= 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- cached_resource (5.0.0)
4
+ cached_resource (5.1.0)
5
5
  activeresource (>= 4.0)
6
6
  activesupport (>= 4.0)
7
7
  nilio (>= 1.0)
@@ -7,7 +7,7 @@ module CachedResource
7
7
  included do
8
8
  class << self
9
9
  alias_method :find_without_cache, :find
10
- alias_method :find, :find_with_cache
10
+ alias_method :find, :find_with_cache
11
11
  end
12
12
  end
13
13
 
@@ -84,7 +84,8 @@ module CachedResource
84
84
 
85
85
  # Read a entry from the cache for the given key.
86
86
  def cache_read(key)
87
- object = cached_resource.cache.read(key).try do |cache|
87
+ object = cached_resource.cache.read(key).try do |json_cache|
88
+ cache = json_to_object(JSON.parse(json_cache))
88
89
  if cache.is_a? Enumerable
89
90
  restored = cache.map { |record| full_dup(record) }
90
91
  next restored unless respond_to?(:collection_parser)
@@ -99,7 +100,7 @@ module CachedResource
99
100
 
100
101
  # Write an entry to the cache for the given key and value.
101
102
  def cache_write(key, object)
102
- result = cached_resource.cache.write(key, object, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
103
+ result = cached_resource.cache.write(key, object.to_json, :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
103
104
  result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
104
105
  result
105
106
  end
@@ -124,6 +125,14 @@ module CachedResource
124
125
  end
125
126
  end
126
127
 
128
+ def json_to_object(json)
129
+ if json.is_a? Array
130
+ json.map { |attrs| self.new(attrs) }
131
+ else
132
+ self.new(json)
133
+ end
134
+ end
135
+
127
136
  end
128
137
  end
129
138
  end
@@ -1,3 +1,3 @@
1
1
  module CachedResource
2
- VERSION = "5.0.1"
2
+ VERSION = "5.1.0"
3
3
  end
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe CachedResource do
4
4
 
5
+ def read_from_cache(key)
6
+ Thing.send(:cache_read, key)
7
+ end
8
+
5
9
  before(:each) do
6
10
  class Thing < ActiveResource::Base
7
11
  self.site = "http://api.thing.com"
@@ -43,23 +47,24 @@ describe CachedResource do
43
47
 
44
48
  it "should cache a response" do
45
49
  result = Thing.find(1)
46
- Thing.cached_resource.cache.read("thing/1").should == result
50
+
51
+ read_from_cache("thing/1").should == result
47
52
  end
48
53
 
49
54
  it "should cache a response for a string primary key" do
50
55
  result = Thing.find("fded")
51
- Thing.cached_resource.cache.read("thing/fded").should == result
56
+ read_from_cache("thing/fded").should == result
52
57
  end
53
58
 
54
59
  it "should cache without whitespace in keys" do
55
60
  result = Thing.find(1, :from => 'path', :params => { :foo => 'bar' })
56
- Thing.cached_resource.cache.read('thing/1/{:from=>"path",:params=>{:foo=>"bar"}}').should == result
61
+ read_from_cache('thing/1/{:from=>"path",:params=>{:foo=>"bar"}}').should == result
57
62
  end
58
63
 
59
64
  it "should empty the cache when clear_cache is called" do
60
65
  result = Thing.find(1)
61
66
  Thing.clear_cache
62
- Thing.cached_resource.cache.read("thing/1").should == nil
67
+ read_from_cache("thing/1").should == nil
63
68
  end
64
69
 
65
70
  it "should cache a response with the same persistence" do
@@ -108,7 +113,7 @@ describe CachedResource do
108
113
  # make a request
109
114
  Thing.find(1)
110
115
  # get the cached result of the request
111
- old_result = Thing.cached_resource.cache.read("thing/1")
116
+ old_result = read_from_cache("thing/1")
112
117
 
113
118
  # change the response
114
119
  ActiveResource::HttpMock.reset!
@@ -117,7 +122,7 @@ describe CachedResource do
117
122
  end
118
123
 
119
124
  Thing.find(1, :reload => true)
120
- new_result = Thing.cached_resource.cache.read("thing/1")
125
+ new_result = read_from_cache("thing/1")
121
126
  # since active resources are equal if and only if they
122
127
  # are the same object or an instance of the same class,
123
128
  # not new?, and have the same id.
@@ -156,7 +161,7 @@ describe CachedResource do
156
161
  shared_examples "collection_return_type" do
157
162
  if ActiveResource::VERSION::MAJOR >= 4
158
163
  it "should return an ActiveResource::Collection" do
159
- cached = Thing.cached_resource.cache.read("thing/all")
164
+ cached = read_from_cache("thing/all")
160
165
  cached.should be_instance_of(ActiveResource::Collection)
161
166
  end
162
167
 
@@ -165,12 +170,12 @@ describe CachedResource do
165
170
  class CustomCollection < ActiveResource::Collection; end
166
171
  Thing.collection_parser = CustomCollection
167
172
  Thing.all
168
- cached = Thing.cached_resource.cache.read("thing/all")
173
+ cached = read_from_cache("thing/all")
169
174
  cached.should be_instance_of(CustomCollection)
170
175
  end
171
176
  else
172
177
  it "should return an Array" do
173
- cached = Thing.cached_resource.cache.read("thing/all")
178
+ cached = read_from_cache("thing/all")
174
179
  cached.should be_instance_of(Array)
175
180
  end
176
181
  end
@@ -222,8 +227,8 @@ describe CachedResource do
222
227
  shared_examples "collection_cache_clearing" do
223
228
  it "should empty the cache when clear_cache is called" do
224
229
  Thing.clear_cache
225
- Thing.cached_resource.cache.read("thing/all").should == nil
226
- Thing.cached_resource.cache.read("thing/1").should == nil
230
+ read_from_cache("thing/all").should == nil
231
+ read_from_cache("thing/1").should == nil
227
232
  end
228
233
 
229
234
  end
@@ -254,8 +259,8 @@ describe CachedResource do
254
259
  # only the all request should have been made
255
260
  ActiveResource::HttpMock.requests.length.should == 1
256
261
  # the result should be cached with the appropriate key
257
- Thing.cached_resource.cache.read("thing/1").should == result
258
- Thing.cached_resource.cache.read("thing/fded").should == string_result
262
+ read_from_cache("thing/1").should == result
263
+ read_from_cache("thing/fded").should == string_result
259
264
  end
260
265
 
261
266
  include_examples "collection_cache_clearing"
@@ -274,11 +279,11 @@ describe CachedResource do
274
279
  Thing.all(:reload => true)
275
280
  # get the updated result, read from the cache
276
281
  result = Thing.find(1)
277
- Thing.cached_resource.cache.read("thing/all")[0].should == result
278
- Thing.cached_resource.cache.read("thing/all")[0].name.should == result.name
282
+ read_from_cache("thing/all")[0].should == result
283
+ read_from_cache("thing/all")[0].name.should == result.name
279
284
  string_result = Thing.find("fded")
280
- Thing.cached_resource.cache.read("thing/all")[1].should == string_result
281
- Thing.cached_resource.cache.read("thing/all")[1].name.should == string_result.name
285
+ read_from_cache("thing/all")[1].should == string_result
286
+ read_from_cache("thing/all")[1].name.should == string_result.name
282
287
  end
283
288
 
284
289
  it "should update the collection when an individual request is reloaded" do
@@ -291,11 +296,11 @@ describe CachedResource do
291
296
 
292
297
  # reload the individual
293
298
  result = Thing.find(1, :reload => true)
294
- Thing.cached_resource.cache.read("thing/all")[0].should == result
295
- Thing.cached_resource.cache.read("thing/all")[0].name.should == result.name
299
+ read_from_cache("thing/all")[0].should == result
300
+ read_from_cache("thing/all")[0].name.should == result.name
296
301
  string_result = Thing.find("fded", :reload => true)
297
- Thing.cached_resource.cache.read("thing/all")[1].should == string_result
298
- Thing.cached_resource.cache.read("thing/all")[1].name.should == string_result.name
302
+ read_from_cache("thing/all")[1].should == string_result
303
+ read_from_cache("thing/all")[1].name.should == string_result.name
299
304
  end
300
305
 
301
306
  it "should update both the collection and the member cache entries when a subset of the collection is retrieved" do
@@ -312,20 +317,20 @@ describe CachedResource do
312
317
  # make a request for a subset of the "mother" collection
313
318
  result = Thing.find(:all, :params => {:name => "Ari"})
314
319
  # the collection should be updated to reflect the server change
315
- Thing.cached_resource.cache.read("thing/all")[0].should == result[0]
316
- Thing.cached_resource.cache.read("thing/all")[0].name.should == result[0].name
320
+ read_from_cache("thing/all")[0].should == result[0]
321
+ read_from_cache("thing/all")[0].name.should == result[0].name
317
322
  # the individual should be updated to reflect the server change
318
- Thing.cached_resource.cache.read("thing/1").should == result[0]
319
- Thing.cached_resource.cache.read("thing/1").name.should == result[0].name
323
+ read_from_cache("thing/1").should == result[0]
324
+ read_from_cache("thing/1").name.should == result[0].name
320
325
 
321
326
  # make a request for a subset of the "mother" collection
322
327
  result = Thing.find(:all, :params => {:name => "Lon"})
323
328
  # the collection should be updated to reflect the server change
324
- Thing.cached_resource.cache.read("thing/all")[1].should == result[0]
325
- Thing.cached_resource.cache.read("thing/all")[1].name.should == result[0].name
329
+ read_from_cache("thing/all")[1].should == result[0]
330
+ read_from_cache("thing/all")[1].name.should == result[0].name
326
331
  # the individual should be updated to reflect the server change
327
- Thing.cached_resource.cache.read("thing/fded").should == result[0]
328
- Thing.cached_resource.cache.read("thing/fded").name.should == result[0].name
332
+ read_from_cache("thing/fded").should == result[0]
333
+ read_from_cache("thing/fded").name.should == result[0].name
329
334
  end
330
335
 
331
336
  it "should maintain the order of the collection when updating it" do
@@ -407,12 +412,12 @@ describe CachedResource do
407
412
  # reload the individual
408
413
  result = Thing.find(1, :reload => true)
409
414
  # the ids are the same, but the names should be different
410
- Thing.cached_resource.cache.read("thing/all")[0].name.should_not == result.name
415
+ read_from_cache("thing/all")[0].name.should_not == result.name
411
416
 
412
417
  # reload the individual
413
418
  string_result = Thing.find("fded", :reload => true)
414
419
  # the ids are the same, but the names should be different
415
- Thing.cached_resource.cache.read("thing/all")[1].name.should_not == string_result.name
420
+ read_from_cache("thing/all")[1].name.should_not == string_result.name
416
421
  end
417
422
  end
418
423
 
@@ -452,12 +457,12 @@ describe CachedResource do
452
457
 
453
458
  it "should cache a response" do
454
459
  result = Thing.find(1)
455
- Thing.cached_resource.cache.read("thing/1").should == result
460
+ read_from_cache("thing/1").should == result
456
461
  end
457
462
 
458
463
  it "should cache a response for a string primary key" do
459
464
  result = Thing.find("fded")
460
- Thing.cached_resource.cache.read("thing/fded").should == result
465
+ read_from_cache("thing/fded").should == result
461
466
  end
462
467
 
463
468
  it "should always remake the request" do
@@ -476,7 +481,7 @@ describe CachedResource do
476
481
 
477
482
  it "should rewrite the cache for each request" do
478
483
  Thing.find(1)
479
- old_result = Thing.cached_resource.cache.read("thing/1")
484
+ old_result = read_from_cache("thing/1")
480
485
 
481
486
  # change the response
482
487
  ActiveResource::HttpMock.reset!
@@ -485,7 +490,7 @@ describe CachedResource do
485
490
  end
486
491
 
487
492
  Thing.find(1)
488
- new_result = Thing.cached_resource.cache.read("thing/1")
493
+ new_result = read_from_cache("thing/1")
489
494
  # since active resources are equal if and only if they
490
495
  # are the same object or an instance of the same class,
491
496
  # not new?, and have the same id.
@@ -494,7 +499,7 @@ describe CachedResource do
494
499
 
495
500
  it "should rewrite the cache for each request for a string primary key" do
496
501
  Thing.find("fded")
497
- old_result = Thing.cached_resource.cache.read("thing/fded")
502
+ old_result = read_from_cache("thing/fded")
498
503
 
499
504
  # change the response
500
505
  ActiveResource::HttpMock.reset!
@@ -503,7 +508,7 @@ describe CachedResource do
503
508
  end
504
509
 
505
510
  Thing.find("fded")
506
- new_result = Thing.cached_resource.cache.read("thing/fded")
511
+ new_result = read_from_cache("thing/fded")
507
512
  # since active resources are equal if and only if they
508
513
  # are the same object or an instance of the same class,
509
514
  # not new?, and have the same id.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morgan Brown
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-19 00:00:00.000000000 Z
11
+ date: 2018-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource