Skip to content

Commit

Permalink
reset and default_ttl features passing
Browse files Browse the repository at this point in the history
  • Loading branch information
moonmaster9000 committed Jun 13, 2011
1 parent 16bb35e commit f9e5d19
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm 1.8.7@wallet --create
98 changes: 98 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,98 @@
PATH
remote: .
specs:
wallet (1.0.0.beta.1)
rails (~> 3.0)

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.8)
actionpack (= 3.0.8)
mail (~> 2.2.19)
actionpack (3.0.8)
activemodel (= 3.0.8)
activesupport (= 3.0.8)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.5.0)
rack (~> 1.2.1)
rack-mount (~> 0.6.14)
rack-test (~> 0.5.7)
tzinfo (~> 0.3.23)
activemodel (3.0.8)
activesupport (= 3.0.8)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.8)
activemodel (= 3.0.8)
activesupport (= 3.0.8)
arel (~> 2.0.10)
tzinfo (~> 0.3.23)
activeresource (3.0.8)
activemodel (= 3.0.8)
activesupport (= 3.0.8)
activesupport (3.0.8)
arel (2.0.10)
builder (2.1.2)
cucumber (0.10.6)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
gherkin (~> 2.4.0)
json (>= 1.4.6)
term-ansicolor (>= 1.0.5)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
gherkin (2.4.0)
json (>= 1.4.6)
i18n (0.5.0)
json (1.5.1)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-test (0.5.7)
rack (>= 1.0)
rails (3.0.8)
actionmailer (= 3.0.8)
actionpack (= 3.0.8)
activerecord (= 3.0.8)
activeresource (= 3.0.8)
activesupport (= 3.0.8)
bundler (~> 1.0)
railties (= 3.0.8)
railties (3.0.8)
actionpack (= 3.0.8)
activesupport (= 3.0.8)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.9.2)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
term-ansicolor (1.0.5)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.27)

PLATFORMS
ruby

DEPENDENCIES
cucumber (~> 0.10)
rspec (~> 2.0)
wallet!
12 changes: 12 additions & 0 deletions features/default_ttl.feature
@@ -0,0 +1,12 @@
Feature:
As a programmer
I want a configurable TTL default
So that I can provide a default TTL for my action cache

Scenario: TTL defaults to 10 minutes
Given I have not configured Wallet
Then the default TTL should be 10 minutes

Scenario: Change the default TTL
When I set the default TTL to 20 minutes
Then all cache TTL's should default to 20 minutes
9 changes: 9 additions & 0 deletions features/reset.feature
@@ -0,0 +1,9 @@
Feature: Reset Wallet
As a programmer
I want a way to reset the Wallet configuration
So that I can easily test it

Scenario: resetting the Wallet back to it's factory settings
Given I have configured Wallet
When I reset Wallet
Then Wallet should return to it's default settings
14 changes: 14 additions & 0 deletions features/step_definitions/default_ttl_steps.rb
@@ -0,0 +1,14 @@
Given /^I have not configured Wallet$/ do
end

Then /^the default TTL should be (\d+) minutes$/ do |num|
Wallet.default_ttl.should == num.to_i.minutes
end

When /^I set the default TTL to (\d+) minutes$/ do |num|
Wallet.default_ttl = num.to_i.minutes
end

Then /^all cache TTL's should default to (\d+) minutes$/ do |num|
Wallet.default_ttl.should == num.to_i.minutes
end
11 changes: 11 additions & 0 deletions features/step_definitions/reset_steps.rb
@@ -0,0 +1,11 @@
Given /^I have configured Wallet$/ do
Wallet.default_ttl = 100.minutes
end

When /^I reset Wallet$/ do
Wallet.reset!
end

Then /^Wallet should return to it's default settings$/ do
Wallet.default_ttl.should == 10.minutes
end
3 changes: 3 additions & 0 deletions features/support/env.rb
@@ -0,0 +1,3 @@
$LOAD_PATH.unshift './lib'
require 'wallet'
require 'cucumber/rspec/doubles'
2 changes: 2 additions & 0 deletions lib/wallet.rb
@@ -0,0 +1,2 @@
require 'wallet/wallet'
require 'active_support/all'
12 changes: 12 additions & 0 deletions lib/wallet/wallet.rb
@@ -0,0 +1,12 @@
module Wallet
extend self
attr_accessor :default_ttl

def default_ttl
@default_ttl ||= 10.minutes
end

def reset!
@default_ttl = nil
end
end
84 changes: 84 additions & 0 deletions readme.markdown
@@ -0,0 +1,84 @@
# Wallet

Wallet, a simple ruby DSL for centralizing action cache configuration inside your Rails app.

## Installation

Wallet is a gem, available from http://rubygems.org.

Add the following to your Rails 3 app's Gemfile:

gem 'wallet', '~> 1.0'

## Usage

Let's imagine your app has a controller `HomeController`, and you want to action cache all of the
actions inside of it. First, create a `config/wallet.rb` inside your rails root, then open your `Wallet` and add some `cash`:

Wallet.open do
cash :home
end

Next, open your `ApplicationController`, and add the following into it:

class ApplicationController < ActionController::Base
include Wallet::Cash
cash!
end

The `cash!` method will setup action caching for your controllers (if they have any cache configuration in Wallet).

With this code in place, Rails will action cache every action inside your home controller for the default TTL (time-to-live) duration: 10 minutes. You can change the default ttl by passing a duration to the `default_ttl` method:

Wallet.default_ttl = 2.minutes

Now, suppose we have another controller, `ArticlesController`, and we want to cache the `show` and `index` actions on it for a duration of 20 minutes. Simple!

Wallet.open do
cash :articles, :for => 20.minutes do
show
index
end
end

This will cache only the `show` and `index` actions on the `ArticlesController`.

Now imagine we've added a third action, `comments`, to our `ArticlesController`, and we want to cache it for only 1 minute:

Wallet.open do
cash :articles, :for => 20.minutes do
show
index
comments 1.minute
end
end

## Before filters and action caching

Sometimes, even though an action is cached, you want a before filter to run before Rails try's to serve out the cached response. Authentication is one common use case. You can accomplish this with Wallet by simply calling `cash!` after the before filters:

class ApplicationController < ActionController::Base
include Wallet::Cash

before_filter :authentication
cash!

private
def authentication
#...
end
end

Now, the `authentication` method will run before your before filters.

## Engines

Wallet plays nice with engines. If you'd like to use Wallet inside your engine, create a `config/wallet.rb` file inside your engine, then include `Wallet::Railtie` inside your Engine railtie:

module Comments
class Engine < Rails::Engine
include Wallet::Railtie
end
end

Now, Rails will load the `config/wallet.rb` inside your engine before your controllers load.

0 comments on commit f9e5d19

Please sign in to comment.