sinatra-cache 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sinatra/cache.rb +1 -1
- data/lib/sinatra/cache/helpers.rb +1 -1
- data/sinatra-cache.gemspec +2 -2
- data/spec/sinatra/cache_spec.rb +190 -57
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.7
|
data/lib/sinatra/cache.rb
CHANGED
@@ -416,7 +416,7 @@ module Sinatra
|
|
416
416
|
content_2_cache << block_content
|
417
417
|
content_2_cache << "<!-- /cache fragment: #{fragment_name} cached at [ #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] -->\n"
|
418
418
|
else
|
419
|
-
content_2_cache
|
419
|
+
content_2_cache = block_content
|
420
420
|
end
|
421
421
|
# 6. write it to cache
|
422
422
|
cache_write_file(cf, content_2_cache)
|
data/sinatra-cache.gemspec
CHANGED
@@ -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.
|
8
|
+
s.version = "0.3.7"
|
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{
|
12
|
+
s.date = %q{2011-04-08}
|
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 = [
|
data/spec/sinatra/cache_spec.rb
CHANGED
@@ -23,6 +23,7 @@ describe "Sinatra" do
|
|
23
23
|
set :cache_environment, :test
|
24
24
|
set :cache_output_dir, "#{test_cache_path('system/cache')}"
|
25
25
|
set :cache_fragments_output_dir, "#{test_cache_path('system/cache')}_fragments"
|
26
|
+
set :cache_fragments_wrap_with_html_comments, false
|
26
27
|
|
27
28
|
# NB! Although without tests, the positioning of the custom method in relation to other
|
28
29
|
# Cache related declaration has no effect.
|
@@ -140,6 +141,10 @@ describe "Sinatra" do
|
|
140
141
|
MyDefaultsTestApp.cache_logging_level.should == :info
|
141
142
|
end
|
142
143
|
|
144
|
+
it "should set :cache_fragments_wrap_with_html_comments to true" do
|
145
|
+
MyDefaultsTestApp.cache_fragments_wrap_with_html_comments.should == true
|
146
|
+
end
|
147
|
+
|
143
148
|
end #/ with Default Settings
|
144
149
|
|
145
150
|
describe "with Custom Settings" do
|
@@ -172,6 +177,10 @@ describe "Sinatra" do
|
|
172
177
|
MyTestApp.cache_logging_level.should == :info
|
173
178
|
end
|
174
179
|
|
180
|
+
it "should set :cache_fragments_wrap_with_html_comments to false" do
|
181
|
+
MyTestApp.cache_fragments_wrap_with_html_comments.should == false
|
182
|
+
end
|
183
|
+
|
175
184
|
end #/ Custom
|
176
185
|
|
177
186
|
end #/ Configuration
|
@@ -520,84 +529,208 @@ describe "Sinatra" do
|
|
520
529
|
|
521
530
|
describe "and Fragment caching" do
|
522
531
|
|
523
|
-
describe "
|
532
|
+
describe "with default settings" do
|
524
533
|
|
525
|
-
|
526
|
-
FileUtils.rm_r("#{test_cache_path('system/cache')}_fragments/fragments") if @delete_cached_test_files
|
527
|
-
end
|
534
|
+
def app; ::MyTestApp.new ; end
|
528
535
|
|
529
|
-
|
530
|
-
/fragments /fragments/a/b/ /fragments/with/param/?page=1
|
531
|
-
/fragments/dasherised-urls/works-as-well
|
532
|
-
).each do |url|
|
536
|
+
describe "using NOT shared fragments" do
|
533
537
|
|
534
|
-
|
535
|
-
|
538
|
+
after(:all) do
|
539
|
+
FileUtils.rm_r("#{test_cache_path('system/cache')}_fragments/fragments") if @delete_cached_test_files
|
540
|
+
end
|
536
541
|
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
542
|
-
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
542
|
+
%w(
|
543
|
+
/fragments /fragments/a/b/ /fragments/with/param/?page=1
|
544
|
+
/fragments/dasherised-urls/works-as-well
|
545
|
+
).each do |url|
|
543
546
|
|
544
|
-
|
545
|
-
|
546
|
-
body.should have_tag('h1','FRAGMENTS', :count => 1)
|
547
|
-
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
548
|
-
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
547
|
+
params_free_url = url =~ /\?/ ? url.split('?').first.chomp('?') : url
|
548
|
+
dir_structure = params_free_url.gsub(/^\//,'').gsub(/\/$/,'')
|
549
549
|
|
550
|
-
|
551
|
-
|
552
|
-
|
550
|
+
it "should cache the fragments from the URL [#{url}] as [#{dir_structure}/test_fragment.html]" do
|
551
|
+
get(url)
|
552
|
+
# body.should have_tag(:debug)
|
553
|
+
body.should have_tag('h1','FRAGMENTS', :count => 1)
|
554
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
555
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
556
|
+
|
557
|
+
# render the page a 2nd time from cache
|
558
|
+
get(url)
|
559
|
+
body.should have_tag('h1','FRAGMENTS', :count => 1)
|
560
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
561
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
562
|
+
|
563
|
+
# the cached fragment has already been found if we get this far,
|
564
|
+
# but just for good measure do we check for the existence of the fragment file.
|
565
|
+
test(?f, "#{test_cache_path('system/cache')}_fragments/#{dir_structure}/test_fragment.html").should == true
|
566
|
+
end
|
553
567
|
end
|
554
|
-
|
568
|
+
|
569
|
+
end #/ using NOT shared fragments
|
570
|
+
|
571
|
+
describe "using shared fragments" do
|
572
|
+
|
573
|
+
after(:all) do
|
574
|
+
FileUtils.rm_r("#{test_cache_path('system/cache')}_fragments/sharedfragments") if @delete_cached_test_files
|
575
|
+
end
|
576
|
+
|
577
|
+
describe "when requesting the first URL" do
|
578
|
+
|
579
|
+
# FIXME:: work out some clever way to split all of these tests into single items instead of in one big blob
|
580
|
+
|
581
|
+
it "should cache the fragment based on the URL and use it on subsequent requests by URLs sharing the same root URL" do
|
582
|
+
url = '/sharedfragments/2010/02/some-article-01'
|
583
|
+
params_free_url = url =~ /\?/ ? url.split('?').first.chomp('?') : url
|
584
|
+
dir_structure = params_free_url.gsub(/^\//,'').gsub(/\/$/,'')
|
585
|
+
dirs = dir_structure.split('/')
|
586
|
+
dir_structure = dirs.first(dirs.length-1).join('/')
|
587
|
+
|
588
|
+
get(url)
|
589
|
+
# body.should have_tag(:debug)
|
590
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
591
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
592
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
593
|
+
|
594
|
+
get(url)
|
595
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
596
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
597
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
598
|
+
|
599
|
+
# the cached fragment has already been found if we get this far,
|
600
|
+
# but just for good measure do we check for the existence of the fragment file.
|
601
|
+
test(?f, "#{test_cache_path('system/cache')}_fragments/#{dir_structure}/test_fragment.html").should == true
|
602
|
+
|
603
|
+
# should use the cached fragment rather than cache a new fragment
|
604
|
+
url = '/sharedfragments/2010/02/another-article-02'
|
605
|
+
get(url)
|
606
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
607
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
608
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
609
|
+
end
|
610
|
+
|
611
|
+
end #/ when requesting the first URL
|
612
|
+
|
613
|
+
end #/ using shared fragments
|
555
614
|
|
556
|
-
end #/
|
615
|
+
end #/ with default settings
|
557
616
|
|
558
|
-
describe "
|
617
|
+
describe "with :cache_fragments_wrap_with_html_comments enabled" do
|
559
618
|
|
560
|
-
|
561
|
-
|
619
|
+
class MyWrapTestApp < Sinatra::Base
|
620
|
+
|
621
|
+
set :app_dir, "#{APP_ROOT}/apps/base"
|
622
|
+
set :public, "#{fixtures_path}/public"
|
623
|
+
set :views, "#{app_dir}/views"
|
624
|
+
|
625
|
+
register(Sinatra::Tests)
|
626
|
+
|
627
|
+
enable :raise_errors
|
628
|
+
|
629
|
+
register(Sinatra::Cache)
|
630
|
+
|
631
|
+
# need to set the root of the app for the default :cache_fragments_output_dir to work
|
632
|
+
set :root, ::APP_ROOT
|
633
|
+
|
634
|
+
set :cache_enabled, true
|
635
|
+
set :cache_environment, :test
|
636
|
+
set :cache_output_dir, "#{test_cache_path('system/cache')}"
|
637
|
+
set :cache_fragments_output_dir, "#{test_cache_path('system/cache')}_wrap_fragments"
|
638
|
+
set :cache_fragments_wrap_with_html_comments, true
|
639
|
+
|
640
|
+
get('/') { erb(:index) }
|
641
|
+
|
642
|
+
get %r{^/fragments/?([\s\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?} do
|
643
|
+
erb(:fragments, :layout => false, :cache => false)
|
644
|
+
end
|
645
|
+
get %r{^/sharedfragments/?([\s\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?/?([\w-]+)?} do
|
646
|
+
erb(:fragments_shared, :layout => false, :cache => false)
|
647
|
+
end
|
648
|
+
|
562
649
|
end
|
563
650
|
|
564
|
-
|
651
|
+
def app; ::MyWrapTestApp.new ; end
|
652
|
+
|
653
|
+
describe "using NOT shared fragments" do
|
654
|
+
|
655
|
+
after(:all) do
|
656
|
+
FileUtils.rm_r("#{test_cache_path('system/cache')}_wrap_fragments/fragments") if @delete_cached_test_files
|
657
|
+
end
|
565
658
|
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
659
|
+
%w(
|
660
|
+
/fragments /fragments/a/b/ /fragments/with/param/?page=1
|
661
|
+
/fragments/dasherised-urls/works-as-well
|
662
|
+
).each do |url|
|
663
|
+
|
570
664
|
params_free_url = url =~ /\?/ ? url.split('?').first.chomp('?') : url
|
571
665
|
dir_structure = params_free_url.gsub(/^\//,'').gsub(/\/$/,'')
|
572
|
-
dirs = dir_structure.split('/')
|
573
|
-
dir_structure = dirs.first(dirs.length-1).join('/')
|
574
666
|
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
667
|
+
it "should cache the fragments from the URL [#{url}] as [#{dir_structure}/test_fragment.html]" do
|
668
|
+
get(url)
|
669
|
+
# body.should have_tag(:debug)
|
670
|
+
body.should have_tag('h1','FRAGMENTS', :count => 1)
|
671
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
672
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
673
|
+
|
674
|
+
# render the page a 2nd time from cache
|
675
|
+
get(url)
|
676
|
+
body.should have_tag('h1','FRAGMENTS', :count => 1)
|
677
|
+
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
678
|
+
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
679
|
+
|
680
|
+
# the cached fragment has already been found if we get this far,
|
681
|
+
# but just for good measure do we check for the existence of the fragment file.
|
682
|
+
test(?f, "#{test_cache_path('system/cache')}_wrap_fragments/#{dir_structure}/test_fragment.html").should == true
|
683
|
+
end
|
684
|
+
end
|
685
|
+
|
686
|
+
end #/ using NOT shared fragments
|
687
|
+
|
688
|
+
describe "using shared fragments" do
|
689
|
+
|
690
|
+
after(:all) do
|
691
|
+
FileUtils.rm_r("#{test_cache_path('system/cache')}_wrap_fragments/sharedfragments") if @delete_cached_test_files
|
692
|
+
end
|
693
|
+
|
694
|
+
describe "when requesting the first URL" do
|
580
695
|
|
581
|
-
|
582
|
-
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
583
|
-
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
584
|
-
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
696
|
+
# FIXME:: work out some clever way to split all of these tests into single items instead of in one big blob
|
585
697
|
|
586
|
-
|
587
|
-
|
588
|
-
|
698
|
+
it "should cache the fragment based on the URL and use it on subsequent requests by URLs sharing the same root URL" do
|
699
|
+
url = '/sharedfragments/2010/02/some-article-01'
|
700
|
+
params_free_url = url =~ /\?/ ? url.split('?').first.chomp('?') : url
|
701
|
+
dir_structure = params_free_url.gsub(/^\//,'').gsub(/\/$/,'')
|
702
|
+
dirs = dir_structure.split('/')
|
703
|
+
dir_structure = dirs.first(dirs.length-1).join('/')
|
704
|
+
|
705
|
+
get(url)
|
706
|
+
# body.should have_tag(:debug)
|
707
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
708
|
+
body.should_not match(/<!-- cache fragment:(.+)test_fragment -->/)
|
709
|
+
body.should_not match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
710
|
+
|
711
|
+
get(url)
|
712
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
713
|
+
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
714
|
+
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
715
|
+
|
716
|
+
# the cached fragment has already been found if we get this far,
|
717
|
+
# but just for good measure do we check for the existence of the fragment file.
|
718
|
+
test(?f, "#{test_cache_path('system/cache')}_wrap_fragments/#{dir_structure}/test_fragment.html").should == true
|
719
|
+
|
720
|
+
# should use the cached fragment rather than cache a new fragment
|
721
|
+
url = '/sharedfragments/2010/02/another-article-02'
|
722
|
+
get(url)
|
723
|
+
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
724
|
+
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
725
|
+
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
726
|
+
end
|
589
727
|
|
590
|
-
|
591
|
-
url = '/sharedfragments/2010/02/another-article-02'
|
592
|
-
get(url)
|
593
|
-
body.should have_tag('h1','FRAGMENTS - SHARED', :count => 1)
|
594
|
-
body.should match(/<!-- cache fragment:(.+)test_fragment -->/)
|
595
|
-
body.should match(/<!-- \/cache fragment: test_fragment cached at \[ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\] -->/)
|
596
|
-
end
|
728
|
+
end #/ when requesting the first URL
|
597
729
|
|
598
|
-
end #/
|
730
|
+
end #/ using shared fragments
|
599
731
|
|
600
|
-
end #/
|
732
|
+
end #/ with customs settings
|
733
|
+
|
601
734
|
|
602
735
|
end #/ and Fragment caching
|
603
736
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 7
|
10
|
+
version: 0.3.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- kematzy
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-04-08 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|