Skip to content

Instantly share code, notes, and snippets.

@rtomayko
Forked from kematzy/01-NOTES.txt
Created February 22, 2009 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtomayko/68419 to your computer and use it in GitHub Desktop.
Save rtomayko/68419 to your computer and use it in GitHub Desktop.
The "registered" module method was added to help ensure that extensions can
initialize options in a way that guarantees they can be accessed using
"options.option_name" at the request-level or "self.option_name" at the class
level. I've rewritten the examples to illustrate the new usage.
require 'sinatra/base'
module Sinatra
# Page Caching module
#
module Cache
module Helpers
def cache(content)
# NOTE:: this uses 'options' here, but 'self.class' can also be used, albeit only for classic apps
if options.cache_enabled
"I SHOULD CACHE THIS = [#{content}] :cache_dummy = [#{options.cache_dummy}]"
else
"CACHE DISABLED: I'M IGNORING THIS = [#{content}] :cache_dummy = [#{options.cache_dummy}]"
end
end
end
def self.registered(app)
app.helpers(Helpers)
# toggle for cache functionality
app.set :cache_enabled, true
# dummy test to see override in the app
app.set :cache_dummy, "set in [#{self}.registered]"
end
end #/module Cache
register Cache
end #/module Sinatra
## SIMPLE CLASSIC TEST APP: Code taken from [http://www.sinatrarb.com/extensions.html].
require 'rubygems'
require 'sinatra'
require 'sinatra_cache'
# toggle for cache functionality
set :cache_enabled, false
# dummy test to see override in the app
set :cache_dummy, "set in [#{self}]"
get '/' do
cache("Hello World")
end
## SIMPLE MODULAR APP: Code taken from [http://www.sinatrarb.com/extensions.html].
require 'rubygems'
require 'sinatra/base'
require 'sinatra_cache'
class Hello < Sinatra::Base
register Sinatra::Cache
# toggle for cache functionality
set :cache_enabled, false
# dummy test to see override in the app
set :cache_dummy, "set in [#{self}]"
get '/' do
cache("Hello World")
end
end
Hello.run!(:port => 4568) if __FILE__ == $0
## SIMPLE TEST APP: Code taken from [http://www.sinatrarb.com/extensions.html].
require 'rubygems'
require 'sinatra'
require 'sinatra_cache.rb'
class Hello < Sinatra::Default
register Sinatra::Cache
# # toggle for cache functionality
set :cache_enabled, false
# # dummy test to see override in the app
set :cache_dummy, "set in [#{self}]"
get '/' do
cache("Hello World")
end
run! :port => 4569 if __FILE__ == $0 and !reloading?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment