Class: Wallet
- Inherits:
-
Object
- Object
- Wallet
- Defined in:
- lib/wallet/wallet.rb
Overview
- Author
-
Matt Parker ([email protected])
- License
-
Distributes under the same terms as Ruby
Defined Under Namespace
Classes: ActionNotCached, UnknownController, YamlError
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
:nodoc:.
-
#default_ttl ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#cached?(controller, action) ⇒ Boolean
Returns true or false based on whether or not a controller action is configured for caching in the wallet irb> w = Wallet.new “pages:n show:” irb> w.cached? :pages, :show => true.
- #cached_actions(controller) ⇒ Object
-
#initialize(config_yml = "") ⇒ Wallet
constructor
Pass in a yaml string to this method that represents your configuration.
-
#ttl(controller, action) ⇒ Object
Returns the value of the ttl for a controller / action.
Constructor Details
#initialize(config_yml = "") ⇒ Wallet
Pass in a yaml string to this method that represents your configuration. Let’s assume you have a PagesController with a show action and an index action, and let’s suppose you would like wallet to default your action cache ttl’s to 5 hours. Then your yaml might be formatted like so:
# /path/to/wallet.yml
default_ttl: 5 hours
pages:
show:
index: 20 minutes
That would result in the following values:
irb> w = Wallet.new File.open("/path/to/wallet.yml")
irb> w.cached? :pages, :show
=> true
irb> w.cached? :pages, :index
=> true
irb> w.cached? :pages, :new
=> false
irb> w.ttl :pages, :show
=> 300
irb> w.ttl :pages, :index
=> 1200
irb> w.ttl :pages, :new
Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
but according to our wallet configuration, that action is not cached.
from ./lib/wallet/wallet.rb:54:in `ttl'
from (irb):6
48 49 50 51 52 53 54 |
# File 'lib/wallet/wallet.rb', line 48 def initialize(config_yml="") yml = YAML.load config_yml rescue nil @config = yml || {} @default_ttl = @config["default_ttl"] ? convert_time(@config["default_ttl"]) : 60 @config.delete "default_ttl" setup_action_caching end |
Instance Attribute Details
#config ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/wallet/wallet.rb', line 16 def config @config end |
#default_ttl ⇒ Object (readonly)
:nodoc:
17 18 19 |
# File 'lib/wallet/wallet.rb', line 17 def default_ttl @default_ttl end |
Instance Method Details
#cached?(controller, action) ⇒ Boolean
Returns true or false based on whether or not a controller action is configured for caching in the wallet
irb> w = Wallet.new "pages:\n show:"
irb> w.cached? :pages, :show
=> true
60 61 62 63 64 65 |
# File 'lib/wallet/wallet.rb', line 60 def cached?(controller, action) controller, action = stringify_params controller, action @config.has_key?(controller) && @config[controller].respond_to?(:has_key?) && @config[controller].has_key?(action) end |
#cached_actions(controller) ⇒ Object
67 68 69 |
# File 'lib/wallet/wallet.rb', line 67 def cached_actions(controller) @config[controller] ? @config[controller].keys : [] end |
#ttl(controller, action) ⇒ Object
Returns the value of the ttl for a controller / action. Throws an exception if the controller/action isn’t setup for caching.
irb> w = Wallet.new "pages:\n show:"
irb> w.cached? :pages, :show
=> true
irb> w.ttl :pages, :new
Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
but according to our wallet configuration, that action is not cached.
from ./lib/wallet/wallet.rb:54:in `ttl'
from (irb):6
81 82 83 84 85 86 87 88 89 |
# File 'lib/wallet/wallet.rb', line 81 def ttl(controller, action) raise ActionNotCached.new("You asked for the TTL for the '#{action}' action in the '#{controller}' controller, but according to our wallet configuration, that action is not cached.") unless cached?(controller, action) controller, action = stringify_params controller, action if @config[controller][action] convert_time @config[controller][action] else @default_ttl end end |