From 350c3a551077c1e6311f06a909bf6aa3d5526b43 Mon Sep 17 00:00:00 2001 From: Morgan Brown Date: Fri, 6 Jan 2012 13:24:38 -0800 Subject: [PATCH] Write examples for when collection synchronize is disabled Signed-off-by: David Souza --- lib/cached_resource/caching.rb | 22 +++++++++------- spec/cached_resource/caching_spec.rb | 39 +++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lib/cached_resource/caching.rb b/lib/cached_resource/caching.rb index d5132ff..439e572 100644 --- a/lib/cached_resource/caching.rb +++ b/lib/cached_resource/caching.rb @@ -11,8 +11,8 @@ class << self end module ClassMethods - # find a resource using the cache or resend the request - # if :reload is set to true or caching is disabled + # Find a resource using the cache or resend the request + # if :reload is set to true or caching is disabled. def find_with_cache(*arguments) arguments << {} unless arguments.last.is_a?(Hash) should_reload = arguments.last.delete(:reload) || !cached_resource.enabled @@ -28,13 +28,13 @@ def find_with_cache(*arguments) private - # try to find a cached response for the given key. If + # Try to find a cached response for the given key. If # no cache entry exists, send a new request. def find_via_cache(key, *arguments) cache_read(key) || find_via_reload(key, *arguments) end - # re/send the request to fetch the resource. Cache the response + # Re/send the request to fetch the resource. Cache the response # for the request. def find_via_reload(key, *arguments) object = find_without_cache(*arguments) @@ -43,10 +43,12 @@ def find_via_reload(key, *arguments) object end - # if this is a pure, unadulterated "all" request + # If this is a pure, unadulterated "all" request # write cache entries for all its members - # otherwise update an existing collection if possible + # otherwise update an existing collection if possible. def cache_collection_synchronize(object, *arguments) + return unless cached_resource.collection_synchronize + if arguments.length == 1 && arguments[0] == :all object.each {|r| cache_write(cached_resource.get_resource_id(r), r)} elsif !arguments.include?(:all) && (collection = cache_read(:all)) @@ -55,8 +57,8 @@ def cache_collection_synchronize(object, *arguments) end end - # read a entry from the cache for the given key. - # the key is processed to make sure it is valid + # Read a entry from the cache for the given key. + # Rhe key is processed to make sure it is valid. def cache_read(key) key = cache_key(Array(key)) unless key.is_a? String object = cached_resource.cache.read(key).try(:dup) @@ -64,8 +66,8 @@ def cache_read(key) object end - # write an entry to the cache for the given key and value. - # the key is processed to make sure it is valid + # Write an entry to the cache for the given key and value. + # The key is processed to make sure it is valid. def cache_write(key, object) key = cache_key(Array(key)) unless key.is_a? String cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key}") diff --git a/spec/cached_resource/caching_spec.rb b/spec/cached_resource/caching_spec.rb index ef3657c..9051060 100644 --- a/spec/cached_resource/caching_spec.rb +++ b/spec/cached_resource/caching_spec.rb @@ -87,9 +87,11 @@ class Thing < ActiveResource::Base ActiveResource::HttpMock.requests.length.should == 2 end - describe "when caching a collection" do + describe "when collection synchronize is enabled" do before(:each) do Thing.cached_resource.cache.clear + Thing.cached_resource.collection_synchronize = true + ActiveResource::HttpMock.reset! ActiveResource::HttpMock.respond_to do |mock| mock.get "/things/1.json", {}, @thing_json @@ -137,6 +139,41 @@ class Thing < ActiveResource::Base Thing.cached_resource.cache.read("thing/all")[0].name.should == result.name end end + + describe "when collection synchronize is disabled" do + before(:each) do + Thing.cached_resource.cache.clear + Thing.cached_resource.collection_synchronize = false + + ActiveResource::HttpMock.reset! + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/things/1.json", {}, @thing_json + mock.get "/things.json", {}, [@thing[:thing]].to_json(:root => :thing) + end + + # make a request for all things + Thing.all + end + + it "should not write cache entries for its members" do + result = Thing.find(1) + # both the all in the before each and this request should have been made + ActiveResource::HttpMock.requests.length.should == 2 + end + + it "should not update the collection when an individual request is reloaded" do + # change the server + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/things/1.json", {}, @other_thing_json + mock.get "/things.json", {}, [@other_thing[:thing]].to_json(:root => :thing) + end + + # reload the individual + result = Thing.find(1, :reload => true) + # the ids are the same, but the names should be different + Thing.cached_resource.cache.read("thing/all")[0].name.should_not == result.name + end + end end describe "when disabled" do