wallet 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1,56 @@
1
- Wallet is a rails gem that allows you to manage all of your action caching configuration in a single yaml file. It supports TTL.
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.2', :source => "http://gemcutter.org"
17
+
18
+ Then install the missing gem:
19
+
20
+ bash$ sudo rake gems:install
21
+
22
+
23
+ ==Usage
24
+
25
+ Once it's installed, you can create a "wallet.yml" file in your RAILS_ROOT/config directory to manage action caching for your application.
26
+ Suppose you have a controller "ChannelsController" with actions "cartoon_network" and "current_tv". At it's simplest, you could put
27
+ the following in your "wallet.yml":
28
+
29
+ # RAILS_ROOT/config/wallet.yml
30
+
31
+ channels:
32
+ cartoon_network:
33
+ current_tv:
34
+
35
+ Wallet would read this file and setup action caching for the cartoon_network and current_tv actions; it would default the TTL
36
+ on those actions to 60 seconds. If you wanted a different default, you could add a special "default_ttl:" entry to your "wallet.yml":
37
+
38
+ # RAILS_ROOT/config/wallet.yml
39
+
40
+ default_ttl: 30 minutes
41
+
42
+ channels:
43
+ cartoon_network:
44
+ current_tv:
45
+
46
+ Noticed that we simple specified "30 minutes". The format of the ttls is "\d\ (minutes?|days?|hours?)".
47
+
48
+ Lastly, suppose you wanted current_tv to cache for 5 hours, but cartoon_network to still use the default:
49
+
50
+ # RAILS_ROOT/config/wallet.yml
51
+
52
+ default_ttl: 30 minutes
53
+
54
+ channels:
55
+ cartoon_network:
56
+ current_tv: 5 hours
@@ -1,3 +1,2 @@
1
1
  require 'wallet/wallet'
2
- require 'wallet/action_controller'
3
2
  Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue "")) if defined?(RAILS_ROOT)
@@ -7,6 +7,8 @@ class Wallet
7
7
  # for an action that's not in the wallet.
8
8
  class ActionNotCached < StandardError
9
9
  end
10
+
11
+ class YamlError < StandardError; end
10
12
 
11
13
  class UnknownController < StandardError
12
14
  end
@@ -20,11 +22,11 @@ class Wallet
20
22
  # Then your yaml might be formatted like so:
21
23
  # # /path/to/wallet.yml
22
24
  #
23
- # default_ttl: 5.hours
25
+ # default_ttl: 5 hours
24
26
  #
25
27
  # pages:
26
28
  # show:
27
- # index: 20.minutes
29
+ # index: 20 minutes
28
30
  #
29
31
  # That would result in the following values:
30
32
  # irb> w = Wallet.new File.open("/path/to/wallet.yml")
@@ -46,7 +48,7 @@ class Wallet
46
48
  def initialize(config_yml="")
47
49
  yml = YAML.load config_yml rescue nil
48
50
  @config = yml || {}
49
- @default_ttl = (eval(@config["default_ttl"]).to_i || 60) rescue 60
51
+ @default_ttl = @config["default_ttl"] ? convert_time(@config["default_ttl"]) : 60
50
52
  @config.delete "default_ttl"
51
53
  setup_action_caching
52
54
  end
@@ -80,13 +82,25 @@ class Wallet
80
82
  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)
81
83
  controller, action = stringify_params controller, action
82
84
  if @config[controller][action]
83
- (eval(@config[controller][action]).to_i || @default_ttl) rescue @default_ttl
85
+ convert_time @config[controller][action]
84
86
  else
85
87
  @default_ttl
86
88
  end
87
89
  end
88
90
 
89
91
  private
92
+ def convert_time(time)
93
+ if !(time =~ /\d+(?:\ +|\.)(?:hours?|minutes?|days?)/)
94
+ raise YamlError.new(
95
+ "'#{time}' is not a valid time phrase. Valid phrases are a number followed by " +
96
+ "a space followed by 'hours' or 'minutes' or 'days' (or the singular versions of those)."
97
+ )
98
+ else
99
+ time = time.strip.gsub(/\ +/, '.')
100
+ eval time
101
+ end
102
+ end
103
+
90
104
  def stringify_params(*args)
91
105
  args.map! {|a| a.to_s}
92
106
  return *args
@@ -23,10 +23,14 @@ describe Wallet do
23
23
  wallet = Wallet.new "default_ttl: 5.hours"
24
24
  wallet.default_ttl.should == 5.hours.to_i
25
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
26
31
 
27
- it "should set the default_ttl to 60 seconds if i make a mistake in my default_ttl entry in the config yml" do
28
- wallet = Wallet.new "default_ttl: 5.hurs"
29
- wallet.default_ttl.should == 60
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)
30
34
  end
31
35
  end
32
36
 
@@ -53,17 +57,25 @@ describe Wallet do
53
57
 
54
58
  describe "Wallet::ttl" do
55
59
  before do
56
- config_yml = "channels:\n cartoon_network: 5.minute\n current_tv: "
60
+ config_yml = "channels:\n cartoon_network: 5 hours\n current_tv:\n hbo: 2.minutes\n pbs: 60 days"
57
61
  @wallet = Wallet.new config_yml
58
62
  end
59
63
 
60
64
  it "should return 5 minutes for the channels:cartoon_network ttl" do
61
- @wallet.ttl(:channels, :cartoon_network).should == 5.minutes.to_i
65
+ @wallet.ttl(:channels, :cartoon_network).should == 5.hours.to_i
62
66
  end
63
67
 
64
68
  it "should return the default ttl for the current_tv action" do
65
69
  @wallet.ttl(:channels, :current_tv).should == @wallet.default_ttl
66
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
67
79
 
68
80
  it "should raise a ActionNotCached error for the dne action" do
69
81
  proc {@wallet.ttl(:channels, :dne)}.should raise_error(Wallet::ActionNotCached)
@@ -1,3 +1,3 @@
1
1
  $LOAD_PATH << './lib'
2
- require 'activesupport'
2
+ require 'active_support'
3
3
  require 'wallet'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Parker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-21 00:00:00 -04:00
12
+ date: 2009-12-02 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -33,7 +33,6 @@ extra_rdoc_files:
33
33
  files:
34
34
  - README.rdoc
35
35
  - lib/wallet.rb
36
- - lib/wallet/action_controller.rb
37
36
  - lib/wallet/wallet.rb
38
37
  has_rdoc: true
39
38
  homepage: http://github.com/moonmaster9000/wallet
@@ -66,20 +65,3 @@ summary: Centralized YAML configuration for action caching.
66
65
  test_files:
67
66
  - spec/lib_specs/wallet_spec.rb
68
67
  - spec/spec_helper.rb
69
- - examples/tv/app/controllers/application_controller.rb
70
- - examples/tv/app/controllers/channels_controller.rb
71
- - examples/tv/app/helpers/application_helper.rb
72
- - examples/tv/config/boot.rb
73
- - examples/tv/config/environment.rb
74
- - examples/tv/config/environments/development.rb
75
- - examples/tv/config/environments/production.rb
76
- - examples/tv/config/environments/test.rb
77
- - examples/tv/config/initializers/backtrace_silencers.rb
78
- - examples/tv/config/initializers/inflections.rb
79
- - examples/tv/config/initializers/mime_types.rb
80
- - examples/tv/config/initializers/new_rails_defaults.rb
81
- - examples/tv/config/initializers/session_store.rb
82
- - examples/tv/config/routes.rb
83
- - examples/tv/lib/wallet.rb
84
- - examples/tv/test/performance/browsing_test.rb
85
- - examples/tv/test/test_helper.rb
@@ -1,10 +0,0 @@
1
- # Filters added to this controller apply to all controllers in the application.
2
- # Likewise, all the methods added will be available for all controllers.
3
-
4
- class ApplicationController < ActionController::Base
5
- helper :all # include all helpers, all the time
6
- protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
-
8
- # Scrub sensitive parameters from your log
9
- # filter_parameter_logging :password
10
- end
@@ -1,11 +0,0 @@
1
- class ChannelsController < ApplicationController
2
-
3
- caches_action :cartoon_network, :expires_in => 10.seconds
4
-
5
- def cartoon_network
6
- end
7
-
8
- def current
9
- end
10
-
11
- end
@@ -1,3 +0,0 @@
1
- # Methods added to this helper will be available to all templates in the application.
2
- module ApplicationHelper
3
- end
@@ -1,110 +0,0 @@
1
- # Don't change this file!
2
- # Configure your app in config/environment.rb and config/environments/*.rb
3
-
4
- RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
-
6
- module Rails
7
- class << self
8
- def boot!
9
- unless booted?
10
- preinitialize
11
- pick_boot.run
12
- end
13
- end
14
-
15
- def booted?
16
- defined? Rails::Initializer
17
- end
18
-
19
- def pick_boot
20
- (vendor_rails? ? VendorBoot : GemBoot).new
21
- end
22
-
23
- def vendor_rails?
24
- File.exist?("#{RAILS_ROOT}/vendor/rails")
25
- end
26
-
27
- def preinitialize
28
- load(preinitializer_path) if File.exist?(preinitializer_path)
29
- end
30
-
31
- def preinitializer_path
32
- "#{RAILS_ROOT}/config/preinitializer.rb"
33
- end
34
- end
35
-
36
- class Boot
37
- def run
38
- load_initializer
39
- Rails::Initializer.run(:set_load_path)
40
- end
41
- end
42
-
43
- class VendorBoot < Boot
44
- def load_initializer
45
- require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
- Rails::Initializer.run(:install_gem_spec_stubs)
47
- Rails::GemDependency.add_frozen_gem_path
48
- end
49
- end
50
-
51
- class GemBoot < Boot
52
- def load_initializer
53
- self.class.load_rubygems
54
- load_rails_gem
55
- require 'initializer'
56
- end
57
-
58
- def load_rails_gem
59
- if version = self.class.gem_version
60
- gem 'rails', version
61
- else
62
- gem 'rails'
63
- end
64
- rescue Gem::LoadError => load_error
65
- $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
- exit 1
67
- end
68
-
69
- class << self
70
- def rubygems_version
71
- Gem::RubyGemsVersion rescue nil
72
- end
73
-
74
- def gem_version
75
- if defined? RAILS_GEM_VERSION
76
- RAILS_GEM_VERSION
77
- elsif ENV.include?('RAILS_GEM_VERSION')
78
- ENV['RAILS_GEM_VERSION']
79
- else
80
- parse_gem_version(read_environment_rb)
81
- end
82
- end
83
-
84
- def load_rubygems
85
- require 'rubygems'
86
- min_version = '1.3.1'
87
- unless rubygems_version >= min_version
88
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
- exit 1
90
- end
91
-
92
- rescue LoadError
93
- $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
- exit 1
95
- end
96
-
97
- def parse_gem_version(text)
98
- $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
- end
100
-
101
- private
102
- def read_environment_rb
103
- File.read("#{RAILS_ROOT}/config/environment.rb")
104
- end
105
- end
106
- end
107
- end
108
-
109
- # All that for this:
110
- Rails.boot!
@@ -1,12 +0,0 @@
1
- # Be sure to restart your server when you modify this file
2
-
3
- # Specifies gem version of Rails to use when vendor/rails is not present
4
- RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION
5
-
6
- # Bootstrap the Rails environment, frameworks, and default configuration
7
- require File.join(File.dirname(__FILE__), 'boot')
8
-
9
- Rails::Initializer.run do |config|
10
- config.gem "wallet", :lib => 'wallet', :version => '>=0.0.1', :source => "http://gemcutter.org"
11
- config.time_zone = 'UTC'
12
- end
@@ -1,17 +0,0 @@
1
- # Settings specified here will take precedence over those in config/environment.rb
2
-
3
- # In the development environment your application's code is reloaded on
4
- # every request. This slows down response time but is perfect for development
5
- # since you don't have to restart the webserver when you make code changes.
6
- config.cache_classes = false
7
-
8
- # Log error messages when you accidentally call methods on nil.
9
- config.whiny_nils = true
10
-
11
- # Show full error reports and disable caching
12
- config.action_controller.consider_all_requests_local = true
13
- config.action_view.debug_rjs = true
14
- config.action_controller.perform_caching = false
15
-
16
- # Don't care if the mailer can't send
17
- config.action_mailer.raise_delivery_errors = false
@@ -1,28 +0,0 @@
1
- # Settings specified here will take precedence over those in config/environment.rb
2
-
3
- # The production environment is meant for finished, "live" apps.
4
- # Code is not reloaded between requests
5
- config.cache_classes = true
6
-
7
- # Full error reports are disabled and caching is turned on
8
- config.action_controller.consider_all_requests_local = false
9
- config.action_controller.perform_caching = true
10
- config.action_view.cache_template_loading = true
11
-
12
- # See everything in the log (default is :info)
13
- # config.log_level = :debug
14
-
15
- # Use a different logger for distributed setups
16
- # config.logger = SyslogLogger.new
17
-
18
- # Use a different cache store in production
19
- config.cache_store = :mem_cache_store
20
-
21
- # Enable serving of images, stylesheets, and javascripts from an asset server
22
- # config.action_controller.asset_host = "http://assets.example.com"
23
-
24
- # Disable delivery errors, bad email addresses will be ignored
25
- # config.action_mailer.raise_delivery_errors = false
26
-
27
- # Enable threaded mode
28
- # config.threadsafe!
@@ -1,28 +0,0 @@
1
- # Settings specified here will take precedence over those in config/environment.rb
2
-
3
- # The test environment is used exclusively to run your application's
4
- # test suite. You never need to work with it otherwise. Remember that
5
- # your test database is "scratch space" for the test suite and is wiped
6
- # and recreated between test runs. Don't rely on the data there!
7
- config.cache_classes = true
8
-
9
- # Log error messages when you accidentally call methods on nil.
10
- config.whiny_nils = true
11
-
12
- # Show full error reports and disable caching
13
- config.action_controller.consider_all_requests_local = true
14
- config.action_controller.perform_caching = false
15
- config.action_view.cache_template_loading = true
16
-
17
- # Disable request forgery protection in test environment
18
- config.action_controller.allow_forgery_protection = false
19
-
20
- # Tell Action Mailer not to deliver emails to the real world.
21
- # The :test delivery method accumulates sent emails in the
22
- # ActionMailer::Base.deliveries array.
23
- config.action_mailer.delivery_method = :test
24
-
25
- # Use SQL instead of Active Record's schema dumper when creating the test database.
26
- # This is necessary if your schema can't be completely dumped by the schema dumper,
27
- # like if you have constraints or database-specific column types
28
- # config.active_record.schema_format = :sql
@@ -1,7 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
- # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
-
6
- # You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
7
- # Rails.backtrace_cleaner.remove_silencers!
@@ -1,10 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Add new inflection rules using the following format
4
- # (all these examples are active by default):
5
- # ActiveSupport::Inflector.inflections do |inflect|
6
- # inflect.plural /^(ox)$/i, '\1en'
7
- # inflect.singular /^(ox)en/i, '\1'
8
- # inflect.irregular 'person', 'people'
9
- # inflect.uncountable %w( fish sheep )
10
- # end
@@ -1,5 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Add new mime types for use in respond_to blocks:
4
- # Mime::Type.register "text/richtext", :rtf
5
- # Mime::Type.register_alias "text/html", :iphone
@@ -1,19 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # These settings change the behavior of Rails 2 apps and will be defaults
4
- # for Rails 3. You can remove this initializer when Rails 3 is released.
5
-
6
- if defined?(ActiveRecord)
7
- # Include Active Record class name as root for JSON serialized output.
8
- ActiveRecord::Base.include_root_in_json = true
9
-
10
- # Store the full class name (including module namespace) in STI type column.
11
- ActiveRecord::Base.store_full_sti_class = true
12
- end
13
-
14
- # Use ISO 8601 format for JSON serialized times and dates.
15
- ActiveSupport.use_standard_json_time_format = true
16
-
17
- # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
18
- # if you're including raw json in an HTML page.
19
- ActiveSupport.escape_html_entities_in_json = false
@@ -1,15 +0,0 @@
1
- # Be sure to restart your server when you modify this file.
2
-
3
- # Your secret key for verifying cookie session data integrity.
4
- # If you change this key, all old sessions will become invalid!
5
- # Make sure the secret is at least 30 characters and all random,
6
- # no regular words or you'll be exposed to dictionary attacks.
7
- ActionController::Base.session = {
8
- :key => '_tv_session',
9
- :secret => '91c1045d111179c9c4f12bea2f0539bd547c4e313f9ac11d0b737b60257666d0be6aea4542e620bc51d55635e98d2e1b2c46bdb308ffe3b73fd8fe8d59a811bc'
10
- }
11
-
12
- # Use the database for sessions instead of the cookie-based default,
13
- # which shouldn't be used to store highly confidential information
14
- # (create the session table with "rake db:sessions:create")
15
- # ActionController::Base.session_store = :active_record_store
@@ -1,43 +0,0 @@
1
- ActionController::Routing::Routes.draw do |map|
2
- # The priority is based upon order of creation: first created -> highest priority.
3
-
4
- # Sample of regular route:
5
- # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
6
- # Keep in mind you can assign values other than :controller and :action
7
-
8
- # Sample of named route:
9
- # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
10
- # This route can be invoked with purchase_url(:id => product.id)
11
-
12
- # Sample resource route (maps HTTP verbs to controller actions automatically):
13
- # map.resources :products
14
-
15
- # Sample resource route with options:
16
- # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
17
-
18
- # Sample resource route with sub-resources:
19
- # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
20
-
21
- # Sample resource route with more complex sub-resources
22
- # map.resources :products do |products|
23
- # products.resources :comments
24
- # products.resources :sales, :collection => { :recent => :get }
25
- # end
26
-
27
- # Sample resource route within a namespace:
28
- # map.namespace :admin do |admin|
29
- # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
30
- # admin.resources :products
31
- # end
32
-
33
- # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
34
- # map.root :controller => "welcome"
35
-
36
- # See how all your routes lay out with "rake routes"
37
-
38
- # Install the default routes as the lowest priority.
39
- # Note: These default routes make all actions in every controller accessible via GET requests. You should
40
- # consider removing or commenting them out if you're using named routes and resources.
41
- map.connect ':controller/:action/:id'
42
- map.connect ':controller/:action/:id.:format'
43
- end
@@ -1,123 +0,0 @@
1
- # Author:: Matt Parker (mailto:moonmaster9000@gmail.com)
2
- # License:: Distributes under the same terms as Ruby
3
-
4
- class Wallet
5
-
6
- # This error gets thrown whenever you attempt to retreive a TTL
7
- # for an action that's not in the wallet.
8
- class ActionNotCached < Exception
9
- end
10
-
11
- attr_reader :config #:nodoc:
12
- attr_reader :default_ttl #:nodoc:
13
-
14
- # Pass in a yaml string to this method that represents your configuration.
15
- # Let's assume you have a PagesController with a show action and an index action,
16
- # and let's suppose you would like wallet to default your action cache ttl's to 5 hours.
17
- # Then your yaml might be formatted like so:
18
- # # /path/to/wallet.yml
19
- #
20
- # default_ttl: 5.hours
21
- #
22
- # pages:
23
- # show:
24
- # index: 20.minutes
25
- #
26
- # That would result in the following values:
27
- # irb> w = Wallet.new File.open("/path/to/wallet.yml")
28
- # irb> w.cached? :pages, :show
29
- # => true
30
- # irb> w.cached? :pages, :index
31
- # => true
32
- # irb> w.cached? :pages, :new
33
- # => false
34
- # irb> w.ttl :pages, :show
35
- # => 300
36
- # irb> w.ttl :pages, :index
37
- # => 1200
38
- # irb> w.ttl :pages, :new
39
- # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
40
- # but according to our wallet configuration, that action is not cached.
41
- # from ./lib/wallet/wallet.rb:54:in `ttl'
42
- # from (irb):6
43
- def initialize(config_yml="")
44
- yml = YAML.load config_yml rescue nil
45
- @config = yml || {}
46
- @default_ttl = (eval(@config["default_ttl"]).to_i || 60) rescue 60
47
- setup_action_caching
48
- end
49
-
50
- # Returns true or false based on whether or not a controller action is configured for caching in the wallet
51
- # irb> w = Wallet.new "pages:\n show:"
52
- # irb> w.cached? :pages, :show
53
- # => true
54
- def cached?(controller, action)
55
- controller, action = stringify_params controller, action
56
- @config.has_key?(controller) &&
57
- @config[controller].respond_to?(:has_key?) &&
58
- @config[controller].has_key?(action)
59
- end
60
-
61
- def cached_actions(controller)
62
- @config[controller] ? @config[controller].keys : []
63
- end
64
-
65
- # Returns the value of the ttl for a controller / action. Throws an exception if the controller/action isn't setup
66
- # for caching.
67
- # irb> w = Wallet.new "pages:\n show:"
68
- # irb> w.cached? :pages, :show
69
- # => true
70
- # irb> w.ttl :pages, :new
71
- # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
72
- # but according to our wallet configuration, that action is not cached.
73
- # from ./lib/wallet/wallet.rb:54:in `ttl'
74
- # from (irb):6
75
- def ttl(controller, action)
76
- 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)
77
- controller, action = stringify_params controller, action
78
- if @config[controller][action]
79
- (eval(@config[controller][action]).to_i || @default_ttl) rescue @default_ttl
80
- else
81
- @default_ttl
82
- end
83
- end
84
-
85
- private
86
- def stringify_params(*args)
87
- args.map! {|a| a.to_s}
88
- return *args
89
- end
90
-
91
- def setup_action_caching
92
- @config.each do |controller, actions|
93
- controller_class = (controller + "_controller").camelize.constantize
94
- cached_actions(controller).each do |action|
95
- controller_class.send :caches_action, action.to_sym, :expires_in => ttl(controller, action)
96
- end
97
- end
98
- end
99
- end
100
-
101
- Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
102
-
103
- #ActionController::Base.class_eval do
104
- # before_filter :wallet
105
- # @@action_cached_controllers = {}
106
- #
107
- # def wallet
108
- # @wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
109
- # controller_class_name = self.class.name
110
- # controller = controller_class_name.underscore.gsub(/_controller$/, '')
111
- #
112
- # # if we haven't already setup action caching on this controller
113
- # if @@action_cached_controllers[controller] == nil
114
- #
115
- # @@action_cached_controllers[controller] = controller
116
- # @wallet.cached_actions(controller).each do |action|
117
- # puts "I'm setting up action caching for #{controller_class_name}::#{action}"
118
- # controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
119
- # end
120
- # end
121
- # end
122
- #
123
- #end
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
- require 'performance_test_help'
3
-
4
- # Profiling results for each test method are written to tmp/performance.
5
- class BrowsingTest < ActionController::PerformanceTest
6
- def test_homepage
7
- get '/'
8
- end
9
- end
@@ -1,38 +0,0 @@
1
- ENV["RAILS_ENV"] = "test"
2
- require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
3
- require 'test_help'
4
-
5
- class ActiveSupport::TestCase
6
- # Transactional fixtures accelerate your tests by wrapping each test method
7
- # in a transaction that's rolled back on completion. This ensures that the
8
- # test database remains unchanged so your fixtures don't have to be reloaded
9
- # between every test method. Fewer database queries means faster tests.
10
- #
11
- # Read Mike Clark's excellent walkthrough at
12
- # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
13
- #
14
- # Every Active Record database supports transactions except MyISAM tables
15
- # in MySQL. Turn off transactional fixtures in this case; however, if you
16
- # don't care one way or the other, switching from MyISAM to InnoDB tables
17
- # is recommended.
18
- #
19
- # The only drawback to using transactional fixtures is when you actually
20
- # need to test transactions. Since your test is bracketed by a transaction,
21
- # any transactions started in your code will be automatically rolled back.
22
- self.use_transactional_fixtures = true
23
-
24
- # Instantiated fixtures are slow, but give you @david where otherwise you
25
- # would need people(:david). If you don't want to migrate your existing
26
- # test cases which use the @david style and don't mind the speed hit (each
27
- # instantiated fixtures translates to a database query per test method),
28
- # then set this back to true.
29
- self.use_instantiated_fixtures = false
30
-
31
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
32
- #
33
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
34
- # -- they do not yet inherit this setting
35
- fixtures :all
36
-
37
- # Add more helper methods to be used by all tests here...
38
- end
@@ -1,19 +0,0 @@
1
- #ActionController::Base.class_eval do
2
- # before_filter :wallet
3
- # @@action_cached_controllers = {}
4
- #
5
- # def wallet
6
- # @wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
7
- # controller_class_name = self.class.name
8
- # controller = controller_class_name.underscore.gsub(/_controller$/, '')
9
- #
10
- # # if we haven't already setup action caching on this controller
11
- # if @@action_cached_controllers[controller] == nil
12
- # @@action_cached_controllers[controller] = controller
13
- # @wallet.cached_actions(controller).each do |action|
14
- # controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
15
- # end
16
- # end
17
- # end
18
- #
19
- #end if defined?(ActionController::Base)