diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..fb5e503 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm 1.8.7@wallet --create diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..d9a668d --- /dev/null +++ b/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! diff --git a/features/default_ttl.feature b/features/default_ttl.feature new file mode 100644 index 0000000..2982a67 --- /dev/null +++ b/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 diff --git a/features/reset.feature b/features/reset.feature new file mode 100644 index 0000000..897f593 --- /dev/null +++ b/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 diff --git a/features/step_definitions/default_ttl_steps.rb b/features/step_definitions/default_ttl_steps.rb new file mode 100644 index 0000000..1b25608 --- /dev/null +++ b/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 diff --git a/features/step_definitions/reset_steps.rb b/features/step_definitions/reset_steps.rb new file mode 100644 index 0000000..27d209e --- /dev/null +++ b/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 diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..74ab8e6 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,3 @@ +$LOAD_PATH.unshift './lib' +require 'wallet' +require 'cucumber/rspec/doubles' diff --git a/lib/wallet.rb b/lib/wallet.rb new file mode 100644 index 0000000..abc874f --- /dev/null +++ b/lib/wallet.rb @@ -0,0 +1,2 @@ +require 'wallet/wallet' +require 'active_support/all' diff --git a/lib/wallet/wallet.rb b/lib/wallet/wallet.rb new file mode 100644 index 0000000..78626ea --- /dev/null +++ b/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 diff --git a/readme.markdown b/readme.markdown new file mode 100644 index 0000000..d6ce958 --- /dev/null +++ b/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.