action_param_caching 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1232085c788b054a445d683168928962fb2094c0
4
+ data.tar.gz: 796b06fd01e778e03cc5c58083dd2c9504323428
5
+ SHA512:
6
+ metadata.gz: 9fe6a6437d081580361e31feec198b77d69ab3ba658b92f1780edfcc2f02646633fec48ee36497042edf9868781542226cf853fccf38460b4ef1f76cf7413c6c
7
+ data.tar.gz: 56ed26ce55487a71e631cb3e51e05d61fb991a79421f9c789bd34a3ae52aab75a1f4fe16f73f4c6b13ec9a16079fa0b37f81a21b51af011e86e955f8426e0e01
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in action_param_caching.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 cparratto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # ActionParamCaching
2
+
3
+ Action param caching simplfies action caching bases on parameter values. It allows you to quickly specify what actions you want to cache base on the parameters they are passed, the set of parameters you are interested in caching on, and an option prefix value for ignoring parameters that are designed to bust your cache.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'action_param_caching'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install action_param_caching
18
+
19
+ ## Usage
20
+
21
+ 1. Enable rails caching (See rails docs).
22
+ 2. Require 'action_param_caching'
23
+ 3. Add 'extend ActionParamCaching::Rails::ActionController' to your controller
24
+ 4. Add the correct param cache statement to your controller.
25
+ - Standard statement for full param chaching:
26
+ cache_with_params :on => [:index]
27
+ - Cache on a set or subset
28
+ cache_with_params :on => [:index], :with_set_or_subset => [:param1, :param2]
29
+ - Ignore params with prefix
30
+ cache_with_params :on => [:index], :filter_starting_with => '_'
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+ desc "Run all spec in spec directory (excluding plugin spec)"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'action_param_caching/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "action_param_caching"
8
+ spec.version = ActionParamCaching::VERSION
9
+ spec.authors = ["cparratto"]
10
+ spec.email = ["chris.parratto@pnmac.com"]
11
+ spec.description = %q{Parameterized caching for ActionControllers}
12
+ spec.summary = %q{Allows for parameterized caching for action controllers.}
13
+ spec.homepage = "https://github.com/pennymac/action_param_caching"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "mocha"
25
+
26
+ spec.add_dependency "active_support"
27
+ spec.add_dependency "i18n"
28
+
29
+ end
30
+
@@ -0,0 +1,41 @@
1
+ module ActionParamCaching
2
+ class ActionConfig
3
+ attr_reader :action, :valid_params, :filter_starting_with, :argz
4
+ attr_accessor :did_config_cache
5
+
6
+ def initialize(action, valid_params = [], filter_starting_with = nil)
7
+ @action = action
8
+ @valid_params = valid_params
9
+ @filter_starting_with = filter_starting_with
10
+ @did_config_cache = false
11
+ end
12
+
13
+ def cache_args
14
+ @argz = {}
15
+
16
+ if @valid_params && @valid_params.any?
17
+ @argz[:if] = self.if_proc
18
+ end
19
+
20
+ if @filter_starting_with
21
+ @argz[:cache_path] = self.filtered_path_proc
22
+ else
23
+ @argz[:cache_path] = self.path_proc
24
+ end
25
+
26
+ @argz
27
+ end
28
+
29
+ def filtered_path_proc
30
+ Proc.new { |request| params.delete_if { |k,v| k.to_s.start_with?(@filter_starting_with) }.to_query }
31
+ end
32
+
33
+ def path_proc
34
+ Proc.new { |request| params.to_query }
35
+ end
36
+
37
+ def if_proc
38
+ Proc.new { params.collect{|key, value| @valid_params.include?(key)}.all? }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,24 @@
1
+ module ActionParamCaching
2
+ module Rails
3
+ module ActionController
4
+ attr_reader :action_cache_configs
5
+
6
+ def cache_with_params(options = {})
7
+ @action_cache_configs = {} if @action_cache_configs.nil?
8
+
9
+ options[:on].each do |action|
10
+ @action_cache_configs[action] = ActionParamCaching::ActionConfig.new(action, options[:with_set_or_subset], options[:filter_starting_with]) unless @action_cache_configs[action]
11
+ end
12
+
13
+ options[:on].each do |action|
14
+ config = @action_cache_configs[action]
15
+
16
+ unless config.did_config_cache
17
+ caches_action action, config.cache_args
18
+ config.did_config_cache = true
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActionParamCaching
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "action_param_caching/version"
2
+ require "action_param_caching/action_config"
3
+ require "action_param_caching/rails/action_controller"
4
+
5
+ module ActionParamCaching
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ module ActionParamCaching
4
+ describe ActionConfig do
5
+ context "in order to track the cache configuration of an action" do
6
+ it "is initialized with valid params, an action, and a prefix filter" do
7
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount], '_')
8
+ cache_config.action.should == :index
9
+ cache_config.valid_params.should == [:id, :name, :amount]
10
+ cache_config.filter_starting_with.should == '_'
11
+ end
12
+
13
+ it "sets the correct default values for valid params and prefix filters" do
14
+ cache_config = ActionParamCaching::ActionConfig.new(:index)
15
+ cache_config.action.should == :index
16
+ cache_config.valid_params.should == []
17
+ cache_config.filter_starting_with.should be_nil
18
+ end
19
+
20
+ it "tracks if a cache has been configured" do
21
+ cache_config = ActionParamCaching::ActionConfig.new(:index)
22
+ cache_config.did_config_cache.should be_false
23
+ cache_config.did_config_cache = true
24
+ cache_config.did_config_cache.should be_true
25
+ end
26
+ end
27
+
28
+ context "when generating action cache arguments" do
29
+ it "has an if condition when you specify valid params" do
30
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount], '_')
31
+ cache_args = cache_config.cache_args
32
+ cache_args[:if].should be
33
+ end
34
+
35
+ it "does not have an if condition if you don't specify valid params" do
36
+ cache_config = ActionParamCaching::ActionConfig.new(:index)
37
+ cache_args = cache_config.cache_args
38
+ cache_args[:if].should be_nil
39
+ end
40
+
41
+ it "has a the correct cache path when stripping params with a prefix" do
42
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount], '_')
43
+
44
+
45
+ cache_config.stubs(:params).returns( {:_remove_me => true} )
46
+ cache_args = cache_config.cache_args
47
+ proc = cache_args[:cache_path]
48
+ proc.call(nil).should == ""
49
+ end
50
+
51
+ it "has a the correct cache path under normal conditions" do
52
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount])
53
+ cache_args = cache_config.cache_args
54
+ cache_config.stubs(:params).returns( {:_remove_me => false} )
55
+ proc = cache_args[:cache_path]
56
+ proc.call(nil).should == cache_config.params.to_query
57
+ end
58
+
59
+ it "does not have a if argument if the valid params are empty" do
60
+ cache_config = ActionParamCaching::ActionConfig.new(:index)
61
+ cache_args = cache_config.cache_args
62
+ cache_args[:if].should be_nil
63
+ end
64
+
65
+ it "does have an if argument that returns true if the params are a subset of the valid params" do
66
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount])
67
+ cache_args = cache_config.cache_args
68
+ cache_config.stubs(:params).returns( {:id => 23, :name => "test"} )
69
+ proc = cache_args[:if]
70
+ proc.call.should be_true
71
+ end
72
+
73
+ it "does have an if argument that returns false if the params are not a subset of the valid params" do
74
+ cache_config = ActionParamCaching::ActionConfig.new(:index,[:id, :name, :amount])
75
+ cache_args = cache_config.cache_args
76
+ cache_config.stubs(:params).returns( {:id => 23, :name => "test", :not => "valid"} )
77
+ proc = cache_args[:if]
78
+ proc.call.should be_false
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ class TestController
4
+ extend ActionParamCaching::Rails::ActionController
5
+ def self.caches_action(action, args)
6
+ end
7
+ end
8
+
9
+ module ActionParamCaching
10
+ module Rails
11
+ describe ActionController do
12
+ it "provides param configuration for action caching" do
13
+ TestController.respond_to?(:action_cache_configs).should be_true
14
+ end
15
+
16
+ it "provides a means to configure caching for mutiple actions with one statement" do
17
+ TestController.cache_with_params :on => [:test1]
18
+ TestController.action_cache_configs[:test1].should be
19
+ end
20
+
21
+ it "provided a means to set a set or subset or params to cache on" do
22
+ TestController.cache_with_params :on => [:test2], :with_set_or_subset => [:param1, :param2]
23
+ TestController.action_cache_configs[:test2].valid_params.should == [:param1, :param2]
24
+ end
25
+
26
+ it "provides a means to filter params that have a prefix" do
27
+ TestController.cache_with_params :on => [:test3], :filter_starting_with => '_'
28
+ TestController.action_cache_configs[:test3].filter_starting_with.should == '_'
29
+ end
30
+
31
+ it "configures the cache actions using the specified params" do
32
+ TestController.expects(:caches_action)
33
+ TestController.cache_with_params :on => [:test4], :filter_starting_with => '_', :with_set_or_subset => [:param1, :param2]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+ require 'active_support/all'
3
+
4
+ require 'action_param_caching' # and any other gems you need
5
+
6
+ #Hash.send(:include, ActiveSupport::CoreExtensions::Hash) unless Hash.included_modules.include?(ActiveSupport::CoreExtensions::Hash)
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :mocha
10
+ config.order = "random"
11
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_param_caching
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cparratto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: active_support
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: i18n
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Parameterized caching for ActionControllers
98
+ email:
99
+ - chris.parratto@pnmac.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - action_param_caching.gemspec
110
+ - lib/action_param_caching.rb
111
+ - lib/action_param_caching/action_config.rb
112
+ - lib/action_param_caching/rails/action_controller.rb
113
+ - lib/action_param_caching/version.rb
114
+ - spec/action_config_spec.rb
115
+ - spec/rails/action_controller_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: https://github.com/pennymac/action_param_caching
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.0.3
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Allows for parameterized caching for action controllers.
141
+ test_files:
142
+ - spec/action_config_spec.rb
143
+ - spec/rails/action_controller_spec.rb
144
+ - spec/spec_helper.rb