Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

set options given to Rack::Cache.new

  • Loading branch information...
commit 505932b53e419d79e4186869b64617187ff9f836 1 parent 0356b30
@rtomayko authored
View
2  doc/index.markdown
@@ -3,7 +3,7 @@ for [Rack][]-based applications that produce freshness (`Expires`,
`Cache-Control`) and/or validation (`Last-Modified`, `ETag`) information.
* Standards-based ([RFC 2616][rfc] / [Section 13][s13]).
- * Freshness _and_ expiration based caching and validation.
+ * Freshness/expiration based caching and validation.
* Portable: 100% Ruby / works with any Rack-enabled framework.
* [VCL][]ish configuration language for advanced caching policies.
* Disk, memcached, and heap memory storage backends.
View
16 lib/rack/cache/options.rb
@@ -7,7 +7,7 @@ module Options
class << self
private
- def option_accessor(key)
+ def attr_accessor(key)
define_method(key) { || read_option(key) }
define_method("#{key}=") { |value| write_option(key, value) }
define_method("#{key}?") { || !! read_option(key) }
@@ -16,12 +16,12 @@ def option_accessor(key)
# Enable verbose trace logging. This option is currently enabled by
# default but is likely to be disabled in a future release.
- option_accessor :verbose
+ attr_accessor :verbose
# The storage resolver. Defaults to the Rack::Cache.storage singleton instance
# of Rack::Cache::Storage. This object is responsible for resolving metastore
# and entitystore URIs to an implementation instances.
- option_accessor :storage
+ attr_accessor :storage
# A URI specifying the meta-store implementation that should be used to store
# request/response meta information. The following URIs schemes are
@@ -34,7 +34,7 @@ def option_accessor(key)
# If no meta store is specified the 'heap:/' store is assumed. This
# implementation has significant draw-backs so explicit configuration is
# recommended.
- option_accessor :metastore
+ attr_accessor :metastore
# A URI specifying the entity-store implement that should be used to store
# response bodies. See the metastore option for information on supported URI
@@ -43,7 +43,7 @@ def option_accessor(key)
# If no entity store is specified the 'heap:/' store is assumed. This
# implementation has significant draw-backs so explicit configuration is
# recommended.
- option_accessor :entitystore
+ attr_accessor :entitystore
# The number of seconds that a cached object should be considered
# "fresh" when no explicit freshness information is provided in
@@ -51,7 +51,7 @@ def option_accessor(key)
# override this value.
#
# Default: 0
- option_accessor :default_ttl
+ attr_accessor :default_ttl
# The underlying options Hash. During initialization (or outside of a
# request), this is a default values Hash. During a request, this is the
@@ -75,7 +75,7 @@ def set(option, value=self)
end
end
- protected
+ private
def read_option(key)
options[option_name(key)]
end
@@ -101,7 +101,7 @@ def initialize_options(options={})
'rack-cache.entitystore' => 'heap:/',
'rack-cache.default_ttl' => 0
}
- @default_options.merge!(options)
+ self.options = options
end
end
View
15 test/cache_test.rb
@@ -5,17 +5,9 @@ def dumb_app(env)
[ 200, {'Content-Type' => 'text/plain'}, body ]
end
-describe 'Rack::Cache' do
- it 'has a Request class' do
- Rack::Cache::Request.should.be.kind_of Class
- end
- it 'has a Response class' do
- Rack::Cache::Response.should.be.kind_of Class
- end
-end
-
describe 'Rack::Cache::new' do
before { @app = method(:dumb_app) }
+
it 'takes a backend and returns a middleware component' do
Rack::Cache.new(@app).
should.respond_to :call
@@ -24,6 +16,11 @@ def dumb_app(env)
lambda { Rack::Cache.new(@app, {}) }.
should.not.raise(ArgumentError)
end
+ it 'sets options provided in the options Hash' do
+ object = Rack::Cache.new(@app, :foo => 'bar', 'foo.bar' => 'bling')
+ object.options['foo.bar'].should.be == 'bling'
+ object.options['rack-cache.foo'].should.be == 'bar'
+ end
it 'takes a block; executes it during initialization' do
state, block_scope = 'not invoked', nil
object =
View
25 test/options_test.rb
@@ -2,7 +2,7 @@
require 'rack/cache/options'
module Rack::Cache::Options
- option_accessor :foo
+ attr_accessor :foo
end
class MockOptions
@@ -11,25 +11,34 @@ class MockOptions
end
describe 'Rack::Cache::Options' do
-
before { @options = MockOptions.new }
describe '#set' do
- it 'sets existing options' do
- @options.set :foo, 'bar'
- @options.foo.should.be == 'bar'
+ it 'sets a Symbol option as rack-cache.symbol' do
+ @options.set :bar, 'baz'
+ @options.options['rack-cache.bar'].should.be == 'baz'
+ end
+ it 'sets a String option as string' do
+ @options.set 'foo.bar', 'bling'
+ @options.options['foo.bar'].should.be == 'bling'
end
it 'sets all key/value pairs when given a Hash' do
- @options.set :foo => 'bar',
- :bar => 'baz'
+ @options.set :foo => 'bar', :bar => 'baz', 'foo.bar' => 'bling'
@options.foo.should.be == 'bar'
@options.options['rack-cache.bar'].should.be == 'baz'
+ @options.options['foo.bar'].should.be == 'bling'
end
end
+ it 'makes options declared with option_accessor available as attributes' do
+ @options.set :foo, 'bar'
+ @options.foo.should.be == 'bar'
+ end
+
it 'allows setting multiple options via assignment' do
- @options.options = { :foo => 'bar', :bar => 'baz' }
+ @options.options = { :foo => 'bar', :bar => 'baz', 'foo.bar' => 'bling' }
@options.foo.should.be == 'bar'
+ @options.options['foo.bar'].should.be == 'bling'
@options.options['rack-cache.bar'].should.be == 'baz'
end
Please sign in to comment.
Something went wrong with that request. Please try again.