garterbelt 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{garterbelt}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kane Baccigalupi"]
@@ -2,17 +2,22 @@ module Garterbelt
2
2
  class Cache < Renderer
3
3
  include ContentRendering
4
4
 
5
- attr_accessor :key, :cache_output, :view_output
5
+ attr_accessor :key, :cache_output, :view_output, :expires_in
6
6
 
7
7
  def initialize(opts, &block)
8
8
  super
9
9
  self.key = opts[:key]
10
+ self.expires_in = opts[:expires_in]
10
11
  raise ArgumentError, ":key option required" unless key
11
12
  self.content = block if block_given?
12
13
  raise_unless_block_content
13
14
  self.cache_output = ""
14
15
  end
15
16
 
17
+ def cache_options
18
+ expires_in ? {:expires_in => expires_in} : {}
19
+ end
20
+
16
21
  def head
17
22
  self.view_output = output
18
23
  self.output = cache_output
@@ -27,8 +32,8 @@ module Garterbelt
27
32
  if cached = view.cache_store[key]
28
33
  self.output << cached
29
34
  else
30
- super # renders block to the diverted output
31
- view.cache_store[key] = cache_output # set the cache
35
+ super # renders block to the diverted output
36
+ view.cache_store.store(key, cache_output, cache_options) # set the cache
32
37
  end
33
38
  end
34
39
  end
@@ -332,10 +332,9 @@ module Garterbelt
332
332
  "#{cache_key_base}_#{detail}"
333
333
  end
334
334
 
335
- def cache(key, &block)
336
- renderer = Cache.new(:view => self, :key => cache_key(key), &block)
337
- buffer << renderer
338
- renderer
335
+ def cache(key, opts={}, &block)
336
+ opts = opts.merge(:key => cache_key(key), :view => curator)
337
+ add_to_buffer Cache.new(opts, &block)
339
338
  end
340
339
  end
341
340
  end
@@ -29,6 +29,13 @@ describe Garterbelt::Cache do
29
29
  it 'stores the full key' do
30
30
  build_cache.key.should == 'good_deal'
31
31
  end
32
+
33
+ it 'stores the expiration information' do
34
+ cache = Garterbelt::Cache.new(:key => 'expiring_deal', :view => @view, :expires_in => 3600) do # one hour, in seconds
35
+ Garterbelt::ContentTag.new(:type => :p, :view => @view)
36
+ end
37
+ cache.expires_in.should == 3600
38
+ end
32
39
  end
33
40
 
34
41
  describe 'rendering' do
@@ -80,6 +87,27 @@ describe Garterbelt::Cache do
80
87
  @view.should_receive(:render_buffer)
81
88
  @cache.render
82
89
  end
90
+
91
+ it 'adds puts the render block into the cache' do
92
+ @view.output = "view output; "
93
+ @view.cache_store.should_receive(:[]).with('good_deal').and_return(nil)
94
+ @view.stub(:render_buffer).and_return('buffer rendered')
95
+ @view.cache_store.should_receive(:store).with('good_deal', @cache.cache_output, {})
96
+ @cache.render
97
+ end
98
+
99
+ it 'uses the expiration when it has one' do
100
+ cache = Garterbelt::Cache.new(:key => 'good_deal', :view => @view, :expires_in => 3600) do
101
+ Garterbelt::ContentTag.new(:type => :p, :view => @view)
102
+ end
103
+
104
+ @view.output = "view output; "
105
+ @view.cache_store.should_receive(:[]).with('good_deal').and_return(nil)
106
+ @view.stub(:render_buffer).and_return('buffer rendered')
107
+
108
+ @view.cache_store.should_receive(:store).with('good_deal', cache.cache_output, {:expires_in => 3600})
109
+ cache.render
110
+ end
83
111
  end
84
112
  end
85
113
  end
@@ -128,5 +128,13 @@ describe Garterbelt::View, "Caching" do
128
128
  cache.is_a?(Garterbelt::Cache).should be_true
129
129
  cache.key.should == 'cached_foo_you'
130
130
  end
131
+
132
+ it 'passes on correctly the :expires_in option' do
133
+ @view.cache("my_key", :expires_in => 24*3600) do
134
+ puts "foo"
135
+ end
136
+ cache = @view.buffer.last
137
+ cache.expires_in.should == 24*3600
138
+ end
131
139
  end
132
140
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garterbelt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kane Baccigalupi