From b9c7f51f7693b741f9dbb9e04cad7da866efd573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 16 Jan 2013 23:04:38 +0100 Subject: add lifetime parameter to #static! --- lib/zorglub/node.rb | 24 ++++++++++++++---------- spec/node_spec.rb | 8 ++++++++ spec/spec_helper.rb | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb index 1e7c735..3cc4d8d 100644 --- a/lib/zorglub/node.rb +++ b/lib/zorglub/node.rb @@ -1,5 +1,7 @@ # -*- coding: UTF-8 -*- # +require 'fileutils' +# module Zorglub # class Node @@ -10,7 +12,7 @@ module Zorglub # class << self # - attr_reader :static + attr_reader :static, :cache_lifetime # def engine! engine @engine = engine @@ -34,8 +36,9 @@ module Zorglub @layout end # - def static! val + def static! val, lifetime=0 @static = ( (val==true or val==false) ? val : false ) + @cache_lifetime = lifetime end # def layout_base_path! path @@ -83,8 +86,9 @@ module Zorglub File.join(self.class.view_base_path, @view)+ext end # - def static! val + def static! val, lifetime=0 @static = ( (val==true or val==false) ? val : false ) + @cache_lifetime = lifetime end # def static @@ -257,6 +261,7 @@ module Zorglub @layout = ( partial ? nil : self.class.layout ) @view = r(meth) @static = self.class.static + @cache_lifetime = self.class.cache_lifetime self.class.cli_vals.each do |s,v| cli_val s, *v end end # @@ -288,17 +293,16 @@ module Zorglub end # def static_page! path - if not File.exists? path - compile_page! - Dir.mkdir app.static_base_path - Dir.mkdir File.dirname path - File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); } - puts " * cache file created : #{path}" if @debug - else + if File.exists?(path) and ( @cache_lifetime.nil? or @cache_lifetime==0 or ( (Time.now-File.stat(path).mtime) < @cache_lifetime ) ) puts " * use cache file : #{path}" if @debug content = File.open(path, 'r') {|f| f.read } @content = content.sub /^@mime:(.*)\n/,'' @mime = $1 + else + compile_page! + FileUtils.mkdir_p File.dirname(path) + File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); } + puts " * cache file created : #{path}" if @debug end end # diff --git a/spec/node_spec.rb b/spec/node_spec.rb index a125d56..654966f 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -187,6 +187,14 @@ describe Zorglub do r = Node6.my_call '/no_static' r.body[0].should == 'VAL 4' r.header['Content-type'].should == 'text/static' + r = Node6.my_call '/do_static' + r.body[0].should == 'VAL 1' + r.header['Content-type'].should == 'text/static' + Node6.static! true, 0.000001 + sleep 0.0001 + r = Node6.my_call '/do_static' + r.body[0].should == 'VAL 6' + r.header['Content-type'].should == 'text/static' end # it "redirect should work" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 262e966..d678c81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -152,7 +152,7 @@ class Node6 < Zorglub::Node attr_accessor :static_cpt end attr_reader :value - static! true + static! true, 1 def no_static static! false engine! 'static' -- cgit v1.1-2-g2b99