preheat 0.1.0

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/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *~
2
+ .DS_Store
3
+ pkg
4
+ doc
5
+
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "preheat"
8
+ gemspec.summary = "Keep your Rails.cache warm"
9
+ gemspec.description = "Keep your Rails.cache warm"
10
+ gemspec.email = "tomhallett@gmail.com"
11
+ gemspec.homepage = "http://github.com/tommyh/preheat"
12
+ gemspec.authors = ["Tom Hallett"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/preheat.rb ADDED
@@ -0,0 +1,31 @@
1
+ class ActiveSupport::Cache::Store
2
+
3
+ def fetch_with_force(name, options = {})
4
+ options.merge!({:force => true})
5
+ if block_given?
6
+ fetch_without_force(name, options) do
7
+ yield
8
+ end
9
+ else
10
+ fetch_without_force(name, options)
11
+ end
12
+ end
13
+
14
+ #the singleton class_eval workaround for the aliasing is due to this issue:
15
+ # http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.ruby/2006-05/msg00575.html
16
+ def enable_preheat
17
+ (class << self; self; end).class_eval { alias_method_chain :fetch, :force }
18
+ end
19
+
20
+ def disable_preheat
21
+ (class << self; self; end).class_eval { alias_method :fetch, :fetch_without_force }
22
+ end
23
+ end
24
+
25
+ module Preheat
26
+ def self.it
27
+ RAILS_CACHE.enable_preheat
28
+ yield if block_given?
29
+ RAILS_CACHE.disable_preheat
30
+ end
31
+ end
@@ -0,0 +1,213 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_support'
4
+ require 'lib/preheat'
5
+
6
+ describe ActiveSupport::Cache do
7
+
8
+ before do
9
+ @cache = ActiveSupport::Cache.lookup_store(:memory_store)
10
+ @cache.clear
11
+ end
12
+
13
+ describe "#fetch_with_force" do
14
+
15
+ context "block is not given" do
16
+
17
+ it "should set :force to true if its not set" do
18
+ @cache.should_receive(:fetch_without_force).with("city", hash_including(:force => true))
19
+ @cache.fetch_with_force("city")
20
+ end
21
+
22
+ [true, false].each do |force_value|
23
+ it "should keep :force to true if it is set to #{force_value}" do
24
+ @cache.should_receive(:fetch_without_force).with("city", hash_including(:force => true))
25
+ @cache.fetch_with_force("city", :force => force_value)
26
+ end
27
+ end
28
+
29
+ context "after preheat has been enabled" do
30
+ before do
31
+ @cache.enable_preheat
32
+ end
33
+
34
+ it "should correctly force a cache miss" do
35
+ @cache.read("dog").should be_nil
36
+ @cache.write("dog", "beagle").should == "beagle"
37
+ @cache.fetch("dog").should be_nil
38
+ @cache.read("dog").should == "beagle"
39
+ end
40
+ end
41
+ end
42
+
43
+ context "block is given" do
44
+ it "should set :force to true if its not set" do
45
+ @cache.should_receive(:fetch_without_force).with("city", hash_including(:force => true))
46
+ @cache.fetch_with_force("city") do
47
+ "London"
48
+ end
49
+ end
50
+
51
+ [true, false].each do |force_value|
52
+ it "should keep :force to true if it is set to #{force_value}" do
53
+ @cache.should_receive(:fetch_without_force).with("city", hash_including(:force => true))
54
+ @cache.fetch_with_force("city", :force => force_value) do
55
+ "London"
56
+ end
57
+ end
58
+ end
59
+
60
+ it "should call fetch_without_force with a block" do
61
+ @cache.should_receive(:fetch_without_force).with("city", hash_including(:force => true)).and_yield(anything)
62
+ @cache.fetch_with_force("city") do
63
+ "foo"
64
+ end
65
+ end
66
+
67
+ context "after preheat has been enabled" do
68
+ before do
69
+ @cache.enable_preheat
70
+ end
71
+
72
+ it "should correctly set the cache" do
73
+ @cache.read("dog").should be_nil
74
+ @cache.write("dog", "beagle").should == "beagle"
75
+ @cache.fetch("dog") do
76
+ "spaniel"
77
+ end.should == "spaniel"
78
+ @cache.read("dog").should == "spaniel"
79
+ end
80
+ end
81
+
82
+
83
+ end
84
+ end
85
+
86
+
87
+ describe "#enable_preheat" do
88
+ it "should switch force to be true for all fetch calls after it" do
89
+ @cache.should_receive(:fetch).exactly(2).times.with("before-enable")
90
+ @cache.should_receive(:fetch).exactly(2).times.with("after-enable",hash_including(:force => true))
91
+
92
+ @cache.fetch("before-enable")
93
+ @cache.fetch("before-enable") do
94
+ "Foo"
95
+ end
96
+
97
+ @cache.enable_preheat
98
+ @cache.fetch("after-enable")
99
+ @cache.fetch("after-enable") do
100
+ "Foo"
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "#disable_preheat" do
106
+ it "should switch force to be default active support behavior for all fetch calls after it" do
107
+ @cache.should_receive(:fetch).exactly(2).times.with("after-disable")
108
+ @cache.should_receive(:fetch).exactly(2).times.with("before-disable",hash_including(:force => true))
109
+
110
+ @cache.enable_preheat
111
+ @cache.fetch("before-disable")
112
+ @cache.fetch("before-disable") do
113
+ "Foo"
114
+ end
115
+ @cache.disable_preheat
116
+ @cache.fetch("after-disable")
117
+ @cache.fetch("after-disable") do
118
+ "Foo"
119
+ end
120
+
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+
127
+ describe Preheat do
128
+
129
+
130
+ before do
131
+ @cache = ActiveSupport::Cache.lookup_store(:memory_store)
132
+ silence_warnings {
133
+ Object.const_set "RAILS_CACHE", @cache
134
+ }
135
+ @cache.clear
136
+ end
137
+
138
+ describe "it" do
139
+
140
+ context "no block given" do
141
+ it "should enable preheat when called" do
142
+ @cache.should_receive(:enable_preheat)
143
+ @cache.stub!(:disable_preheat)
144
+ Preheat.it
145
+ end
146
+
147
+ it "should disable preheat when called" do
148
+ @cache.stub!(:enable_preheat)
149
+ @cache.should_receive(:disable_preheat)
150
+ Preheat.it
151
+ end
152
+ end
153
+
154
+ context "block given" do
155
+ it "should enable preheat when called" do
156
+ @cache.should_receive(:enable_preheat)
157
+ @cache.stub!(:disable_preheat)
158
+ Preheat.it do
159
+ "hello"
160
+ end
161
+ end
162
+
163
+ it "should disable preheat when called" do
164
+ @cache.stub!(:enable_preheat)
165
+ @cache.should_receive(:disable_preheat)
166
+ Preheat.it do
167
+ "goodbye"
168
+ end
169
+ end
170
+
171
+ it "should execute the block given" do
172
+ Preheat.it do
173
+ @cache.fetch("city") do
174
+ "test"
175
+ end
176
+ end
177
+ @cache.read("city").should == "test"
178
+ end
179
+ end
180
+
181
+ end
182
+
183
+
184
+ context "integration tests" do
185
+
186
+ it "should not suck :)" do
187
+ @cache.read("dog").should be_nil
188
+ @cache.write("dog", "beagle").should == "beagle"
189
+ @cache.fetch("dog").should == "beagle"
190
+ @cache.fetch("dog") do
191
+ "poodle"
192
+ end.should == "beagle"
193
+ @cache.fetch("dog", :force => true) do
194
+ "lab"
195
+ end.should == "lab"
196
+
197
+ @cache.fetch("dog") do
198
+ "spaniel"
199
+ end.should == "lab"
200
+
201
+ Preheat.it do
202
+ @cache.fetch("dog") do
203
+ "pug"
204
+ end.should == "pug"
205
+ end
206
+
207
+ @cache.read("dog").should == "pug"
208
+ @cache.fetch("dog") do
209
+ "bulldog"
210
+ end.should == "pug"
211
+ end
212
+ end
213
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: preheat
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Hallett
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-28 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Keep your Rails.cache warm
23
+ email: tomhallett@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - .gitignore
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - lib/preheat.rb
36
+ - spec/lib/preheat_spec.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/tommyh/preheat
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Keep your Rails.cache warm
71
+ test_files:
72
+ - spec/lib/preheat_spec.rb