Created
February 10, 2009 14:47
-
-
Save kematzy/61410 to your computer and use it in GitHub Desktop.
This file contains 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
SINATRA EXTENSIONS & CONFIGURATION OPTIONS:: ( 2009-02-10 22:31 ) | |
Tests made with Edge Sinatra version ( commit 4ab3b3bfcb66ace4f14b35b5d4b259be3742513f ) | |
This is the description of this problem, three (3) test scenarios below: | |
=========================================================== | |
CASE 1: Classic.rb (Classic Sinatra App) | |
=========================================================== | |
This app works in these three modes: | |
-- with the default configurations from within the Sinatra::Cache module, ie: nothing set in classic.rb file | |
-- with the :cache_enabled toggled (true/false) in the classic.rb file. | |
-- with the :cache_dummy configuration collected from default or classic.rb file. | |
In other words, NO MAJOR PROBLEMS HERE | |
HOWEVER, when you change the lines (see sinatra_cache.rb::16-17) to 'self.cache_enabled', ie from 'options' to 'self' | |
then we get the following error when trying to load the page in the browser: | |
NoMethodError - undefined method `cache_enabled' for #<Sinatra::Application:0x5a7ee0> | |
sinatra_cache.rb:26:in `cache' | |
../cache_tests/classic.rb:22:in `GET /' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/methodoverride.rb:24:in `call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/commonlogger.rb:20:in `_call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/commonlogger.rb:13:in `call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:59:in `process' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:32:in `run' | |
../cache_tests/classic.rb:21 | |
Changing the above 'self.cache_enabled' to 'self.class.cache_enabled' makes things work again | |
=========================================================== | |
CASE 2: myapp.rb (MyApp < Sinatra::Base) sub-classed app | |
=========================================================== | |
Performing the same (classic.rb) tests on this file results in the following: | |
-- default configurations are read from the Sinatra::Cache module | |
-- we get a deprecation warning: | |
./sinatra_cache.rb:17: warning: The 'options' class method is deprecated; use 'self' instead. | |
-- no configurations are picked up from myapp.rb. | |
-- when you change the lines (see sinatra_cache.rb::16-17) to 'self.class.cache_enabled' you get | |
the following error: | |
Read error: #<NoMethodError: undefined method 'cache_enabled' for Class:Class> | |
./sinatra_cache.rb:16:in `cache' | |
(__DELEGATE__):2:in `cache' | |
../cache_tests/myapp.rb:27:in `GET /' | |
./vendor/sinatra/lib/sinatra/base.rb:644:in `call' | |
./vendor/sinatra/lib/sinatra/base.rb:644:in `route' | |
./vendor/sinatra/lib/sinatra/base.rb:386:in `instance_eval' | |
./vendor/sinatra/lib/sinatra/base.rb:386:in `route!' | |
./vendor/sinatra/lib/sinatra/base.rb:383:in `catch' | |
./vendor/sinatra/lib/sinatra/base.rb:383:in `route!' | |
./vendor/sinatra/lib/sinatra/base.rb:362:in `each' | |
./vendor/sinatra/lib/sinatra/base.rb:362:in `route!' | |
./vendor/sinatra/lib/sinatra/base.rb:448:in `dispatch!' | |
./vendor/sinatra/lib/sinatra/base.rb:327:in `call!' | |
./vendor/sinatra/lib/sinatra/base.rb:415:in `instance_eval' | |
./vendor/sinatra/lib/sinatra/base.rb:415:in `invoke' | |
./vendor/sinatra/lib/sinatra/base.rb:415:in `catch' | |
./vendor/sinatra/lib/sinatra/base.rb:415:in `invoke' | |
./vendor/sinatra/lib/sinatra/base.rb:327:in `call!' | |
./vendor/sinatra/lib/sinatra/base.rb:316:in `call' | |
./vendor/sinatra/lib/sinatra/base.rb:724:in `call' | |
./vendor/sinatra/lib/sinatra/base.rb:786:in `synchronize' | |
./vendor/sinatra/lib/sinatra/base.rb:721:in `call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:59:in `process' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new' | |
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:32:in `run' | |
./vendor/sinatra/lib/sinatra/base.rb:709:in `run!' | |
../cache_tests/myapp.rb:32 | |
-- when you change the lines (see sinatra_cache.rb::16-17) to 'self.cache_enabled' the app works again, | |
but no configurations are picked up from the myapp.rb file. | |
=========================================================== | |
CASE 3: myapp_default.rb (MyApp < Sinatra::Default) | |
=========================================================== | |
Performing the same (classic.rb) tests on this file results in the following: | |
-- default configurations are read from the Sinatra::Cache module | |
-- we get a deprecation warning: | |
./sinatra_cache.rb:27: warning: The 'options' class method is deprecated; use 'self' instead. | |
-- no configurations are picked up from myapp_default.rb. | |
-- when you change the lines (see sinatra_cache.rb::16-17) to self.class.cache_enabled you get | |
the following error: | |
NoMethodError - undefined method `cache_enabled' for Class:Class: | |
sinatra_cache.rb:26:in `cache' | |
(__DELEGATE__):2:in `cache' | |
../cache_tests/myapp_default.rb:46:in `GET /' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/methodoverride.rb:24:in `call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/commonlogger.rb:20:in `_call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/commonlogger.rb:13:in `call' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:59:in `process' | |
.../lib/ruby/gems/1.8/gems/rack-0.9.1/lib/rack/handler/mongrel.rb:32:in `run' | |
.../cache_tests/myapp_default.rb:51 | |
-- when you change the lines (see sinatra_cache.rb::16-17) to 'self.cache_enabled' no configurations are picked up | |
from the myapp_default.rb file. | |
=========================================================== | |
CONCLUSIONS: | |
=========================================================== | |
-- It is currently (2009-02-10) impossible to write an extension that depends on configurations and | |
that works both in 'classic' and sub-classed modes. | |
I would consider this a major failing, since configurations of module/extension functionality is very important. | |
=========================================================== | |
Apologies for not being able to write a test suite to highlight these problems, but I couldn't make the tests work. Perhaps that's something for the FAQ or the Extensions pages on the website? ie: how to create a Sinatra app for testing purposes only. | |
This file contains 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 | |
# toggle for cache functionality | |
set :cache_enabled, true | |
# dummy test to see override in the app | |
set :cache_dummy, "set in [#{self}]" | |
def cache(content) | |
# NOTE:: this uses 'options' here, but 'self.class' can also be used, albeit only for classic apps | |
if self.class.cache_enabled | |
# 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 #/module Cache | |
register Cache | |
end #/module Sinatra |
This file contains 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]. | |
# | |
## ONLY DIFFERENCES IN THIS VERSION IS: | |
# | |
# - loads EDGE Sinatra, since the gem (0.9.0.4) does not support extensions | |
# - adds load and include of sinatra_cache.rb / Sinatra::Cache | |
# - adds a set :cache_enabled / :cache_dummy configurations | |
# | |
### SEE NOTES.txt for further info. | |
require 'rubygems' | |
require 'vendor/sinatra/lib/sinatra.rb' | |
require 'sinatra_cache.rb' | |
include 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 | |
#/EOF |
This file contains 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]. | |
# | |
## ONLY DIFFERENCES IN THIS VERSION IS: | |
# | |
# - loads EDGE Sinatra, since the gem (0.9.0.4) does not support extensions | |
# - adds load of sinatra_cache.rb and register(Sinatra::Cache) | |
# - adds a set :cache_enabled / :cache_dummy configurations | |
# - Hello.run!(:port => 4568 ) at the EOF | |
# - runs on port 4568 | |
### SEE NOTES.txt for further info. | |
require 'rubygems' | |
require 'vendor/sinatra/lib/sinatra.rb' | |
require 'sinatra/base' | |
require 'sinatra_cache.rb' | |
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 ) | |
#/EOF |
This file contains 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]. | |
# | |
## ONLY DIFFERENCES IN THIS VERSION IS: | |
# | |
# - loads EDGE Sinatra, since the gem (0.9.0.4) does not support extensions | |
# - inherits from Sinatra::Default instead of Base | |
# - adds support for :reloading? to limit the number of reloadings happening | |
# - adds load of sinatra_cache.rb and register(Sinatra::Cache) | |
# - adds a set :cache_enabled / :cache_dummy configurations | |
# - added Hello.run!(:port => 4569 ) unless Hello.reloading? at the EOF | |
# - runs on port 4569 | |
# | |
### SEE NOTES.txt for further info. | |
require 'rubygems' | |
require 'vendor/sinatra/lib/sinatra.rb' | |
require 'sinatra/base' | |
require 'sinatra_cache.rb' | |
# Sinatra has an issue with .reloading? being private, so monkey patching it here | |
# otherwise we get this multiple restarts malarkey going on. | |
# == Sinatra/0.9.0.4 has taken the stage on 4569 for development with backup from Mongrel | |
# == Sinatra/0.9.0.4 has taken the stage on 4569 for development with backup from Mongrel | |
# == Someone is already performing on port 4569! | |
# 127.0.0.1 - - [05/Feb/2009 22:48:36] "GET / HTTP/1.1" 403 8 0.0018 | |
# == Sinatra/0.9.0.4 has taken the stage on 4569 for development with backup from Mongrel | |
# == Someone is already performing on port 4569! | |
# 127.0.0.1 - - [05/Feb/2009 22:48:36] "GET /favicon.ico HTTP/1.1" 403 8 0.0008 | |
# | |
class Sinatra::Base | |
class << self | |
public :reloading? | |
end | |
end | |
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 | |
end | |
Hello.run!(:port => 4569 ) unless Hello.reloading? | |
#/EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment