cached_resource 6.0.0 → 7.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +109 -0
- data/.gitignore +1 -0
- data/FUNDING.json +7 -0
- data/Gemfile +26 -0
- data/README.md +51 -25
- data/cached_resource.gemspec +3 -2
- data/gemfiles/4.2.gemfile +2 -5
- data/gemfiles/5.0.gemfile +0 -4
- data/gemfiles/5.1.gemfile +0 -4
- data/gemfiles/6.0.gemfile +0 -4
- data/gemfiles/6.1.gemfile +0 -4
- data/gemfiles/7.0.gemfile +0 -4
- data/lib/cached_resource/caching.rb +41 -20
- data/lib/cached_resource/version.rb +1 -1
- data/spec/cached_resource/caching_spec.rb +33 -46
- data/spec/spec_helper.rb +2 -0
- metadata +11 -27
- data/.travis.yml +0 -43
- data/Appraisals +0 -28
- data/gemfiles/4.2.gemfile.lock +0 -142
- data/gemfiles/5.0.gemfile.lock +0 -149
- data/gemfiles/5.1.gemfile.lock +0 -149
- data/gemfiles/6.0.gemfile.lock +0 -172
- data/gemfiles/6.1.gemfile.lock +0 -175
- data/gemfiles/7.0.gemfile.lock +0 -193
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0895f45566dd7c4ee2f394be6dda1c7a86aed9ad4e7c6a470d9a59d584c529c1'
|
4
|
+
data.tar.gz: 16106c7680c3cd6708b8171aa5db5f3993f3a77368685876e3e125b03e2d8e85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dbb4bc9fc1019dbf38dce7553b1ad15a964ca3699a461d551b892a26608b53ab0dbe4d8dbcce3878977f4b00debd88affa6494ad52d4fb2fb739a3ca6d239c3
|
7
|
+
data.tar.gz: 689a7edad73b7de5cff68c17d34135422d7166b1ae2745ae3be398cc77399e1ccd2d09a8423a3c7fda00f647216834e4b93c0a82287c39dd5ea5177d3fc7072f
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Test
|
9
|
+
|
10
|
+
on: [push]
|
11
|
+
|
12
|
+
permissions:
|
13
|
+
contents: read
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
test:
|
17
|
+
|
18
|
+
runs-on: ubuntu-20.04
|
19
|
+
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['1.9', '2.2', '2.3', '2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2']
|
23
|
+
rails-version: ['4.2', '5.0', '5.1', '6.0', '6.1', '7.0']
|
24
|
+
exclude:
|
25
|
+
# Segmentation faults during tests, but should work?
|
26
|
+
- ruby-version: '2.2'
|
27
|
+
rails-version: '5.0'
|
28
|
+
- ruby-version: '2.2'
|
29
|
+
rails-version: '5.1'
|
30
|
+
# Too old
|
31
|
+
- ruby-version: '1.9'
|
32
|
+
rails-version: '5.0'
|
33
|
+
- ruby-version: '1.9'
|
34
|
+
rails-version: '5.1'
|
35
|
+
- ruby-version: '1.9'
|
36
|
+
rails-version: '6.0'
|
37
|
+
- ruby-version: '1.9'
|
38
|
+
rails-version: '6.1'
|
39
|
+
- ruby-version: '1.9'
|
40
|
+
rails-version: '7.0'
|
41
|
+
# activesupport (~> 6.0.0) was resolved to 6.0.6.1, which depends on ruby (>= 2.5.0)
|
42
|
+
# activesupport (~> 6.1.0) was resolved to 6.1.7.2, which depends on ruby (>= 2.5.0)
|
43
|
+
- ruby-version: '2.2'
|
44
|
+
rails-version: '6.0'
|
45
|
+
- ruby-version: '2.2'
|
46
|
+
rails-version: '6.1'
|
47
|
+
- ruby-version: '2.3'
|
48
|
+
rails-version: '6.0'
|
49
|
+
- ruby-version: '2.3'
|
50
|
+
rails-version: '6.1'
|
51
|
+
- ruby-version: '2.4'
|
52
|
+
rails-version: '6.0'
|
53
|
+
- ruby-version: '2.4'
|
54
|
+
rails-version: '6.1'
|
55
|
+
# activesupport (~> 7.0.0) was resolved to 7.0.4.2, which depends on Ruby (>= 2.7.0)
|
56
|
+
- ruby-version: '2.2'
|
57
|
+
rails-version: '7.0'
|
58
|
+
- ruby-version: '2.3'
|
59
|
+
rails-version: '7.0'
|
60
|
+
- ruby-version: '2.4'
|
61
|
+
rails-version: '7.0'
|
62
|
+
- ruby-version: '2.5'
|
63
|
+
rails-version: '7.0'
|
64
|
+
- ruby-version: '2.6'
|
65
|
+
rails-version: '7.0'
|
66
|
+
# incompatbility with BigDecimal.new
|
67
|
+
- ruby-version: '2.7'
|
68
|
+
rails-version: '4.2'
|
69
|
+
- ruby-version: '3.0'
|
70
|
+
rails-version: '4.2'
|
71
|
+
- ruby-version: '3.1'
|
72
|
+
rails-version: '4.2'
|
73
|
+
- ruby-version: '3.2'
|
74
|
+
rails-version: '4.2'
|
75
|
+
# ArgumentError: expected attributes to be able to convert to Hash, got "#<Thing:0x000055d208b2e258>"
|
76
|
+
# probably keyword argument delegation different in Ruby 3
|
77
|
+
# https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
|
78
|
+
- ruby-version: '3.0'
|
79
|
+
rails-version: '5.0'
|
80
|
+
- ruby-version: '3.0'
|
81
|
+
rails-version: '5.1'
|
82
|
+
- ruby-version: '3.1'
|
83
|
+
rails-version: '5.0'
|
84
|
+
- ruby-version: '3.1'
|
85
|
+
rails-version: '5.1'
|
86
|
+
- ruby-version: '3.2'
|
87
|
+
rails-version: '5.0'
|
88
|
+
- ruby-version: '3.2'
|
89
|
+
rails-version: '5.1'
|
90
|
+
|
91
|
+
steps:
|
92
|
+
- uses: actions/checkout@v3
|
93
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
94
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
95
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
96
|
+
# uses: ruby/setup-ruby@v1
|
97
|
+
uses: ruby/setup-ruby@v1
|
98
|
+
with:
|
99
|
+
ruby-version: ${{ matrix.ruby-version }}
|
100
|
+
- name: Install bundler 1.x.x
|
101
|
+
if: matrix.rails-version == '4.2'
|
102
|
+
run: gem uninstall -aIx bundler && gem install bundler -v 1.17.3
|
103
|
+
- name: Install dependencies
|
104
|
+
run: bundle install
|
105
|
+
env:
|
106
|
+
TEST_RAILS_VERSION: ${{ matrix.rails-version }}
|
107
|
+
DEBUG: true
|
108
|
+
- name: Run tests
|
109
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/FUNDING.json
ADDED
data/Gemfile
CHANGED
@@ -1,3 +1,29 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
3
|
gemspec
|
4
|
+
|
5
|
+
def eval_gemfile(path)
|
6
|
+
gemfile_local = File.expand_path(path, __FILE__)
|
7
|
+
if File.readable?(gemfile_local)
|
8
|
+
puts "Loading #{gemfile_local}..." if ENV['DEBUG']
|
9
|
+
instance_eval(File.read(gemfile_local))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
puts "\e[93mUsing TEST_RAILS_VERSION #{ENV['TEST_RAILS_VERSION']}\e[0m" if ENV['DEBUG']
|
14
|
+
case ENV['TEST_RAILS_VERSION']
|
15
|
+
when "4.2"
|
16
|
+
eval_gemfile('../gemfiles/4.2.gemfile')
|
17
|
+
when "5.0"
|
18
|
+
eval_gemfile('../gemfiles/5.0.gemfile')
|
19
|
+
when "5.1"
|
20
|
+
eval_gemfile('../gemfiles/5.1.gemfile')
|
21
|
+
when "6.0"
|
22
|
+
eval_gemfile('../gemfiles/6.0.gemfile')
|
23
|
+
when "6.1"
|
24
|
+
eval_gemfile('../gemfiles/6.1.gemfile')
|
25
|
+
when "7.0"
|
26
|
+
eval_gemfile('../gemfiles/7.0.gemfile')
|
27
|
+
else
|
28
|
+
puts "\e[93mNo TEST_RAILS_VERSION present, letting dependency manager decide what's best.\e[0m" if ENV['DEBUG']
|
29
|
+
end
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# CachedResource
|
1
|
+
# CachedResource ![Tests](https://github.com/mhgbrown/cached_resource/actions/workflows/ruby.yml/badge.svg)
|
2
|
+
|
2
3
|
CachedResource is a Ruby gem whose goal is to increase the performance of interacting with web services via ActiveResource by caching responses based on request parameters. It can help reduce the lag created by making repeated requests across a network.
|
3
4
|
|
4
5
|
## Installation
|
@@ -8,28 +9,32 @@ gem install cached_resource
|
|
8
9
|
```
|
9
10
|
|
10
11
|
## Compatibility
|
11
|
-
CachedResource supports the following Ruby versions:
|
12
12
|
|
13
|
-
|
14
|
-
* 2.3.x
|
15
|
-
* 2.4.x
|
16
|
-
* 2.5.x
|
17
|
-
* 2.7.x
|
13
|
+
CachedResource is designed to be framework agnostic, but will hook into Rails for caching and logging if available. CachedResource supports the following ActiveSupport/Rails (right) and Ruby (down) version combinations:
|
18
14
|
|
19
|
-
|
15
|
+
| | 🛤️ 4.2 | 🛤️ 5.0 | 🛤️ 5.1 | 🛤️ 6.0 | 🛤️ 6.1 | 🛤️ 7.0 |
|
16
|
+
|-------|-----|-----|-----|-----|-----|-----|
|
17
|
+
| 💎 1.9 | ✅ | | | | | |
|
18
|
+
| 💎 2.2 | ✅ | | | | | |
|
19
|
+
| 💎 2.3 | ✅ | ✅ | ✅ | | | |
|
20
|
+
| 💎 2.4 | ✅ | ✅ | ✅ | | | |
|
21
|
+
| 💎 2.5 | ✅ | ✅ | ✅ | ✅ | ✅ | |
|
22
|
+
| 💎 2.6 | ✅ | ✅ | ✅ | ✅ | ✅ | |
|
23
|
+
| 💎 2.7 | | ✅ | ✅ | ✅ | ✅ | ✅ |
|
24
|
+
| 💎 3.0 | | | | ✅ | ✅ | ✅ |
|
25
|
+
| 💎 3.1 | | | | ✅ | ✅ | ✅ |
|
26
|
+
| 💎 3.2 | | | | ✅ | ✅ | ✅ |
|
20
27
|
|
21
|
-
|
28
|
+
## Limitations
|
22
29
|
|
23
|
-
|
24
|
-
* 5.0.x
|
25
|
-
* 5.1.x
|
26
|
-
* 6.0.x
|
27
|
-
* 6.1.x
|
28
|
-
* 7.0.x
|
30
|
+
The following are limitations for ActiveResource/Rails versions
|
29
31
|
|
30
|
-
|
32
|
+
| ActiveSupport Version | Limitation |
|
33
|
+
|---------------------- | ----------------------------------------------------------------------- |
|
34
|
+
| 🛤️ 4.X | You cannot chain calls. Ie `Thing.where(fn: 'foo').where(ln: 'bar')`. <br> However, you can still access `original_params` and the `resource_class` and replicate it with <br>`call1 = Thing.where(fn: 'foo')`<br>`call1.resource_class.where(call1.original_params.merge(ln: 'bar'))` |
|
31
35
|
|
32
36
|
## Configuration
|
37
|
+
|
33
38
|
**Set up CachedResource across all ActiveResources:**
|
34
39
|
|
35
40
|
```ruby
|
@@ -154,25 +159,46 @@ Since resources are cached with an argument based key, you may pass in extra dat
|
|
154
159
|
```ruby
|
155
160
|
MyActiveResource.find(:one, from: "/admin/shop.json", uid: "unique value")
|
156
161
|
```
|
162
|
+
|
157
163
|
## Testing
|
158
164
|
|
159
|
-
|
160
|
-
|
165
|
+
To test the Ruby + Rails combination configured by default:
|
166
|
+
|
167
|
+
```bash
|
168
|
+
$ rake
|
161
169
|
```
|
162
170
|
|
163
|
-
or to test all supported environments
|
171
|
+
or to test all supported environments...you have to do a little more work...
|
164
172
|
|
165
|
-
|
166
|
-
|
173
|
+
Switch your Ruby version to the desired version. This project's maintainer uses `asdf`, so switching to Ruby 3 looks like this:
|
174
|
+
|
175
|
+
```bash
|
176
|
+
$ asdf local ruby 3.0.5
|
167
177
|
```
|
168
178
|
|
169
|
-
|
179
|
+
If you have a `Gemfile.lock`, delete it:
|
170
180
|
|
171
|
-
```
|
172
|
-
|
181
|
+
```bash
|
182
|
+
$ rm Gemfile.lock
|
173
183
|
```
|
174
184
|
|
175
|
-
|
185
|
+
Then reinstall your dependencies:
|
186
|
+
|
187
|
+
```bash
|
188
|
+
$ bundle install
|
189
|
+
```
|
190
|
+
|
191
|
+
and finally, run the tests:
|
192
|
+
|
193
|
+
```bash
|
194
|
+
$ rake
|
195
|
+
```
|
196
|
+
|
197
|
+
If you want to test with a specific Rails version, start over and install dependencies with `TEST_RAILS_VERSION` set:
|
198
|
+
|
199
|
+
```bash
|
200
|
+
$ TEST_RAILS_VERSION=6.1 bundle install
|
201
|
+
```
|
176
202
|
|
177
203
|
## Credit/Inspiration
|
178
204
|
* quamen and [this gist](http://gist.github.com/947734)
|
data/cached_resource.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = "cached_resource"
|
7
7
|
s.version = CachedResource::VERSION
|
8
8
|
s.authors = "Morgan Brown"
|
9
|
-
s.email = "
|
9
|
+
s.email = "cached_resource@email.mhgbrown.is"
|
10
10
|
s.homepage = "https://github.com/mhgbrown/cached_resource"
|
11
11
|
s.summary = %q{Caching for ActiveResource}
|
12
12
|
s.description = %q{Enables request-based caching for ActiveResource}
|
@@ -17,11 +17,12 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
|
+
s.required_ruby_version = '>= 1.9.0'
|
21
|
+
|
20
22
|
s.add_runtime_dependency "activeresource", ">= 4.0"
|
21
23
|
s.add_runtime_dependency "activesupport", ">= 4.0"
|
22
24
|
s.add_runtime_dependency "nilio", ">= 1.0"
|
23
25
|
|
24
26
|
s.add_development_dependency "rake"
|
25
27
|
s.add_development_dependency "rspec"
|
26
|
-
s.add_development_dependency "appraisal"
|
27
28
|
end
|
data/gemfiles/4.2.gemfile
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
1
|
source "http://rubygems.org"
|
4
2
|
|
5
3
|
gem "rails", "~> 4.2.0"
|
6
|
-
# https://stackoverflow.com/questions/60226893/rails-nomethoderror-undefined-method-new-for-bigdecimalclass
|
7
|
-
gem "bigdecimal", "1.3.5"
|
8
4
|
|
9
|
-
|
5
|
+
# https://stackoverflow.com/questions/60226893/rails-nomethoderror-undefined-method-new-for-bigdecimalclass
|
6
|
+
# gem "bigdecimal", "1.3.5"
|
data/gemfiles/5.0.gemfile
CHANGED
data/gemfiles/5.1.gemfile
CHANGED
data/gemfiles/6.0.gemfile
CHANGED
data/gemfiles/6.1.gemfile
CHANGED
data/gemfiles/7.0.gemfile
CHANGED
@@ -37,13 +37,14 @@ module CachedResource
|
|
37
37
|
cache_read(key) || find_via_reload(key, *arguments)
|
38
38
|
end
|
39
39
|
|
40
|
-
# Re/send the request to fetch the resource
|
41
|
-
# for the request.
|
40
|
+
# Re/send the request to fetch the resource
|
42
41
|
def find_via_reload(key, *arguments)
|
43
42
|
object = find_without_cache(*arguments)
|
43
|
+
return object unless cached_resource.enabled
|
44
|
+
|
44
45
|
cache_collection_synchronize(object, *arguments) if cached_resource.collection_synchronize
|
45
46
|
return object if !cached_resource.cache_collections && is_any_collection?(*arguments)
|
46
|
-
cache_write(key, object)
|
47
|
+
cache_write(key, object, *arguments)
|
47
48
|
cache_read(key)
|
48
49
|
end
|
49
50
|
|
@@ -52,29 +53,29 @@ module CachedResource
|
|
52
53
|
# otherwise update an existing collection if possible.
|
53
54
|
def cache_collection_synchronize(object, *arguments)
|
54
55
|
if object.is_a? Enumerable
|
55
|
-
update_singles_cache(object)
|
56
|
+
update_singles_cache(object, *arguments)
|
56
57
|
# update the collection only if this is a subset of it
|
57
|
-
update_collection_cache(object) unless is_collection?(*arguments)
|
58
|
+
update_collection_cache(object, *arguments) unless is_collection?(*arguments)
|
58
59
|
else
|
59
|
-
update_collection_cache(object)
|
60
|
+
update_collection_cache(object, *arguments)
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
63
64
|
# Update the cache of singles with an array of updates.
|
64
|
-
def update_singles_cache(updates)
|
65
|
+
def update_singles_cache(updates, *arguments)
|
65
66
|
updates = Array(updates)
|
66
|
-
updates.each { |object| cache_write(cache_key(object.send(primary_key)), object) }
|
67
|
+
updates.each { |object| cache_write(cache_key(object.send(primary_key)), object, *arguments) }
|
67
68
|
end
|
68
69
|
|
69
70
|
# Update the "mother" collection with an array of updates.
|
70
|
-
def update_collection_cache(updates)
|
71
|
+
def update_collection_cache(updates, *arguments)
|
71
72
|
updates = Array(updates)
|
72
73
|
collection = cache_read(cache_key(cached_resource.collection_arguments))
|
73
74
|
|
74
75
|
if collection && !updates.empty?
|
75
76
|
index = collection.inject({}) { |hash, object| hash[object.send(primary_key)] = object; hash }
|
76
77
|
updates.each { |object| index[object.send(primary_key)] = object }
|
77
|
-
cache_write(cache_key(cached_resource.collection_arguments), index.values)
|
78
|
+
cache_write(cache_key(cached_resource.collection_arguments), index.values, *arguments)
|
78
79
|
end
|
79
80
|
end
|
80
81
|
|
@@ -101,7 +102,10 @@ module CachedResource
|
|
101
102
|
if cache.is_a? Enumerable
|
102
103
|
restored = cache.map { |record| full_dup(record) }
|
103
104
|
next restored unless respond_to?(:collection_parser)
|
104
|
-
collection_parser.new(restored)
|
105
|
+
collection_parser.new(restored).tap do |parser|
|
106
|
+
parser.resource_class = self
|
107
|
+
parser.original_params = json['original_params']
|
108
|
+
end
|
105
109
|
else
|
106
110
|
full_dup(cache)
|
107
111
|
end
|
@@ -112,8 +116,12 @@ module CachedResource
|
|
112
116
|
end
|
113
117
|
|
114
118
|
# Write an entry to the cache for the given key and value.
|
115
|
-
def cache_write(key, object)
|
116
|
-
|
119
|
+
def cache_write(key, object, *arguments)
|
120
|
+
options = arguments[1] || {}
|
121
|
+
params = options[:params]
|
122
|
+
prefix_options, query_options = split_options(params)
|
123
|
+
|
124
|
+
result = cached_resource.cache.write(key, object_to_json(object, prefix_options, query_options), :race_condition_ttl => cached_resource.race_condition_ttl, :expires_in => cached_resource.generate_ttl)
|
117
125
|
result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}")
|
118
126
|
result
|
119
127
|
end
|
@@ -150,21 +158,34 @@ module CachedResource
|
|
150
158
|
end
|
151
159
|
|
152
160
|
def json_to_object(json)
|
153
|
-
|
154
|
-
|
155
|
-
|
161
|
+
resource = json['resource']
|
162
|
+
if resource.is_a? Array
|
163
|
+
resource.map do |attrs|
|
164
|
+
self.new(attrs["object"], attrs["persistence"]).tap do |resource|
|
165
|
+
resource.prefix_options = json['prefix_options']
|
166
|
+
end
|
167
|
+
end
|
156
168
|
else
|
157
|
-
self.new(
|
169
|
+
self.new(resource["object"], resource["persistence"]).tap do |resource|
|
170
|
+
resource.prefix_options = json['prefix_options']
|
171
|
+
end
|
158
172
|
end
|
159
173
|
end
|
160
174
|
|
161
|
-
def object_to_json(object)
|
175
|
+
def object_to_json(object, prefix_options, query_options)
|
162
176
|
if object.is_a? Enumerable
|
163
|
-
|
177
|
+
{
|
178
|
+
:resource => object.map { |o| { :object => o, :persistence => o.persisted? } },
|
179
|
+
:prefix_options => prefix_options,
|
180
|
+
:original_params => query_options
|
181
|
+
}.to_json
|
164
182
|
elsif object.nil?
|
165
183
|
nil.to_json
|
166
184
|
else
|
167
|
-
{
|
185
|
+
{
|
186
|
+
:resource => { :object => object, :persistence => object.persisted? },
|
187
|
+
:prefix_options => prefix_options
|
188
|
+
}.to_json
|
168
189
|
end
|
169
190
|
end
|
170
191
|
end
|
@@ -18,6 +18,8 @@ describe CachedResource do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
@thing = {:thing => {:id => 1, :name => "Ada"}}
|
21
|
+
@thing_collection = [{:id => 1, :name => "Ada"}, {:id => 2, :name => "Ada", :major => 'CS'}]
|
22
|
+
@thing_collection2 = [{:id => 2, :name => "Ada", :major => 'CS'}]
|
21
23
|
@other_thing = {:thing => {:id => 1, :name => "Ari"}}
|
22
24
|
@thing2 = {:thing => {:id => 2, :name => "Joe"}}
|
23
25
|
@other_thing2 = {:thing => {:id => 2, :name => "Jeb"}}
|
@@ -215,13 +217,39 @@ describe CachedResource do
|
|
215
217
|
cached.should be_instance_of(ActiveResource::Collection)
|
216
218
|
end
|
217
219
|
|
218
|
-
it "should return
|
220
|
+
it "should return a chainable instance of the collection_parser" do
|
219
221
|
Thing.cached_resource.cache.clear
|
220
222
|
class CustomCollection < ActiveResource::Collection; end
|
221
223
|
Thing.collection_parser = CustomCollection
|
222
|
-
|
223
|
-
|
224
|
+
|
225
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
226
|
+
mock.get "/things.json?name=ada", {}, @thing_collection.to_json
|
227
|
+
mock.get "/things.json?major=CS&name=ada", {}, @thing_collection2.to_json
|
228
|
+
end
|
229
|
+
|
230
|
+
non_cached = Thing.where(name: 'ada')
|
231
|
+
non_cached.original_params.should == { 'name' => 'ada' }
|
232
|
+
non_cached.map(&:id).should == @thing_collection.map { |h| h[:id]}
|
233
|
+
|
234
|
+
cached = read_from_cache('thing/all/{:params=>{:name=>"ada"}}')
|
224
235
|
cached.should be_instance_of(CustomCollection)
|
236
|
+
cached.original_params.should == { 'name' => 'ada' }
|
237
|
+
cached.resource_class.should == Thing
|
238
|
+
cached.map(&:id).should == @thing_collection.map { |h| h[:id]}
|
239
|
+
|
240
|
+
if ActiveResource::VERSION::MAJOR < 5
|
241
|
+
non_cached = cached.resource_class.where(cached.original_params.merge(major: 'CS'))
|
242
|
+
else
|
243
|
+
non_cached = cached.where(major: 'CS')
|
244
|
+
end
|
245
|
+
|
246
|
+
non_cached.original_params.should == { 'name' => 'ada', 'major' => 'CS' }
|
247
|
+
non_cached.resource_class.should == Thing
|
248
|
+
non_cached.map(&:id).should == @thing_collection2.map { |h| h[:id]}
|
249
|
+
cached = read_from_cache('thing/all/{:params=>{"name"=>"ada",:major=>"cs"}}')
|
250
|
+
cached.original_params.should == { 'name' => 'ada', 'major' => 'CS' }
|
251
|
+
cached.resource_class.should == Thing
|
252
|
+
cached.map(&:id).should == @thing_collection2.map { |h| h[:id]}
|
225
253
|
end
|
226
254
|
else
|
227
255
|
it "should return an Array" do
|
@@ -505,14 +533,9 @@ describe CachedResource do
|
|
505
533
|
end
|
506
534
|
end
|
507
535
|
|
508
|
-
it "should cache a response" do
|
536
|
+
it "should not cache a response" do
|
509
537
|
result = Thing.find(1)
|
510
|
-
read_from_cache("thing/1").should
|
511
|
-
end
|
512
|
-
|
513
|
-
it "should cache a response for a string primary key" do
|
514
|
-
result = Thing.find("fded")
|
515
|
-
read_from_cache("thing/fded").should == result
|
538
|
+
read_from_cache("thing/1").should be_nil
|
516
539
|
end
|
517
540
|
|
518
541
|
it "should always remake the request" do
|
@@ -528,42 +551,6 @@ describe CachedResource do
|
|
528
551
|
Thing.find("fded")
|
529
552
|
ActiveResource::HttpMock.requests.length.should == 2
|
530
553
|
end
|
531
|
-
|
532
|
-
it "should rewrite the cache for each request" do
|
533
|
-
Thing.find(1)
|
534
|
-
old_result = read_from_cache("thing/1")
|
535
|
-
|
536
|
-
# change the response
|
537
|
-
ActiveResource::HttpMock.reset!
|
538
|
-
ActiveResource::HttpMock.respond_to do |mock|
|
539
|
-
mock.get "/things/1.json", {}, @other_thing_json
|
540
|
-
end
|
541
|
-
|
542
|
-
Thing.find(1)
|
543
|
-
new_result = read_from_cache("thing/1")
|
544
|
-
# since active resources are equal if and only if they
|
545
|
-
# are the same object or an instance of the same class,
|
546
|
-
# not new?, and have the same id.
|
547
|
-
new_result.name.should_not == old_result.name
|
548
|
-
end
|
549
|
-
|
550
|
-
it "should rewrite the cache for each request for a string primary key" do
|
551
|
-
Thing.find("fded")
|
552
|
-
old_result = read_from_cache("thing/fded")
|
553
|
-
|
554
|
-
# change the response
|
555
|
-
ActiveResource::HttpMock.reset!
|
556
|
-
ActiveResource::HttpMock.respond_to do |mock|
|
557
|
-
mock.get "/things/fded.json", {}, @other_string_thing_json
|
558
|
-
end
|
559
|
-
|
560
|
-
Thing.find("fded")
|
561
|
-
new_result = read_from_cache("thing/fded")
|
562
|
-
# since active resources are equal if and only if they
|
563
|
-
# are the same object or an instance of the same class,
|
564
|
-
# not new?, and have the same id.
|
565
|
-
new_result.name.should_not == old_result.name
|
566
|
-
end
|
567
554
|
end
|
568
555
|
|
569
556
|
describe "when cache_collections is disabled" do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'active_resource'
|
4
4
|
require 'active_support'
|
5
|
+
require 'active_support/time'
|
5
6
|
|
6
7
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
7
8
|
require 'cached_resource'
|
@@ -9,3 +10,4 @@ require 'cached_resource'
|
|
9
10
|
RSpec.configure do |config|
|
10
11
|
# nada
|
11
12
|
end
|
13
|
+
|