-
-
Save rtomayko/68419 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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