require 'rbbt/resource/path' require 'rbbt/tsv/manipulate' module Sinatra module SinatraRbbt def production? ENV.include? "RACK_ENV" and not ENV["RACK_ENV"].nil? and ENV['RACK_ENV'].downcase == "production" end def old_cache(path, check) return false if check.nil? return false if not File.exists? path check = [check] unless Array === check return check.select{|file| not File.exists?(file) or File.mtime(file) > File.mtime(path)}.any? end def cache(type, options, &block) cache_type, update, check = Misc.process_options options, :cache_type, :update, :check check = [check] if not check.nil? and not Array === check name = type + ":" + Misc.hash2md5(options) if production? cache_type ||= :memory else cache_type = :none end case cache_type.to_sym when :sync, :synchronous path = File.join(settings.cache_dir, "sinatra", name) task = Task.setup(:name => "Sinatra cache", :result_type => :string, &block) step = Step.new(path, task, nil, nil, self) step.clean if old_cache(path, check) begin case when (not step.started? and not update == 'check') step.run when (step.started? and update == 'reload') step.clean step.run end rescue end begin case when step.error? status 500 status = step.status || "error" messages = step.messages || [] backtrace = step.info[:backtrace] || [] step.clean "" << status.to_s << " " << "\n" << "" << messages.last << " " << "\n" << "