sinatra-cache 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -4,7 +4,7 @@ require 'sinatra/outputbuffer'
4
4
 
5
5
  module Sinatra
6
6
  module Cache
7
- VERSION = '0.3.1' unless const_defined?(:VERSION)
7
+ VERSION = '0.3.2' unless const_defined?(:VERSION)
8
8
  def self.version; "Sinatra::Cache v#{VERSION}"; end
9
9
  end #/ Cache
10
10
  end #/ Sinatra
@@ -26,7 +26,7 @@ module Sinatra
26
26
  cache_enabled = settings.respond_to?(:cache_enabled) ? settings.send(:cache_enabled) : false
27
27
  cache_output_dir = settings.send(:cache_output_dir) if settings.respond_to?(:cache_output_dir)
28
28
  # raise Exception, "The Sinatra::Cache cache_output_dir variable is pointing to a non-existant directory cache_output_dir=[#{cache_output_dir}]" unless test(?d, cache_output_dir)
29
- cache_option = options.delete(:cache)
29
+ cache_option = options[:cache]
30
30
  cache_option = true if cache_option.nil?
31
31
 
32
32
  # compile and render template
@@ -39,7 +39,7 @@ module Sinatra
39
39
  options = options.merge(:views => views, :layout => false)
40
40
  output = render(engine, layout, options, locals) { output }
41
41
  # Cache the content or just return it
42
- (cache_enabled && settings.send(:environment) == settings.cache_environment) ?
42
+ (cache_enabled && cache_option && settings.send(:environment) == settings.cache_environment) ?
43
43
  cache_write_file(cache_file_path, output.gsub(/\n\r?$/,"")) : output
44
44
  rescue Errno::ENOENT
45
45
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra-cache}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["kematzy"]
12
- s.date = %q{2010-03-02}
12
+ s.date = %q{2010-04-27}
13
13
  s.description = %q{A Sinatra Extension that makes Page and Fragment Caching easy.}
14
14
  s.email = %q{kematzy@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -21,8 +21,16 @@ describe "Sinatra" do
21
21
 
22
22
  set :cache_enabled, true
23
23
  set :cache_environment, :test
24
- set :cache_output_dir, "#{public_fixtures_path}/system/cache"
25
- set :cache_fragments_output_dir, "#{public_fixtures_path}/system/cache_fragments"
24
+ set :cache_output_dir, "#{test_cache_path('system/cache')}"
25
+ set :cache_fragments_output_dir, "#{test_cache_path('system/cache')}_fragments"
26
+
27
+ # NB! Although without tests, the positioning of the custom method in relation to other
28
+ # Cache related declaration has no effect.
29
+ helpers do
30
+ def uncached_erb(template, options={})
31
+ erb(template, options.merge(:cache => false ))
32
+ end
33
+ end
26
34
 
27
35
  get('/') { erb(:index) }
28
36
  get('/erb/?') { erb(:index, :layout => false) }
@@ -46,6 +54,11 @@ describe "Sinatra" do
46
54
  end
47
55
  end
48
56
 
57
+ ## NO CACHING
58
+ get('/uncached/erb'){ erb(:index, :cache => false) }
59
+ get('/uncached/erb/no/layout'){ erb(:index, :cache => false, :layout => false ) }
60
+ get('/uncached/uncached_erb'){ uncached_erb(:index) }
61
+
49
62
  get '/css/screen.css' do
50
63
  content_type 'text/css'
51
64
  sass(:css, :style => :compact)
@@ -144,11 +157,11 @@ describe "Sinatra" do
144
157
  end
145
158
 
146
159
  it "should set :cache_output_dir to '../public/system/cache'" do
147
- MyTestApp.cache_output_dir.should == "#{public_fixtures_path}/system/cache"
160
+ MyTestApp.cache_output_dir.should == "#{test_cache_path('system/cache')}"
148
161
  end
149
162
 
150
163
  it "should set :cache_fragments_output_dir to '../public/system/cache_fragments'" do
151
- MyTestApp.cache_fragments_output_dir.should == "#{public_fixtures_path}/system/cache_fragments"
164
+ MyTestApp.cache_fragments_output_dir.should == "#{test_cache_path('system/cache')}_fragments"
152
165
  end
153
166
 
154
167
  it "should set :cache_logging to true" do
@@ -215,7 +228,7 @@ describe "Sinatra" do
215
228
  describe "the Home page - ['/' => /index.html] (with layout)" do
216
229
 
217
230
  before(:each) do
218
- @cache_file = "#{public_fixtures_path}/system/cache/index.html"
231
+ @cache_file = "#{test_cache_path('system/cache')}/index.html"
219
232
  get('/')
220
233
  end
221
234
 
@@ -238,7 +251,7 @@ describe "Sinatra" do
238
251
  describe "a basic URL without a trailing slash - ['/erb' => /erb.html] (ERB without layout)" do
239
252
 
240
253
  before(:each) do
241
- @cache_file = "#{public_fixtures_path}/system/cache/erb.html"
254
+ @cache_file = "#{test_cache_path('system/cache')}/erb.html"
242
255
  get('/erb')
243
256
  end
244
257
 
@@ -259,7 +272,7 @@ describe "Sinatra" do
259
272
  describe "a basic URL with a trailing slash - ['/erb/' => /erb/index.html]" do
260
273
 
261
274
  before(:each) do
262
- @cache_file = "#{public_fixtures_path}/system/cache/erb/index.html"
275
+ @cache_file = "#{test_cache_path('system/cache')}/erb/index.html"
263
276
  get('/erb/')
264
277
  end
265
278
 
@@ -281,7 +294,7 @@ describe "Sinatra" do
281
294
 
282
295
  before(:each) do
283
296
  ext = '.yaml'
284
- @cache_file = "#{public_fixtures_path}/system/cache/file-extensions#{ext}"
297
+ @cache_file = "#{test_cache_path('system/cache')}/file-extensions#{ext}"
285
298
  get("/file-extensions#{ext}")
286
299
  end
287
300
 
@@ -317,14 +330,14 @@ describe "Sinatra" do
317
330
 
318
331
  before(:each) do
319
332
  @file_url = file_url
320
- @cache_file = "#{public_fixtures_path}/system/cache#{file_url}"
333
+ @cache_file = "#{test_cache_path('system/cache')}#{file_url}"
321
334
  @cache_file << ".html" if File.extname(@file_url) == ''
322
335
  # puts "when the URL=[#{url}] the @cache_file=[#{@cache_file}] [#{__FILE__}:#{__LINE__}]"
323
336
  get(url)
324
337
  end
325
338
 
326
339
  after(:each) do
327
- FileUtils.remove_dir("#{public_fixtures_path}/system/cache/params") if @delete_cached_test_files
340
+ FileUtils.remove_dir("#{test_cache_path('system/cache')}/params") if @delete_cached_test_files
328
341
  end
329
342
 
330
343
  it "should render the expected output" do
@@ -343,12 +356,66 @@ describe "Sinatra" do
343
356
 
344
357
  end #/ URLs with multiple levels and/or with ?params attached
345
358
 
359
+ describe "with :cache => false, :layout => false " do
360
+
361
+ before(:each) do
362
+ @cache_file = "#{test_cache_path('system/cache')}/uncached/erb/no/layout.html"
363
+ get('/uncached/erb/no/layout')
364
+ end
365
+
366
+ it "should output the correct HTML as expected" do
367
+ body.should have_tag('h1', 'HOME')
368
+ end
369
+
370
+ it "should NOT cache the output" do
371
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
372
+ test(?f, @cache_file).should == false
373
+ end
374
+
375
+ end #/ with :cache => false, :layout => false
376
+
377
+ describe "with :cache => false" do
378
+
379
+ before(:each) do
380
+ @cache_file = "#{test_cache_path('system/cache')}/uncached/erb.html"
381
+ get('/uncached/erb')
382
+ end
383
+
384
+ it "should output the correct HTML as expected" do
385
+ body.should have_tag('h1', 'HOME')
386
+ end
387
+
388
+ it "should NOT cache the output" do
389
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
390
+ test(?f, @cache_file).should == false
391
+ end
392
+
393
+ end #/ with :cache => false
394
+
395
+ describe "URLs with custom erb helpers, like :admin_erb().." do
396
+
397
+ before(:each) do
398
+ @cache_file = "#{test_cache_path('system/cache')}/uncached/uncached_erb.html"
399
+ get('/uncached/uncached_erb')
400
+ end
401
+
402
+ it "should output the correct HTML as expected" do
403
+ body.should have_tag('html > body > h1', 'HOME')
404
+ end
405
+
406
+ it "should NOT cache the output" do
407
+ test(?d, File.dirname(@cache_file) ).should == false # testing for directory
408
+ test(?f, @cache_file).should == false
409
+ end
410
+
411
+ end #/ URLs with custom erb helpers, like :admin_erb()..
412
+
346
413
  describe "CSS URLs with dynamic .sass files" do
347
414
 
348
415
  describe "the URL ['/css/screen.css' => /css/screen.css]" do
349
416
 
350
417
  before(:each) do
351
- @cache_file = "#{public_fixtures_path}/system/cache/css/screen.css"
418
+ @cache_file = "#{test_cache_path('system/cache')}/css/screen.css"
352
419
  FileUtils.remove_dir(File.dirname(@cache_file), :force => true )
353
420
  get('/css/screen.css')
354
421
  end
@@ -375,7 +442,7 @@ describe "Sinatra" do
375
442
  describe "URLs with ':cache => false' - ['/css/no/cache.css' => /css/no/cache.css]" do
376
443
 
377
444
  before(:each) do
378
- @cache_file = "#{public_fixtures_path}/system/cache/css/no/cache.css"
445
+ @cache_file = "#{test_cache_path('system/cache')}/css/no/cache.css"
379
446
  get('/css/no/cache.css')
380
447
  end
381
448
 
@@ -399,7 +466,7 @@ describe "Sinatra" do
399
466
  describe "POST '/post'" do
400
467
 
401
468
  before(:each) do
402
- @cache_file = "#{public_fixtures_path}/system/cache/post.html"
469
+ @cache_file = "#{test_cache_path('system/cache')}/post.html"
403
470
  post('/post', :test => {:name => "test-#{Time.now.strftime("%Y-%d-%m %H:%M:%S")}", :content => "with content" })
404
471
  end
405
472
 
@@ -416,7 +483,7 @@ describe "Sinatra" do
416
483
  describe "PUT '/put'" do
417
484
 
418
485
  before(:each) do
419
- @cache_file = "#{public_fixtures_path}/system/cache/put.html"
486
+ @cache_file = "#{test_cache_path('system/cache')}/put.html"
420
487
  put('/put', { :test => { :name => "test" }, :_method => "put" })
421
488
  end
422
489
 
@@ -433,7 +500,7 @@ describe "Sinatra" do
433
500
  describe "DELETE '/delete'" do
434
501
 
435
502
  before(:each) do
436
- @cache_file = "#{public_fixtures_path}/system/cache/delete.html"
503
+ @cache_file = "#{test_cache_path('system/cache')}/delete.html"
437
504
  delete('/delete') #, { :test => { :name => "test" }, :_method => "put" })
438
505
  end
439
506
 
@@ -456,7 +523,7 @@ describe "Sinatra" do
456
523
  describe "using NOT shared fragments" do
457
524
 
458
525
  after(:all) do
459
- FileUtils.rm_r("#{public_fixtures_path}/system/cache_fragments/fragments") if @delete_cached_test_files
526
+ FileUtils.rm_r("#{test_cache_path('system/cache')}_fragments/fragments") if @delete_cached_test_files
460
527
  end
461
528
 
462
529
  %w(
@@ -482,7 +549,7 @@ describe "Sinatra" do
482
549
 
483
550
  # the cached fragment has already been found if we get this far,
484
551
  # but just for good measure do we check for the existence of the fragment file.
485
- test(?f, "#{public_fixtures_path}/system/cache_fragments/#{dir_structure}/test_fragment.html").should == true
552
+ test(?f, "#{test_cache_path('system/cache')}_fragments/#{dir_structure}/test_fragment.html").should == true
486
553
  end
487
554
  end
488
555
 
@@ -491,7 +558,7 @@ describe "Sinatra" do
491
558
  describe "using shared fragments" do
492
559
 
493
560
  after(:all) do
494
- FileUtils.rm_r("#{public_fixtures_path}/system/cache_fragments/sharedfragments") if @delete_cached_test_files
561
+ FileUtils.rm_r("#{test_cache_path('system/cache')}_fragments/sharedfragments") if @delete_cached_test_files
495
562
  end
496
563
 
497
564
  describe "when requesting the first URL" do
@@ -518,7 +585,7 @@ describe "Sinatra" do
518
585
 
519
586
  # the cached fragment has already been found if we get this far,
520
587
  # but just for good measure do we check for the existence of the fragment file.
521
- test(?f, "#{public_fixtures_path}/system/cache_fragments/#{dir_structure}/test_fragment.html").should == true
588
+ test(?f, "#{test_cache_path('system/cache')}_fragments/#{dir_structure}/test_fragment.html").should == true
522
589
 
523
590
  # should use the cached fragment rather than cache a new fragment
524
591
  url = '/sharedfragments/2010/02/another-article-02'
@@ -540,23 +607,23 @@ describe "Sinatra" do
540
607
 
541
608
  it "should expire the page ['/params/cache/expire/' => ../cache/params/cache/expire/index.html]" do
542
609
  get('/params/cache/expire/')
543
- test(?f,"#{public_fixtures_path}/system/cache/params/cache/expire/index.html" ).should == true
610
+ test(?f,"#{test_cache_path('system/cache')}/params/cache/expire/index.html" ).should == true
544
611
  lambda {
545
612
  erb_app "<% cache_expire('/params/cache/expire/') %>"
546
613
  }.should_not raise_error(Exception)
547
614
 
548
- test(?f,"#{public_fixtures_path}/system/cache/params/cache/expire/index.html" ).should == false
615
+ test(?f,"#{test_cache_path('system/cache')}/params/cache/expire/index.html" ).should == false
549
616
 
550
617
  end
551
618
 
552
619
  it "should expire the page ['/params/cache/expired' => ../cache/params/cache/expired.html]" do
553
620
  get('/params/cache/expired')
554
- test(?f,"#{public_fixtures_path}/system/cache/params/cache/expired.html" ).should == true
621
+ test(?f,"#{test_cache_path('system/cache')}/params/cache/expired.html" ).should == true
555
622
  lambda {
556
623
  erb_app "<% cache_expire('/params/cache/expired') %>"
557
624
  }.should_not raise_error(Exception)
558
625
 
559
- test(?f,"#{public_fixtures_path}/system/cache/params/cache/expired.html" ).should == false
626
+ test(?f,"#{test_cache_path('system/cache')}/params/cache/expired.html" ).should == false
560
627
 
561
628
  end
562
629
 
@@ -566,20 +633,20 @@ describe "Sinatra" do
566
633
 
567
634
  it "should expire the fragment ['/fragments/cache/expire/' => ../cache_fragments/fragments/cache/expire/test_fragment.html]" do
568
635
  get('/fragments/cache/expire/')
569
- test(?f,"#{public_fixtures_path}/system/cache_fragments/fragments/cache/expire/test_fragment.html" ).should == true
636
+ test(?f,"#{test_cache_path('system/cache')}_fragments/fragments/cache/expire/test_fragment.html" ).should == true
570
637
  lambda {
571
638
  erb_app "<% cache_expire('/fragments/cache/expire/',:fragment => :test_fragment) %>"
572
639
  }.should_not raise_error(Exception)
573
- test(?f,"#{public_fixtures_path}/system/cache/params/cache_fragments/expire/test_fragment.html" ).should == false
640
+ test(?f,"#{test_cache_path('system/cache')}/params/cache_fragments/expire/test_fragment.html" ).should == false
574
641
  end
575
642
 
576
643
  it "should expire the fragment ['/fragments/cache/expired' => ../cache_fragments/fragments/cache/expired/test_fragment.html]" do
577
644
  get('/fragments/cache/expired')
578
- test(?f,"#{public_fixtures_path}/system/cache_fragments/fragments/cache/expired/test_fragment.html" ).should == true
645
+ test(?f,"#{test_cache_path('system/cache')}_fragments/fragments/cache/expired/test_fragment.html" ).should == true
579
646
  lambda {
580
647
  erb_app "<% cache_expire('/fragments/cache/expired',:fragment => :test_fragment) %>"
581
648
  }.should_not raise_error(Exception)
582
- test(?f,"#{public_fixtures_path}/system/cache/params/cache_fragments/expired/test_fragment.html" ).should == false
649
+ test(?f,"#{test_cache_path('system/cache')}/params/cache_fragments/expired/test_fragment.html" ).should == false
583
650
  end
584
651
 
585
652
  end #/ Pages
@@ -44,6 +44,10 @@ def public_fixtures_path
44
44
  "#{fixtures_path}/public"
45
45
  end
46
46
 
47
+ def test_cache_path(ext='')
48
+ "/tmp/sinatra-cache/#{ext}"
49
+ end
50
+
47
51
  class MyTestApp < Sinatra::Base
48
52
 
49
53
  set :app_dir, "#{APP_ROOT}/apps/base"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - kematzy
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-02 00:00:00 +08:00
17
+ date: 2010-04-27 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency