Module: Rack::Cache::Options

Extended by:
Options
Included in:
Context, Options
Defined in:
lib/rack/cache/options.rb

Overview

Configuration options and utility methods for option access. Rack::Cache uses the Rack Environment to store option values. All options documented below are stored in the Rack Environment as “rack-cache.<option>”, where <option> is the option name.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.option_accessor(key) ⇒ Object



13
14
15
16
17
18
# File 'lib/rack/cache/options.rb', line 13

def self.option_accessor(key)
  name = option_name(key)
  define_method(key) { || options[name] }
  define_method("#{key}=") { |value| options[name] = value }
  define_method("#{key}?") { || !! options[name] }
end

.option_name(key) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rack/cache/options.rb', line 20

def option_name(key)
  case key
  when Symbol ; "rack-cache.#{key}"
  when String ; key
  else raise ArgumentError
  end
end

Instance Method Details

#optionsObject

The underlying options Hash. During initialization (or outside of a request), this is a default values Hash. During a request, this is the Rack environment Hash. The default values Hash is merged in underneath the Rack environment before each request is processed.



118
119
120
# File 'lib/rack/cache/options.rb', line 118

def options
  @env || @default_options
end

#options=(hash = {}) ⇒ Object

Set multiple options.



123
124
125
# File 'lib/rack/cache/options.rb', line 123

def options=(hash={})
  hash.each { |key,value| write_option(key, value) }
end

#set(option, value = self, &block) ⇒ Object

Set an option. When option is a Symbol, it is set in the Rack Environment as “rack-cache.option”. When option is a String, it exactly as specified. The option argument may also be a Hash in which case each key/value pair is merged into the environment as if the #set method were called on each.



132
133
134
135
136
137
138
139
140
# File 'lib/rack/cache/options.rb', line 132

def set(option, value=self, &block)
  if block_given?
    write_option option, block
  elsif value == self
    self.options = option.to_hash
  else
    write_option option, value
  end
end