Skip to content

Instantly share code, notes, and snippets.

@kematzy
Created June 26, 2009 05:52
Show Gist options
  • Save kematzy/136321 to your computer and use it in GitHub Desktop.
Save kematzy/136321 to your computer and use it in GitHub Desktop.
### QUESTION:: Why does this behave in this way ??
#
# Summary:
#
# When adding a Sinatra Extension with instance variables that are essentially buffers during the request
# the new values does NOT get added to the instance variable.
#
# Key Issues:
#
# * Compare using 'app' vs '@app' in the tests below.
# * Why does the content not get output during the request?
#
#
# If I'm doing something wrong below, please fork, show & let me know.
require 'rubygems'
require 'sinatra/base'
require 'test/spec' # sorry, using test/spec since rspec via TextMate don't work in single file
require 'rack/test' # using v0.4.0
module Assets
# this should hold all custom css added during the request.
@kematzy_assets_custom_css = []
attr_accessor :kematzy_assets_custom_css
def add_custom_css(css)
@kematzy_assets_custom_css ||= []
@kematzy_assets_custom_css << css
end
def custom_css
@kematzy_assets_custom_css
end
end #/module Assets
##===================================================
## Sinatra Extension
module Sinatra
module Kematzy
module Helpers
include Assets
end #/module Helpers
def self.registered(app)
app.helpers Kematzy::Helpers
end
end #/module Kematzy
end #/module Sinatra
##===================================================
## Sinatra App
class Dummy < Sinatra::Base
register(Sinatra::Kematzy)
set :public, 'public'
set :views, 'views'
get '/' do
erb("<style>#{custom_css}</style>")
end
end
##===================================================
class Test::Unit::TestCase
include Rack::Test::Methods
def app ; Dummy.new() ; end
def setup ; Sinatra::Base.set :environment, :test ; end
end
##===================================================
describe "Sinatra::Kematzy::Helpers" do
describe "should add custom css and store it in the @kematzy_assets_custom_css instance var" do
describe "WHY does using 'app' NOT work ?" do
it "should store it in the @kematzy_assets_custom_css" do
app.add_custom_css('custom')
app.kematzy_assets_custom_css.should == ['custom'] # => returns NIL
end
it "should return the custom css via the #custom_css method" do
app.add_custom_css('custom')
app.custom_css.should == ['custom'] # => returns NIL
end
end
describe "WHY does using '@app' work" do
before(:each) do
@app = app # just making app and instance variable
end
it "should store it in the @kematzy_assets_custom_css" do
@app.add_custom_css('custom')
@app.kematzy_assets_custom_css.should == ['custom'] # => Works as expected
end
it "should return the custom css via the #custom_css method" do
@app.add_custom_css('custom')
@app.custom_css.should == ['custom'] # => Works as expected
end
end #/ WHY does @app work
it "should return the custom css through the response" do
# NOTE:: this test passes IF I use 'sinatra/test' instead of 'rack/test'.
# (obviously with the required code changes)
get '/'
last_response.body.should == '<style>custom</style>' # => does not work :(
end
end
end #/ Sinatra::Kematzy::Helpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment