Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mckramer/earth_tools
base: b72b5c6ff9
...
head fork: mckramer/earth_tools
compare: ff71b399b3
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
2  earth_tools.gemspec
@@ -1,4 +1,4 @@
-require 'rake'
+require 'rake'
require './lib/earth_tools/version'
Gem::Specification.new do |gem|
View
12 lib/earth_tools/cache.rb
@@ -22,13 +22,13 @@ def initialize(store, prefix)
# Read from the cache.
# @return the object saved in the cache
def [](url)
- store[key_for(url)]
+ @store[key_for(url)]
end
##
# Write to the cache.
def []=(url, value)
- store[key_for(url)] = value
+ @store[key_for(url)] = value
end
##
@@ -38,7 +38,7 @@ def expire(url)
if url == :all
urls.each{ |u| expire(u) }
else
- store.del(key_for(url))
+ @store.send(@store.respond_to?(:del) ? :del : :delete, key_for(url))
end
end
@@ -51,7 +51,7 @@ def expire(url)
# Cache key for a given URL.
# @return [String] the cache key
def key_for(url)
- [prefix, url].join
+ [@prefix, url].join
end
##
@@ -59,14 +59,14 @@ def key_for(url)
# that have non-nil values.
# @return [Array] the list of keys
def keys
- store.keys.select{ |k| k.match /^#{prefix}/ and cleave(store[k]) }
+ @store.keys.select{ |k| k.match /^#{@prefix}/ and cleave(@store[k]) }
end
##
# Array of cached URLs.
# @return [Array] the list of cached URLs
def urls
- keys.map{ |k| k[/^#{prefix}(.*)/, 1] }
+ keys.map{ |k| k[/^#{@prefix}(.*)/, 1] }
end
##
View
49 test/cache_test.rb
@@ -0,0 +1,49 @@
+# encoding: utf-8
+
+require 'test_helper'
+require 'earth_tools'
+require 'earth_tools/cache'
+
+##
+# Tests for the Cache
+#
+class CacheTest < MiniTest::Unit::TestCase
+
+ def setup
+ @raw_cache = {}
+ @cache = EarthTools::Cache.new(@raw_cache, 'earth_tools:')
+ end
+
+ def test_store_and_retrieve_from_cache
+ @cache['test/1'] = :data
+
+ assert_equal :data, @cache['test/1'], 'The cache was not stored properly.'
+ end
+
+ def test_remove_single_url
+ @cache['test/1'] = :data
+ @cache['test/2'] = :data
+
+ @cache.expire('test/1')
+
+ assert_nil @cache['test/1'], 'The cache should have deleted entry.'
+ assert_equal :data, @cache['test/2'], 'The cache should not have deleted entry.'
+
+ end
+
+ def test_remove_all_prefixed_urls
+ @raw_cache['test'] = :data
+ @raw_cache['other:test'] = :data
+ @cache['test/1'] = :data
+ @cache['test/2'] = :data
+ @cache['test/3'] = :data
+
+ @cache.expire(:all)
+ assert_equal :data, @raw_cache['test'], 'The cache should not affect non-prefixed entries.'
+ assert_equal :data, @raw_cache['other:test'], 'The cache should not affect non-prefixed entries.'
+ assert_nil @cache['test/1'], 'The cache should be empty of prefixed entries.'
+ assert_nil @cache['test/2'], 'The cache should be empty of prefixed entries.'
+ assert_nil @cache['test/3'], 'The cache should be empty of prefixed entries.'
+ end
+
+end

No commit comments for this range

Something went wrong with that request. Please try again.