expiry_control 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +49 -0
  3. data/Rakefile +30 -0
  4. data/lib/expiry_control.rb +61 -0
  5. data/rails/init.rb +1 -0
  6. metadata +66 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Urban Hafner
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ Rails 2.3 allows you to set the Expires header using the expires_in
2
+ method. However this couples your caching strategy with your controller
3
+ code. And it spreads out the caching configuration all over the place.
4
+ This plugin allows you to keep your caching configuration in one place
5
+ out of the controllers.
6
+
7
+ Install
8
+ =======
9
+
10
+ Install as a gem (`gem install expiry_control`) and add it in your
11
+ `environmen.rb` file.
12
+
13
+ Usage
14
+ =====
15
+
16
+ Create a file somewhere, maybe in `config/initializers` that will contain
17
+ your configuration. The name doesn't matter. Your configuration will go
18
+ into the following block of code:
19
+
20
+ ExpiryControl.configure do |config|
21
+ # configuration goes here
22
+ end
23
+
24
+ Just like when you configure your routes you call methods on the object
25
+ passed into the block:
26
+
27
+ # Set Expires header to 1 hour for the whole blog controller
28
+ config.cache "blog", :for => 1.hour
29
+
30
+ # Set Expires header to 5 minutes for a special action, this overrides
31
+ # the controller wide setting
32
+ config.cache "blog#special", :for => 5.minutes
33
+
34
+ # Set a default caching time
35
+ config.default 1.minute
36
+
37
+ This is all there is to it for the basic usage. Note that more specialized
38
+ rules take precedence. Specifically, the code first looks for a rule at
39
+ the action level (or rather action in a specific controller), then at the
40
+ controller level and the it uses the default rule (if it exists).
41
+
42
+ Sometimes however this is not enough and you need to make the caching
43
+ optional. For these cases you can use `:if` and `:unless` like you would
44
+ use in validations. Note, that you have to use a `Proc` symbols or strings
45
+ are not supported. This `Proc` gets passed the controller object for
46
+ maximum flexibility.
47
+
48
+ # Only cache if 'cache' param is set
49
+ config.cache "blog", :for => 1.hour, :if => Proc.new {|controller| controller.params["cache"]}
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake/gempackagetask'
2
+
3
+ PKG_FILES = FileList[
4
+ '[a-zA-Z]*',
5
+ 'generators/**/*',
6
+ 'lib/**/*',
7
+ 'rails/**/*',
8
+ 'tasks/**/*',
9
+ 'test/**/*'
10
+ ]
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = "expiry_control"
14
+ s.version = "0.0.1"
15
+ s.author = "Urban Hafner"
16
+ s.email = "urban@bettong.net"
17
+ s.homepage = "http://github.com/msales/expiry_control"
18
+ s.platform = Gem::Platform::RUBY
19
+ s.summary = "DSL to set expires headers for Rails apps"
20
+ s.files = PKG_FILES.to_a
21
+ s.require_path = "lib"
22
+
23
+ s.has_rdoc = false
24
+ s.extra_rdoc_files = ["README.md"]
25
+ end
26
+
27
+ desc 'Turn this plugin into a gem.'
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
@@ -0,0 +1,61 @@
1
+ class ActionController::Base
2
+
3
+ after_filter :_set_expires_header
4
+
5
+ private
6
+
7
+ def _set_expires_header
8
+ et = ExpiryControl.expiration_time(self)
9
+ expires_in et, :public => true if et
10
+ end
11
+
12
+ end
13
+
14
+ module ExpiryControl
15
+
16
+ def self.configure
17
+ @@config ||= Config.new
18
+ yield @@config
19
+ end
20
+
21
+ def self.expiration_time(controller)
22
+ @@config.expiration_time(controller) if @@config
23
+ end
24
+
25
+ class Config
26
+
27
+ def initialize
28
+ @config = {}
29
+ end
30
+
31
+ def cache(path, options)
32
+ raise "Missing expiration time!" unless options[:for]
33
+ raise "You cannot use :if and :unless at the same time!" if [:if, :unless].all? {|o| options[o]}
34
+ @config[path] = options
35
+ end
36
+
37
+ def default(time)
38
+ @config[:default] = time
39
+ end
40
+
41
+ def expiration_time(controller)
42
+ ca_string = "#{controller.controller_name}##{controller.action_name}"
43
+ request_config = @config[ca_string]
44
+ request_config ||= @config[controller.controller_name]
45
+ request_config ||= @config[:default]
46
+ request_config ||= {}
47
+ if [:if, :unless].any? {|o| request_config[o]}
48
+ r = request_config[:if].call(controller) if request_config[:if]
49
+ r = !request_config[:unless].call(controller) if request_config[:unless]
50
+ if r
51
+ request_config[:for]
52
+ else
53
+ nil
54
+ end
55
+ else
56
+ request_config[:for]
57
+ end
58
+ end
59
+ end
60
+
61
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'expiry_control'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expiry_control
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Urban Hafner
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-28 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: urban@bettong.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.md
29
+ files:
30
+ - LICENSE
31
+ - Rakefile
32
+ - README.md
33
+ - lib/expiry_control.rb
34
+ - rails/init.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/msales/expiry_control
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: DSL to set expires headers for Rails apps
65
+ test_files: []
66
+