Skip to content

Commit

Permalink
Add ability to specify which arguments constitute the collection request
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 8202cfa commit 991b4be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/cached_resource/caching.rb
Expand Up @@ -61,20 +61,20 @@ def update_singles_cache(updates)
# Update the "mother" collection with an array of updates.
def update_collection_cache(updates)
updates = Array(updates)
collection = cache_read(:all)
collection = cache_read(cached_resource.collection_arguments)

if collection && !updates.empty?
store = RUBY_VERSION.to_f < 1.9 ? ActiveSupport::OrderedHash.new : {}
index = collection.inject(store) {|hash, object| hash[object.send(primary_key)] = object; hash}
updates.each {|object| index[object.send(primary_key)] = object}
cache_write(:all, index.values)
cache_write(cached_resource.collection_arguments, index.values)
end
end

# Determine if the given arguments represent
# the entire collection of objects.
def is_collection?(*arguments)
arguments.length == 1 && arguments[0] == :all
arguments == cached_resource.collection_arguments
end

# Read a entry from the cache for the given key.
Expand Down
1 change: 1 addition & 0 deletions lib/cached_resource/configuration.rb
Expand Up @@ -24,6 +24,7 @@ def initialize(options={})
:enabled => true,
:ttl => 604800,
:collection_synchronize => false,
:collection_arguments => [:all],
:cache => defined?(Rails.cache) && Rails.cache || CACHE,
:logger => defined?(Rails.logger) && Rails.logger || LOGGER
}.merge(options))
Expand Down
13 changes: 12 additions & 1 deletion spec/cached_resource/configuration_spec.rb
Expand Up @@ -17,6 +17,10 @@
configuration.collection_synchronize.should == false
end

it "should default to :all for collection arguments" do
configuration.collection_arguments.should == [:all]
end

describe "outside a Rails environment" do
it "should be logging to a buffered logger attached to a NilIO" do
configuration.logger.class.should == ActiveSupport::BufferedLogger
Expand Down Expand Up @@ -55,7 +59,13 @@
describe "when initialized through cached resource" do
before(:each) do
class Foo < ActiveResource::Base
cached_resource :ttl => 1, :cache => "cache", :logger => "logger", :enabled => false, :collection_synchronize => true, :custom => "irrelevant"
cached_resource :ttl => 1,
:cache => "cache",
:logger => "logger",
:enabled => false,
:collection_synchronize => true,
:collection_arguments => [:every],
:custom => "irrelevant"
end
end

Expand All @@ -69,6 +79,7 @@ class Foo < ActiveResource::Base
Foo.cached_resource.logger.should == "logger"
Foo.cached_resource.enabled.should == false
Foo.cached_resource.collection_synchronize.should == true
Foo.cached_resource.collection_arguments.should == [:every]
Foo.cached_resource.custom.should == "irrelevant"
end
end
Expand Down

0 comments on commit 991b4be

Please sign in to comment.