wallet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1 @@
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.
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,11 @@
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
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,110 @@
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!
@@ -0,0 +1,41 @@
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
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Add additional load paths for your own custom dirs
15
+ # config.load_paths += %W( #{RAILS_ROOT}/extras )
16
+
17
+ # Specify gems that this application depends on and have them installed with rake gems:install
18
+ # config.gem "bj"
19
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
+ # config.gem "aws-s3", :lib => "aws/s3"
22
+
23
+ # Only load the plugins named here, in the order given (default is alphabetical).
24
+ # :all can be used as a placeholder for all plugins not explicitly named
25
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
26
+
27
+ # Skip frameworks you're not going to use. To use Rails without a database,
28
+ # you must remove the Active Record framework.
29
+ # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
30
+
31
+ # Activate observers that should always be running
32
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
33
+
34
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
35
+ # Run "rake -D time" for a list of tasks for finding time zone names.
36
+ config.time_zone = 'UTC'
37
+
38
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
39
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
40
+ # config.i18n.default_locale = :de
41
+ end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,28 @@
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!
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,7 @@
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!
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,5 @@
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
@@ -0,0 +1,19 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1 @@
1
+ require 'wallet'
@@ -0,0 +1,43 @@
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
@@ -0,0 +1,112 @@
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
+ end
48
+
49
+ # Returns true or false based on whether or not a controller action is configured for caching in the wallet
50
+ # irb> w = Wallet.new "pages:\n show:"
51
+ # irb> w.cached? :pages, :show
52
+ # => true
53
+ def cached?(controller, action)
54
+ controller, action = stringify_params controller, action
55
+ @config.has_key?(controller) &&
56
+ @config[controller].respond_to?(:has_key?) &&
57
+ @config[controller].has_key?(action)
58
+ end
59
+
60
+ def cached_actions(controller)
61
+ @config[controller] ? @config[controller].keys : []
62
+ end
63
+
64
+ # Returns the value of the ttl for a controller / action. Throws an exception if the controller/action isn't setup
65
+ # for caching.
66
+ # irb> w = Wallet.new "pages:\n show:"
67
+ # irb> w.cached? :pages, :show
68
+ # => true
69
+ # irb> w.ttl :pages, :new
70
+ # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
71
+ # but according to our wallet configuration, that action is not cached.
72
+ # from ./lib/wallet/wallet.rb:54:in `ttl'
73
+ # from (irb):6
74
+ def ttl(controller, action)
75
+ 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)
76
+ controller, action = stringify_params controller, action
77
+ if @config[controller][action]
78
+ (eval(@config[controller][action]).to_i || @default_ttl) rescue @default_ttl
79
+ else
80
+ @default_ttl
81
+ end
82
+ end
83
+
84
+ private
85
+ def stringify_params(*args)
86
+ args.map! {|a| a.to_s}
87
+ return *args
88
+ end
89
+
90
+ end
91
+
92
+ ActionController::Base.class_eval do
93
+ before_filter :wallet
94
+ @@action_cached_controllers = {}
95
+
96
+ def wallet
97
+ @wallet ||= Wallet.new((File.open(RAILS_ROOT + "/config/wallet.yml") rescue ""))
98
+ controller_class_name = self.class.name
99
+ controller = controller_class_name.underscore.gsub(/_controller$/, '')
100
+
101
+ # if we haven't already setup action caching on this controller
102
+ if @@action_cached_controllers[controller] == nil
103
+
104
+ @@action_cached_controllers[controller] = controller
105
+ @wallet.cached_actions(controller).each do |action|
106
+ puts "I'm setting up action caching for #{controller_class_name}::#{action}"
107
+ controller_class_name.constantize.send :caches_action, action.to_sym, :expires_in => @wallet.ttl(controller, action)
108
+ end
109
+ end
110
+ end
111
+
112
+ end
@@ -0,0 +1,9 @@
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
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,19 @@
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)
@@ -0,0 +1,90 @@
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
+ end
48
+
49
+ # Returns true or false based on whether or not a controller action is configured for caching in the wallet
50
+ # irb> w = Wallet.new "pages:\n show:"
51
+ # irb> w.cached? :pages, :show
52
+ # => true
53
+ def cached?(controller, action)
54
+ controller, action = stringify_params controller, action
55
+ @config.has_key?(controller) &&
56
+ @config[controller].respond_to?(:has_key?) &&
57
+ @config[controller].has_key?(action)
58
+ end
59
+
60
+ def cached_actions(controller)
61
+ @config[controller] ? @config[controller].keys : []
62
+ end
63
+
64
+ # Returns the value of the ttl for a controller / action. Throws an exception if the controller/action isn't setup
65
+ # for caching.
66
+ # irb> w = Wallet.new "pages:\n show:"
67
+ # irb> w.cached? :pages, :show
68
+ # => true
69
+ # irb> w.ttl :pages, :new
70
+ # Wallet::ActionNotCached: You asked for the TTL for the 'new' action in the 'pages' controller,
71
+ # but according to our wallet configuration, that action is not cached.
72
+ # from ./lib/wallet/wallet.rb:54:in `ttl'
73
+ # from (irb):6
74
+ def ttl(controller, action)
75
+ 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)
76
+ controller, action = stringify_params controller, action
77
+ if @config[controller][action]
78
+ (eval(@config[controller][action]).to_i || @default_ttl) rescue @default_ttl
79
+ else
80
+ @default_ttl
81
+ end
82
+ end
83
+
84
+ private
85
+ def stringify_params(*args)
86
+ args.map! {|a| a.to_s}
87
+ return *args
88
+ end
89
+
90
+ end
data/lib/wallet.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'wallet/wallet'
2
+ require 'wallet/action_controller'
@@ -0,0 +1,72 @@
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(File.open(File.dirname(__FILE__) + '/../support/wallet.yml'))
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 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
30
+ end
31
+ end
32
+
33
+ describe "Wallet::cached?" do
34
+ before do
35
+ config_yml = "pages:\n show:\n"
36
+ @wallet = Wallet.new config_yml
37
+ end
38
+
39
+ it "should return true for a controller/action that is cached" do
40
+ @wallet.cached?("pages", "show").should == true
41
+ end
42
+
43
+ it "should return false for a controller/action that is not cached" do
44
+ @wallet.cached?("blah", "blah").should == false
45
+ @wallet.cached?("pages", "blah").should == false
46
+ end
47
+
48
+ it "should work with both strings and symbols" do
49
+ @wallet.cached?("pages", "show").should == true
50
+ @wallet.cached?(:pages, :show).should == true
51
+ end
52
+ end
53
+
54
+ describe "Wallet::ttl" do
55
+ before do
56
+ config_yml = "pages:\n show: 5.minute\n index: "
57
+ @wallet = Wallet.new config_yml
58
+ end
59
+
60
+ it "should return 5 minutes for the pages:show ttl" do
61
+ @wallet.ttl(:pages, :show).should == 5.minutes.to_i
62
+ end
63
+
64
+ it "should return the default ttl for the index action" do
65
+ @wallet.ttl(:pages, :index).should == @wallet.default_ttl
66
+ end
67
+
68
+ it "should raise a ActionNotCached error for the dne action" do
69
+ proc {@wallet.ttl(:pages, :dne)}.should raise_error(Wallet::ActionNotCached)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH << './lib'
2
+ require 'activesupport'
3
+ require 'wallet'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wallet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Parker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: Wallet is a rails gem that allows you to manage all of your action caching configuration in a single yaml file. Supports TTLs.
26
+ email: moonmaster9000@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - lib/wallet.rb
36
+ - lib/wallet/action_controller.rb
37
+ - lib/wallet/wallet.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/moonmaster9000/wallet
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Centralized YAML configuration for action caching.
66
+ test_files:
67
+ - spec/lib_specs/wallet_spec.rb
68
+ - 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/initializers/wallet.rb
83
+ - examples/tv/config/routes.rb
84
+ - examples/tv/lib/wallet.rb
85
+ - examples/tv/test/performance/browsing_test.rb
86
+ - examples/tv/test/test_helper.rb