wallet 0.0.5 → 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ Feature: Cash
2
+ As a programmer
3
+ I want a DSL for action caching
4
+ So that I can easily manage action caching inside a large application
5
+
6
+ Scenario: Cash actions in a controller for the default TTL
7
+ When I tell Wallet to `cash` an action on a controller
8
+ Then that action on that controller should cache for the default TTL
9
+
10
+ Scenario: Cash every configured action in a controller for a specific TTL
11
+ When I tell Wallet to `cash` any configured actions on a controller for a specific amount of time
12
+ Then any configured actions on that controller should cache for that amount of time
13
+
14
+ Scenario: Cash a single action in a controller for a spefic amount of time
15
+ When I tell Wallet to `cash` all configured actions in a controller for 20 minutes
16
+ And I tell Wallet to `cash` the `index` action for 30 minutes
17
+ Then all configured actions except for index on that controller should cache for 20 minutes
18
+ And the `index` action on that controller should cache for 30 minutes
@@ -0,0 +1,10 @@
1
+ Feature: Controller DSL
2
+ As a programmer
3
+ I want to be able to mix Wallet into my controller
4
+ so that I can cache actions on that controller
5
+
6
+ Scenario: Telling a controller to cache actions
7
+ Given I have setup Wallet to cache several actions in a controller
8
+ When I mix Wallet::Cash into that controller
9
+ And I call the `cash!` method on it
10
+ Then it should tell Rails to cache the appropriate actions
@@ -0,0 +1,12 @@
1
+ Feature:
2
+ As a programmer
3
+ I want a configurable TTL default
4
+ So that I can provide a default TTL for my action cache
5
+
6
+ Scenario: TTL defaults to 10 minutes
7
+ Given I have not configured Wallet
8
+ Then the default TTL should be 10 minutes
9
+
10
+ Scenario: Change the default TTL
11
+ When I set the default TTL to 20 minutes
12
+ Then all cache TTL's should default to 20 minutes
@@ -0,0 +1,9 @@
1
+ Feature: Reset Wallet
2
+ As a programmer
3
+ I want a way to reset the Wallet configuration
4
+ So that I can easily test it
5
+
6
+ Scenario: resetting the Wallet back to it's factory settings
7
+ Given I have configured Wallet
8
+ When I reset Wallet
9
+ Then Wallet should return to it's default settings
@@ -0,0 +1,45 @@
1
+ When /^I tell Wallet to `cash` an action on a controller$/ do
2
+ Wallet.open do
3
+ cash :home do
4
+ index
5
+ end
6
+ end
7
+ end
8
+
9
+ Then /^that action on that controller should cache for the default TTL$/ do
10
+ Wallet.home.index.should == Wallet.default_ttl
11
+ end
12
+
13
+ When /^I tell Wallet to `cash` any configured actions on a controller for a specific amount of time$/ do
14
+ Wallet.open do
15
+ cash :home, :for => 27.minutes do
16
+ index
17
+ end
18
+ end
19
+ end
20
+
21
+ Then /^any configured actions on that controller should cache for that amount of time$/ do
22
+ Wallet.home.index.should == 27.minutes
23
+ end
24
+
25
+ When /^I tell Wallet to `cash` all configured actions in a controller for (\d+) minutes$/ do |num|
26
+ Wallet.open do
27
+ cash :home, :for => 20.minutes do
28
+ show
29
+ print
30
+ index 30.minutes
31
+ end
32
+ end
33
+ end
34
+
35
+ When /^I tell Wallet to `cash` the `index` action for (\d+) minutes$/ do |arg1|
36
+ end
37
+
38
+ Then /^all configured actions except for index on that controller should cache for (\d+) minutes$/ do |arg1|
39
+ Wallet.home.show.should == 20.minutes
40
+ Wallet.home.print.should == 20.minutes
41
+ end
42
+
43
+ Then /^the `index` action on that controller should cache for (\d+) minutes$/ do |arg1|
44
+ Wallet.home.index.should == 30.minutes
45
+ end
@@ -0,0 +1,27 @@
1
+ Given /^I have setup Wallet to cache several actions in a controller$/ do
2
+ class HomeController
3
+ def self.caches_action(action_name, options)
4
+ end
5
+ end
6
+
7
+ Wallet.open do
8
+ cash :home do
9
+ index 2.minutes
10
+ end
11
+ end
12
+ end
13
+
14
+ When /^I mix Wallet::Cash into that controller$/ do
15
+ class HomeController
16
+ include Wallet::Cash
17
+ end
18
+ end
19
+
20
+ When /^I call the `cash!` method on it$/ do
21
+ @call = proc { HomeController.cash! }
22
+ end
23
+
24
+ Then /^it should tell Rails to cache the appropriate actions$/ do
25
+ HomeController.should_receive(:caches_action).with(:index, :expires_in => 2.minutes).and_return nil
26
+ @call.call
27
+ end
@@ -0,0 +1,14 @@
1
+ Given /^I have not configured Wallet$/ do
2
+ end
3
+
4
+ Then /^the default TTL should be (\d+) minutes$/ do |num|
5
+ Wallet.default_ttl.should == num.to_i.minutes
6
+ end
7
+
8
+ When /^I set the default TTL to (\d+) minutes$/ do |num|
9
+ Wallet.default_ttl = num.to_i.minutes
10
+ end
11
+
12
+ Then /^all cache TTL's should default to (\d+) minutes$/ do |num|
13
+ Wallet.default_ttl.should == num.to_i.minutes
14
+ end
@@ -0,0 +1,21 @@
1
+ Given /^I have configured Wallet$/ do
2
+ Wallet.default_ttl = 100.minutes
3
+
4
+ Wallet.open do
5
+ cash :home do
6
+ index
7
+ end
8
+ end
9
+
10
+ Wallet.default_ttl.should == 100.minutes
11
+ Wallet.home.index.should == Wallet.default_ttl
12
+ end
13
+
14
+ When /^I reset Wallet$/ do
15
+ Wallet.reset!
16
+ end
17
+
18
+ Then /^Wallet should return to it's default settings$/ do
19
+ Wallet.default_ttl.should == 10.minutes
20
+ Wallet.home.should be_nil
21
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'wallet'
3
+ require 'cucumber/rspec/doubles'
@@ -0,0 +1,3 @@
1
+ Before do
2
+ Wallet.reset!
3
+ end
data/lib/wallet.rb CHANGED
@@ -1 +1,7 @@
1
+ require 'active_support/all'
1
2
  require 'wallet/wallet'
3
+ require 'wallet/configuration'
4
+ require 'wallet/cached_actions'
5
+ require 'wallet/cash'
6
+ require 'wallet/railtie'
7
+ require 'wallet/engine_railtie'
@@ -0,0 +1,18 @@
1
+ module Wallet
2
+ module Configuration
3
+ class CachedActions
4
+ attr_reader :actions
5
+
6
+ def initialize(options={})
7
+ @actions = {}
8
+ @default_ttl = options[:default_ttl] || Wallet.default_ttl
9
+ end
10
+
11
+ def method_missing(action_name, *args, &block)
12
+ action = action_name.to_sym
13
+ @actions[action] = args.first.to_i if args.first
14
+ @actions[action] ||= @default_ttl
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module Wallet
2
+ module Cash
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def cash!
7
+ Wallet::Configuration.controllers.each do |controller_name, actions_configs|
8
+ controller = "#{controller_name}_controller".camelize.constantize
9
+ actions_configs.actions.each do |action_name, ttl|
10
+ controller.caches_action action_name, ttl
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module Wallet
2
+ module Configuration
3
+ extend self
4
+
5
+ def cash(controller, options={}, &block)
6
+ controllers[controller.to_sym] ||= Wallet::Configuration::CachedActions.new(
7
+ :default_ttl => (options[:for] || Wallet.default_ttl)
8
+ )
9
+ controllers[controller.to_sym].instance_eval &block
10
+ end
11
+
12
+ def controllers
13
+ @controllers ||= {}
14
+ end
15
+
16
+ def reset!
17
+ @controllers = {}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+
3
+ module Wallet
4
+ module Railtie
5
+ def self.included(base)
6
+ base.config.before_eager_load do
7
+ wallet = base.root + "/config/wallet.rb"
8
+ require wallet if File.exists?(wallet)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+
3
+ module Wallet
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ config.before_eager_load do
7
+ wallet = ::Rails.root.to_s + "/config/wallet.rb"
8
+ require wallet if File.exists?(wallet)
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/wallet/wallet.rb CHANGED
@@ -1,127 +1,21 @@
1
- # Author:: Matt Parker (mailto:moonmaster9000@gmail.com)
2
- # License:: Distributes under the same terms as Ruby
1
+ module Wallet
2
+ extend self
3
+ attr_accessor :default_ttl
3
4
 
4
- class Wallet
5
-
6
- class << self
7
- # Attempts to read and load the configuration file (RAILS_ROOT/config/wallet.yml by default)
8
- def load_from_config(config_file=nil)
9
- config_file = RAILS_ROOT + "/config/wallet.yml" unless config_file
10
- new((File.open(config_file) rescue ""))
11
- end
5
+ def default_ttl
6
+ @default_ttl ||= 10.minutes
12
7
  end
13
8
 
14
- # This error gets thrown whenever you attempt to retreive a TTL
15
- # for an action that's not in the wallet.
16
- class ActionNotCached < StandardError
9
+ def reset!
10
+ @default_ttl = nil
11
+ Configuration.reset!
17
12
  end
18
-
19
- class YamlError < StandardError; end
20
13
 
21
- class UnknownController < StandardError
14
+ def open(&block)
15
+ Configuration.instance_eval &block
22
16
  end
23
17
 
24
- attr_reader :config #:nodoc:
25
- attr_reader :default_ttl #:nodoc:
26
-
27
- # Pass in a yaml string to this method that represents your configuration.
28
- # Let's assume you have a PagesController with a show action and an index action,
29
- # and let's suppose you would like wallet to default your action cache ttl's to 5 hours.
30
- # Then your yaml might be formatted like so:
31
- # # /path/to/wallet.yml
32
- #
33
- # default_ttl: 5 hours
34
- #
35
- # pages:
36
- # show:
37
- # index: 20 minutes
38
- #
39
- # That would result in the following values:
40
- # irb> w = Wallet.new File.open("/path/to/wallet.yml")
41
- # irb> w.cached? :pages, :show
42
- # => true
43
- # irb> w.cached? :pages, :index
44
- # => true
45
- # irb> w.cached? :pages, :new
46
- # => false
47
- # irb> w.ttl :pages, :show
48
- # => 300
49
- # irb> w.ttl :pages, :index
50
- # => 1200
51
- # irb> w.ttl :pages, :new
52
- # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
53
- # but according to our wallet configuration, that action is not cached.
54
- # from ./lib/wallet/wallet.rb:54:in `ttl'
55
- # from (irb):6
56
- def initialize(config_yml="")
57
- yml = YAML.load config_yml rescue nil
58
- @config = yml || {}
59
- @default_ttl = @config["default_ttl"] ? convert_time(@config["default_ttl"]) : 60
60
- @config.delete "default_ttl"
61
- setup_action_caching
62
- end
63
-
64
- # Returns true or false based on whether or not a controller action is configured for caching in the wallet
65
- # irb> w = Wallet.new "pages:\n show:"
66
- # irb> w.cached? :pages, :show
67
- # => true
68
- def cached?(controller, action)
69
- controller, action = stringify_params controller, action
70
- @config.has_key?(controller) &&
71
- @config[controller].respond_to?(:has_key?) &&
72
- @config[controller].has_key?(action)
73
- end
74
-
75
- def cached_actions(controller)
76
- @config[controller] ? @config[controller].keys : []
77
- end
78
-
79
- # Returns the value of the ttl for a controller / action. Throws an exception if the controller/action isn't setup
80
- # for caching.
81
- # irb> w = Wallet.new "pages:\n show:"
82
- # irb> w.cached? :pages, :show
83
- # => true
84
- # irb> w.ttl :pages, :new
85
- # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
86
- # but according to our wallet configuration, that action is not cached.
87
- # from ./lib/wallet/wallet.rb:54:in `ttl'
88
- # from (irb):6
89
- def ttl(controller, action)
90
- 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)
91
- controller, action = stringify_params controller, action
92
- if @config[controller][action]
93
- convert_time @config[controller][action]
94
- else
95
- @default_ttl
96
- end
97
- end
98
-
99
- private
100
- def convert_time(time)
101
- if !(time =~ /^\ *\d+(?:\ +|\.)(?:hours?|minutes?|days?|seconds?)\ *$/)
102
- raise YamlError.new(
103
- "'#{time}' is not a valid time phrase. Valid phrases are a number followed by " +
104
- "a space followed by 'hours' or 'minutes' or 'days' (or the singular versions of those)."
105
- )
106
- else
107
- time = time.strip.gsub(/\ +/, '.')
108
- eval time
109
- end
110
- end
111
-
112
- def stringify_params(*args)
113
- args.map! {|a| a.to_s}
114
- return *args
115
- end
116
-
117
- def setup_action_caching
118
- @config.each do |controller, actions|
119
- controller_class = (controller + "_controller").camelize.constantize rescue nil
120
- if controller_class
121
- cached_actions(controller).each do |action|
122
- controller_class.send :caches_action, action.to_sym, :expires_in => ttl(controller, action)
123
- end
124
- end
125
- end
18
+ def method_missing(method_name, *args, &block)
19
+ Configuration.controllers[method_name.to_sym]
126
20
  end
127
21
  end
data/readme.markdown ADDED
@@ -0,0 +1,85 @@
1
+ # Wallet
2
+
3
+ Wallet, a simple ruby DSL for centralizing action cache configuration inside your Rails app.
4
+
5
+ ## Installation
6
+
7
+ Wallet is a gem, available from http://rubygems.org.
8
+
9
+ Add the following to your Rails 3 app's Gemfile:
10
+
11
+ gem 'wallet', '~> 1.0.0.beta.1'
12
+
13
+ ## Usage
14
+
15
+ Let's imagine your app has a controller `HomeController`, and you want to action cache the index action inside of it. First, create a `config/wallet.rb` inside your rails root, then open your `Wallet` and add some `cash`:
16
+
17
+ Wallet.open do
18
+ cash :home do
19
+ index
20
+ end
21
+ end
22
+
23
+ Next, open your `ApplicationController`, and add the following into it:
24
+
25
+ class ApplicationController < ActionController::Base
26
+ include Wallet::Cash
27
+ cash!
28
+ end
29
+
30
+ The `cash!` method will setup action caching for your controllers (if they have any cache configuration in Wallet).
31
+
32
+ With this code in place, Rails will action cache the `index` 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:
33
+
34
+ Wallet.default_ttl = 2.minutes
35
+
36
+ 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!
37
+
38
+ Wallet.open do
39
+ cash :articles, :for => 20.minutes do
40
+ show
41
+ index
42
+ end
43
+ end
44
+
45
+ This will cache only the `show` and `index` actions on the `ArticlesController`.
46
+
47
+ Now imagine we've added a third action, `comments`, to our `ArticlesController`, and we want to cache it for only 1 minute:
48
+
49
+ Wallet.open do
50
+ cash :articles, :for => 20.minutes do
51
+ show
52
+ index
53
+ comments 1.minute
54
+ end
55
+ end
56
+
57
+ ## Before filters and action caching
58
+
59
+ 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:
60
+
61
+ class ApplicationController < ActionController::Base
62
+ include Wallet::Cash
63
+
64
+ before_filter :authentication
65
+ cash!
66
+
67
+ private
68
+ def authentication
69
+ #...
70
+ end
71
+ end
72
+
73
+ Now, the `authentication` method will run before your before filters.
74
+
75
+ ## Engines
76
+
77
+ 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:
78
+
79
+ module Comments
80
+ class Engine < Rails::Engine
81
+ include Wallet::Railtie
82
+ end
83
+ end
84
+
85
+ Now, Rails will load the `config/wallet.rb` inside your engine before your controllers load.
data/wallet.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ # METADATA
3
+ s.name = "wallet"
4
+ s.version = File.read "VERSION"
5
+ s.summary = "Simple action-cache configuration management for Rails 3."
6
+ s.description = "A simple DSL for configuring action caching inside your rails app."
7
+ s.authors = "Matt Parker"
8
+ s.email = "moonmaster9000@gmail.com"
9
+ s.homepage = "http://github.com/moonmaster9000/wallet"
10
+
11
+ # FILES PACKAGED WITH THIS GEM
12
+ s.files = Dir.glob("lib/**/*") << "readme.markdown" << "wallet.gemspec"
13
+ s.test_files = Dir.glob "features/**/*"
14
+
15
+ # DEPENDENCIES
16
+ s.add_dependency "rails", "~> 3.0"
17
+ s.add_development_dependency "cucumber", "~> 0.10"
18
+ s.add_development_dependency "rspec", "~> 2.0"
19
+ end
metadata CHANGED
@@ -1,12 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallet
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 62196353
5
+ prerelease: 6
5
6
  segments:
7
+ - 1
6
8
  - 0
7
9
  - 0
8
- - 5
9
- version: 0.0.5
10
+ - beta
11
+ - 1
12
+ version: 1.0.0.beta.1
10
13
  platform: ruby
11
14
  authors:
12
15
  - Matt Parker
@@ -14,65 +17,124 @@ autorequire:
14
17
  bindir: bin
15
18
  cert_chain: []
16
19
 
17
- date: 2010-04-05 00:00:00 -04:00
18
- default_executable:
20
+ date: 2011-06-13 00:00:00 Z
19
21
  dependencies:
20
22
  - !ruby/object:Gem::Dependency
21
23
  name: rails
22
24
  prerelease: false
23
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
24
27
  requirements:
25
- - - ">="
28
+ - - ~>
26
29
  - !ruby/object:Gem::Version
30
+ hash: 7
27
31
  segments:
28
- - 2
29
- - 3
30
32
  - 3
31
- version: 2.3.3
33
+ - 0
34
+ version: "3.0"
32
35
  type: :runtime
33
36
  version_requirements: *id001
34
- description: Wallet is a rails gem that allows you to manage all of your action caching configuration in a single yaml file. Supports TTLs.
37
+ - !ruby/object:Gem::Dependency
38
+ name: cucumber
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 0
48
+ - 10
49
+ version: "0.10"
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 2
63
+ - 0
64
+ version: "2.0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: A simple DSL for configuring action caching inside your rails app.
35
68
  email: moonmaster9000@gmail.com
36
69
  executables: []
37
70
 
38
71
  extensions: []
39
72
 
40
- extra_rdoc_files:
41
- - README.rdoc
73
+ extra_rdoc_files: []
74
+
42
75
  files:
43
- - README.rdoc
44
- - lib/wallet.rb
76
+ - lib/wallet/cached_actions.rb
77
+ - lib/wallet/cash.rb
78
+ - lib/wallet/configuration.rb
79
+ - lib/wallet/engine_railtie.rb
80
+ - lib/wallet/railtie.rb
45
81
  - lib/wallet/wallet.rb
46
- has_rdoc: true
82
+ - lib/wallet.rb
83
+ - readme.markdown
84
+ - wallet.gemspec
85
+ - features/cash.feature
86
+ - features/controller.feature
87
+ - features/default_ttl.feature
88
+ - features/reset.feature
89
+ - features/step_definitions/cash_steps.rb
90
+ - features/step_definitions/controller_steps.rb
91
+ - features/step_definitions/default_ttl_steps.rb
92
+ - features/step_definitions/reset_steps.rb
93
+ - features/support/env.rb
94
+ - features/support/hooks.rb
47
95
  homepage: http://github.com/moonmaster9000/wallet
48
96
  licenses: []
49
97
 
50
98
  post_install_message:
51
- rdoc_options:
52
- - --charset=UTF-8
99
+ rdoc_options: []
100
+
53
101
  require_paths:
54
102
  - lib
55
103
  required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
56
105
  requirements:
57
106
  - - ">="
58
107
  - !ruby/object:Gem::Version
108
+ hash: 3
59
109
  segments:
60
110
  - 0
61
111
  version: "0"
62
112
  required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
63
114
  requirements:
64
- - - ">="
115
+ - - ">"
65
116
  - !ruby/object:Gem::Version
117
+ hash: 25
66
118
  segments:
67
- - 0
68
- version: "0"
119
+ - 1
120
+ - 3
121
+ - 1
122
+ version: 1.3.1
69
123
  requirements: []
70
124
 
71
125
  rubyforge_project:
72
- rubygems_version: 1.3.6
126
+ rubygems_version: 1.8.5
73
127
  signing_key:
74
128
  specification_version: 3
75
- summary: Centralized YAML configuration for action caching.
129
+ summary: Simple action-cache configuration management for Rails 3.
76
130
  test_files:
77
- - spec/lib_specs/wallet_spec.rb
78
- - spec/spec_helper.rb
131
+ - features/cash.feature
132
+ - features/controller.feature
133
+ - features/default_ttl.feature
134
+ - features/reset.feature
135
+ - features/step_definitions/cash_steps.rb
136
+ - features/step_definitions/controller_steps.rb
137
+ - features/step_definitions/default_ttl_steps.rb
138
+ - features/step_definitions/reset_steps.rb
139
+ - features/support/env.rb
140
+ - features/support/hooks.rb
data/README.rdoc DELETED
@@ -1,59 +0,0 @@
1
- ==Introduction
2
-
3
- Wallet is a rails gem that allows you to manage all of your action caching configuration in a single yaml file. It supports TTLs.
4
-
5
- ==Motivation
6
-
7
- For a large project, it's nice from a systems administration point of view to be able to quickly manage caching in a single place.
8
- Having a central YAML file for managing action caching also opens up the possibility of creating backend web interfaces for your application
9
- that could allow site administrators of your app to manage the TTL's from anywhere.
10
-
11
-
12
- ==Installation
13
-
14
- To add wallet to your RAILS app, add a config.gem line like the following to the Rails::Initializer section of your environment.rb:
15
-
16
- config.gem "wallet", :lib => 'wallet', :version => '>=0.0.3', :source => "http://gemcutter.org"
17
-
18
- Then install the missing gem:
19
-
20
- bash$ sudo rake gems:install
21
-
22
- Lastly, create an initializer that conditionally loads wallet (e.g., RAILS_ROOT/initializers/wallet.rb)
23
-
24
- Wallet.load_from_config # will load from RAILS_ROOT/config/wallet.yml by default.
25
-
26
- ==Usage
27
-
28
- Once it's installed, you can create a "wallet.yml" file in your RAILS_ROOT/config directory to manage action caching for your application.
29
- Suppose you have a controller "ChannelsController" with actions "cartoon_network" and "current_tv". At it's simplest, you could put
30
- the following in your "wallet.yml":
31
-
32
- # RAILS_ROOT/config/wallet.yml
33
-
34
- channels:
35
- cartoon_network:
36
- current_tv:
37
-
38
- Wallet would read this file and setup action caching for the cartoon_network and current_tv actions; it would default the TTL
39
- on those actions to 60 seconds. If you wanted a different default, you could add a special "default_ttl:" entry to your "wallet.yml":
40
-
41
- # RAILS_ROOT/config/wallet.yml
42
-
43
- default_ttl: 30 minutes
44
-
45
- channels:
46
- cartoon_network:
47
- current_tv:
48
-
49
- Noticed that we simple specified "30 minutes". The format of the ttls is "\d\ (minutes?|days?|hours?)".
50
-
51
- Lastly, suppose you wanted current_tv to cache for 5 hours, but cartoon_network to still use the default:
52
-
53
- # RAILS_ROOT/config/wallet.yml
54
-
55
- default_ttl: 30 minutes
56
-
57
- channels:
58
- cartoon_network:
59
- current_tv: 5 hours
@@ -1,84 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe Wallet do
4
- describe "Wallet::new" do
5
- it "should load the config on initialization if provided with a path to a yaml file" do
6
- YAML.should_receive(:load).once
7
- wallet = Wallet.new("channels:\n adult_swim:")
8
- wallet.config.should_not == false
9
- end
10
-
11
- it "should not load the wallet.yml file on initialization if no file is provided" do
12
- YAML.should_receive(:load).once
13
- wallet = Wallet.new
14
- wallet.config.should be_empty
15
- end
16
-
17
- it "should set the default_ttl to 60 if no default_ttl: is provided in the config yml" do
18
- wallet = Wallet.new
19
- wallet.default_ttl.should == 60
20
- end
21
-
22
- it "should set the default_ttl to 300 if the default_ttl is set to 5.hours in the config yml" do
23
- wallet = Wallet.new "default_ttl: 5.hours"
24
- wallet.default_ttl.should == 5.hours.to_i
25
- end
26
-
27
- it "should set the default_ttl to 300 if the default_ttl is set to '5 hours' in the config yml" do
28
- wallet = Wallet.new "default_ttl: 5 hours"
29
- wallet.default_ttl.should == 5.hours.to_i
30
- end
31
-
32
- it "should throw an exception if i make a mistake with my time format" do
33
- proc {Wallet.new "default_ttl: 5.hurs"}.should raise_error(Wallet::YamlError)
34
- end
35
- end
36
-
37
- describe "Wallet::cached?" do
38
- before do
39
- config_yml = "channels:\n cartoon_network:\n"
40
- @wallet = Wallet.new config_yml
41
- end
42
-
43
- it "should return true for a controller/action that is cached" do
44
- @wallet.cached?("channels", "cartoon_network").should == true
45
- end
46
-
47
- it "should return false for a controller/action that is not cached" do
48
- @wallet.cached?("blah", "blah").should == false
49
- @wallet.cached?("channels", "blah").should == false
50
- end
51
-
52
- it "should work with both strings and symbols" do
53
- @wallet.cached?("channels", "cartoon_network").should == true
54
- @wallet.cached?(:channels, :cartoon_network).should == true
55
- end
56
- end
57
-
58
- describe "Wallet::ttl" do
59
- before do
60
- config_yml = "channels:\n cartoon_network: 5 hours\n current_tv:\n hbo: 2.minutes\n pbs: 60 days"
61
- @wallet = Wallet.new config_yml
62
- end
63
-
64
- it "should return 5 minutes for the channels:cartoon_network ttl" do
65
- @wallet.ttl(:channels, :cartoon_network).should == 5.hours.to_i
66
- end
67
-
68
- it "should return the default ttl for the current_tv action" do
69
- @wallet.ttl(:channels, :current_tv).should == @wallet.default_ttl
70
- end
71
-
72
- it "should return 2 minutes for the channels:hbo ttl" do
73
- @wallet.ttl(:channels, :hbo).should == 2.minutes.to_i
74
- end
75
-
76
- it "should return 60 days for the pbs channel ttl" do
77
- @wallet.ttl(:channels, :pbs).should == 60.days.to_i
78
- end
79
-
80
- it "should raise a ActionNotCached error for the dne action" do
81
- proc {@wallet.ttl(:channels, :dne)}.should raise_error(Wallet::ActionNotCached)
82
- end
83
- end
84
- end
data/spec/spec_helper.rb DELETED
@@ -1,3 +0,0 @@
1
- $LOAD_PATH << './lib'
2
- require 'active_support'
3
- require 'wallet'