Skip to content

Commit

Permalink
Write examples for when collection synchronize is disabled
Browse files Browse the repository at this point in the history
Signed-off-by: David Souza <david@souza.net>
  • Loading branch information
Morgan Brown authored and davidsouza committed Apr 5, 2012
1 parent 0d64c8f commit 350c3a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
22 changes: 12 additions & 10 deletions lib/cached_resource/caching.rb
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -55,17 +57,17 @@ 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)
object && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} 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}")
Expand Down
39 changes: 38 additions & 1 deletion spec/cached_resource/caching_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 350c3a5

Please sign in to comment.