erector_cache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Grockit
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = erector_cache
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Grockit. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "erector_cache"
8
+ gem.summary = %Q{Caching for your Erector}
9
+ gem.description = %Q{Caching for your Erector}
10
+ gem.email = "mmol@grockit.com"
11
+ gem.homepage = "http://github.com/grockit/erector_cache"
12
+ gem.authors = ["Grockit"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.6"
14
+ gem.add_dependency "lawnchair", ">=0.6.8"
15
+ gem.add_dependency "erector", "=0.8.1"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "erector_cache #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
File without changes
@@ -0,0 +1,82 @@
1
+ module ErectorCache
2
+ module Widget
3
+ def self.included(base)
4
+ base.module_eval do
5
+ cattr_accessor :expire_in
6
+ extend ClassMethods
7
+ class_inheritable_array :key_components
8
+ include InstanceMethods
9
+ alias_method_chain :_render_via, :caching
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def cache_with(*components)
15
+ self.key_components = components
16
+ end
17
+
18
+ def cache_for(ttl)
19
+ self.expire_in = ttl
20
+ end
21
+
22
+ def expire!(hash={})
23
+ hash = Hash.new("*").merge(hash)
24
+
25
+ search_key = self.key_components.inject(["Lawnchair", self.to_s]) do |collection, part|
26
+ k_prime = part.is_a?(Hash) ? part.keys.first : part
27
+ collection << k_prime
28
+ collection << hash[k_prime]
29
+ end.join(":")
30
+
31
+ LAWNCHAIR.redis.keys(search_key).split.each{|key| LAWNCHAIR.redis.del(key) }
32
+ end
33
+
34
+ def cache_key(hash)
35
+ self.key_components.inject([self.to_s]) do |collection, part|
36
+ object = part.is_a?(Hash) ? hash[part.keys.first] : hash[part]
37
+
38
+ if part.is_a?(Hash)
39
+ key = part.keys.first
40
+ value = Array(part[key]).map do |p|
41
+ if p.is_a?(Proc)
42
+ p.call(object)
43
+ else
44
+ object.send(p)
45
+ end
46
+ end.join("-")
47
+ else
48
+ key = part
49
+ value = object.to_param
50
+ end
51
+ collection << [key, value]
52
+ end.flatten.join(":")
53
+ end
54
+
55
+ end
56
+
57
+ module InstanceMethods
58
+ def cache_key
59
+ key_data = {}
60
+ self.class.key_components.each do |part|
61
+ part = part.keys.first if part.is_a?(Hash)
62
+ key_data[part] = self.instance_variable_get("@#{part}")
63
+ end
64
+ return self.class.cache_key(key_data)
65
+ end
66
+
67
+ def _render_via_with_caching(parent, options={})
68
+ if self.class.key_components.blank?
69
+ _render_via_without_caching(parent, options)
70
+ else
71
+ options = {:expire_in => @expire_in || 1.hour}
72
+ cached_fragment = LAWNCHAIR.cache(cache_key, options) do
73
+ parent.capture { _render_via_without_caching(parent, options) }
74
+ end
75
+ parent.output << cached_fragment
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ Erector::Widget.send(:include, ErectorCache::Widget)
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'action_controller'
3
+ require 'erector'
4
+ require 'erector/rails'
5
+ require 'erector_cache/fragment'
6
+ require 'erector_cache/widget'
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1,153 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ class Turtle < Erector::Widget
4
+ cache_with :name
5
+
6
+ def content
7
+ div "Cool stuff below"
8
+ widget NinjaTurtle, :name => @name, :weapon => @weapon, :master => @master
9
+ end
10
+ end
11
+
12
+ class Master
13
+ def initialize(name)
14
+ @name = name
15
+ end
16
+
17
+ def name
18
+ @name
19
+ end
20
+
21
+ def to_param
22
+ name
23
+ end
24
+ end
25
+
26
+ class NinjaTurtle < Turtle
27
+ cache_with :weapon, :master => lambda {|m| m.name }
28
+ cache_for 25.years
29
+
30
+ def content
31
+ span "Weapon: #{@weapon}"
32
+ span "Cached at: #{Time.now.to_s(:db)}"
33
+ end
34
+ end
35
+
36
+ describe ErectorCache::Widget do
37
+ before do
38
+ @splinter = Master.new("Splinter")
39
+ end
40
+
41
+ describe "ClassMethods" do
42
+ describe ".cache_with" do
43
+ it "sets the attributes to cache instances of this widget with" do
44
+ Turtle.key_components.should == [:name]
45
+ end
46
+
47
+ it "inherits parent classes cache key components" do
48
+ component_parts = NinjaTurtle.key_components.map{|c| c.is_a?(Hash) ? c.keys.first : c}
49
+ component_parts.should == [:name, :weapon, :master]
50
+ end
51
+ end
52
+
53
+ describe ".expire!" do
54
+ it "expires the cache" do
55
+ render_template do |controller|
56
+ controller.render_widget Turtle, :name => "Leonardo", :weapon => "Katanas", :master => @splinter
57
+ end
58
+ expected_cached_at_time = @output.match(/Cached at: (.*)</)[1]
59
+
60
+ sleep 1
61
+
62
+ render_template do |controller|
63
+ controller.render_widget Turtle, :name => "Leonardo", :weapon => "Katanas", :master => @splinter, :slogan => "Turtle Power!"
64
+ end
65
+ @output.should include expected_cached_at_time
66
+
67
+ NinjaTurtle.expire!(:weapon => "Katanas")
68
+
69
+ sleep 1
70
+
71
+ render_template do |controller|
72
+ controller.render_widget Turtle, :name => "Leonardo", :weapon => "Katanas", :master => @splinter
73
+ end
74
+ @output.should_not include expected_cached_at_time
75
+ end
76
+ end
77
+
78
+ describe ".cache_for" do
79
+ it "sets appropriate ttl" do
80
+ Turtle.expire_in.should == 25.years
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "InstanceMethods" do
86
+ describe "#cache_key" do
87
+ it "builds a simple cache key for an instance properly" do
88
+ widget = Turtle.new(:name => "Yertle")
89
+ widget.cache_key.should == "Turtle:name:Yertle"
90
+ end
91
+
92
+ it "builds a complex cache key" do
93
+ widget = NinjaTurtle.new(:name => "Leonardo", :master => @splinter, :weapon => "Dual Katanas")
94
+ widget.cache_key.should == "NinjaTurtle:name:Leonardo:weapon:Dual Katanas:master:Splinter"
95
+ end
96
+ end
97
+
98
+ describe "_render_via_with_caching" do
99
+ context "when there is a cache_with set" do
100
+ it "stores the widgets output in LAWNCHAIR" do
101
+ now = Time.now
102
+
103
+ render_template do |controller|
104
+ controller.render_widget Turtle, :name => "Leonardo", :weapon => "Dual Katanas", :master => @splinter
105
+ end
106
+
107
+ @output.should include("Weapon: Dual Katanas")
108
+ expected_cached_at_time = @output.match(/Cached at: (.*)</)[1]
109
+
110
+ sleep 1
111
+
112
+ render_template do |controller|
113
+ controller.render_widget Turtle, :name => "Leonardo", :weapon => "Dual Katanas", :master => @splinter
114
+ end
115
+
116
+ @output.should include("Cached at: #{expected_cached_at_time}")
117
+ end
118
+ end
119
+
120
+ context "when there is NO cache_with set" do
121
+ class People < Erector::Widget
122
+ def content
123
+ widget Sheriff, :name => "Boss Hogg", :attitude => "Bad", :boys => ["good", "ol'"], :belly => :round
124
+ end
125
+ end
126
+
127
+ class Sheriff < Erector::Widget
128
+ def content
129
+ div do
130
+ div "Last failed to catch #{@boys.join('-')} boys: #{Time.now.to_s(:db)}"
131
+ div "Name: #{@name}"
132
+ end
133
+ end
134
+ end
135
+
136
+ it "calls _render_via_without_caching" do
137
+ render_template do |controller|
138
+ controller.render_widget People
139
+ end
140
+ @output.should include("Name: Boss Hogg")
141
+ expected_cached_at_time = @output.match(/boys: (.*)</)[1]
142
+ sleep 1
143
+
144
+ render_template do |controller|
145
+ controller.render_widget People
146
+ end
147
+ @output.should_not include(expected_cached_at_time)
148
+ end
149
+ end
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ErectorCache" do
4
+
5
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,55 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'erector_cache'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require "lawnchair"
7
+ LAWNCHAIR = Lawnchair
8
+ LAWNCHAIR.connectdb(Redis.new(:db => 3))
9
+
10
+ Spec::Runner.configure do |config|
11
+ config.before(:each) do
12
+ LAWNCHAIR.flushdb
13
+ end
14
+ end
15
+
16
+ class FixtureTemplate < ActionView::Base
17
+
18
+ def initialize
19
+ ActionView::Base.module_eval do
20
+ def protect_against_forgery?
21
+ false
22
+ end
23
+ end
24
+ @controller = ActionController::Base.new
25
+ @controller.logger = Logger.new("/tmp/foo")
26
+ request = create_request
27
+ response = create_response
28
+ @controller.send(:assign_shortcuts, request, response)
29
+ @controller.instance_variable_set(:@url, ActionController::UrlRewriter.new(request, {}))
30
+ if respond_to?(:output_buffer)
31
+ self.output_buffer = ""
32
+ end
33
+ end
34
+
35
+ def controller
36
+ @controller
37
+ end
38
+
39
+ def create_request
40
+ env = {}
41
+ env['REQUEST_URI'] = "/foo"
42
+ env['action_controller.request.request_parameters'] = {}
43
+ ActionController::Request.new(env)
44
+ end
45
+
46
+ def create_response
47
+ response = ActionController::Response.new
48
+ response.template = ActionView::Base.new([],{},controller)
49
+ response
50
+ end
51
+ end
52
+
53
+ def render_template
54
+ @output = yield FixtureTemplate.new.controller
55
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erector_cache
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Grockit
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-14 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 6
34
+ version: 1.2.6
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: lawnchair
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 0
48
+ - 6
49
+ - 8
50
+ version: 0.6.8
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: erector
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 61
62
+ segments:
63
+ - 0
64
+ - 8
65
+ - 1
66
+ version: 0.8.1
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description: Caching for your Erector
70
+ email: mmol@grockit.com
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - LICENSE
77
+ - README
78
+ - README.rdoc
79
+ files:
80
+ - .document
81
+ - .gitignore
82
+ - LICENSE
83
+ - README
84
+ - README.rdoc
85
+ - Rakefile
86
+ - VERSION
87
+ - lib/erector_cache.rb
88
+ - lib/erector_cache/fragment.rb
89
+ - lib/erector_cache/widget.rb
90
+ - spec/erector_cache/fragment_spec.rb
91
+ - spec/erector_cache/widget_spec.rb
92
+ - spec/erector_cache_spec.rb
93
+ - spec/spec.opts
94
+ - spec/spec_helper.rb
95
+ has_rdoc: true
96
+ homepage: http://github.com/grockit/erector_cache
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options:
101
+ - --charset=UTF-8
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirements: []
123
+
124
+ rubyforge_project:
125
+ rubygems_version: 1.3.7
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Caching for your Erector
129
+ test_files:
130
+ - spec/erector_cache/fragment_spec.rb
131
+ - spec/erector_cache/widget_spec.rb
132
+ - spec/erector_cache_spec.rb
133
+ - spec/spec_helper.rb