Skip to content

Instantly share code, notes, and snippets.

@kematzy
Created February 5, 2009 03:17
Show Gist options
  • Save kematzy/58526 to your computer and use it in GitHub Desktop.
Save kematzy/58526 to your computer and use it in GitHub Desktop.
### NOTES
# This simple cache extension/class works fine in classic_app.rb below.
#
# However, when trying it in the new_app.rb (sub-classed Sinatra) it throws:
#
# NoMethodError: undefined method ‘set’ for Sinatra::Cache:Module
#
# Been re-reading [ http://www.sinatrarb.com/extensions.html ] but still not been able to work out
# what's wrong inside this code.
#
###
require 'sinatra/base'
module Sinatra
module Cache
set :cache_enabled, false
def cache(something)
if self.cache_enabled
"I SHOULD CACHE THIS = [#{something}]"
else
"CACHE DISABLED: I'M IGNORING THIS = [#{something}]"
end
end
private
def private_cache(something)
"I'm private [#{something}]"
end
end #/module Cache
register(Cache)
end #/module Sinatra
APP_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(APP_ROOT)
# need to vendor Sinatra since the gem does NOT support new register features yet.
vendor_sinatra = "#{File.dirname(File.dirname(APP_ROOT))}/vendor/sinatra"
libdir = File.dirname(File.dirname(APP_ROOT)) + '/lib'
$LOAD_PATH.unshift "#{vendor_sinatra}/lib" if test(?d, vendor_sinatra)
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
require 'rubygems'
require 'sinatra'
require 'sinatra/cache'
enable :static, :logging
enable :reloading if development?
set :app_file, __FILE__
set :views, "#{APP_ROOT}/views"
set :cache_enabled, true
helpers do
# NOTE:: in Classic apps you still need to include the Rack::Utils. Why?
include Rack::Utils
alias_method :h, :escape_html
# ... more helpers ...
end
# type anything you want in the URL
get '/:dynamic/?:value?' do
content = "Dynamic Page with :dynamic=[#{params[:dynamic]}]"
content += " and :value=[#{:value}]}" unless params[:value].nil?
content += " and Time now is [#{Time.now.strftime("%Y-%d-%m %H:%M:%S")}]"
cache(erb( content))
end
APP_ROOT = File.expand_path(File.dirname(__FILE__)) unless defined?(APP_ROOT)
# need to vendor Sinatra since the gem does NOT support new register features yet.
vendor_sinatra = "#{File.dirname(File.dirname(APP_ROOT))}/vendor/sinatra"
libdir = File.dirname(File.dirname(APP_ROOT)) + '/lib'
$LOAD_PATH.unshift "#{vendor_sinatra}/lib" if test(?d, vendor_sinatra)
$LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
require 'rubygems'
require 'sinatra/base'
require 'sinatra/cache'
class MyApp < Sinatra::Base
# class MyApp < Sinatra::Default # TEMPORARY FIX?
register Sinatra::Cache
enable :static, :logging
enable :reloading if development?
set :app_file, __FILE__
set :views, "#{APP_ROOT}/views"
set :cache_enabled, true
alias_method :h, :escape_html
# ... more helpers ...
# type anything you want in the URL
get '/:dynamic/?:value?' do
content = "Dynamic Page with :dynamic=[#{params[:dynamic]}]"
content += " and :value=[#{:value}]}" unless params[:value].nil?
content += " and Time now is [#{Time.now.strftime("%Y-%d-%m %H:%M:%S")}]"
cache(erb( content))
end
end
# run only in CLI mode, otherwise skip
if $0 == __FILE__
MyApp.run! unless MyApp.reloading?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment