Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Originally by jschementi (http://bit.ly/1RTt2H) Updated for Sinatra 1.0 by rtomayko
- Loading branch information
1 parent
f375530
commit 4e50ddb
Showing
3 changed files
with
131 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome! i was just going to start this today, but this will save me that trouble :)
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow! Greate feature! Thanks! I wanted for this feature ...
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Just a few modified code lines but feature is great.
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, finally, good job!
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have never needed this feature once.
What are you guys doing wrong?
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its slightly useless in some ways. since you can consider "before" the next request, "after" the last, which i use frequently
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
benschwarz: I wanted to persist an object to a file on disk after each request without having to explicitly call something in each handler.
visionmedia: that's interesting but you lose the context, so any instance variables set in the previous request are gone.
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true. thats how i was dealing with it previously tho, totally welcome this feature :)
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Now how about an around filter? I want to wrap each request in profiling, logging, and activating/deactivating the ActiveRecord cache. These are things best done surrounding a yield (and unfortunately the API for the ActiveRecord cache can only be called via yield as far as I can tell).
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is an around filter something that may be more suited to a custom middleware?
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could just do all that with #before / #after no?
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plus middleware is only downstream no? unless you can process stuff after the lowest level app does its thing, in which case thats good to know :D
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
er... when you're talking about sinatra, it is either the lowest level app, or it contains the lowest level app, in either case, the middleware would be "around" the request....
Just a thought...
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
visionmedia: no, unfortunately the AR cache has the following API:
get '/foo' do
ActiveRecord::Base.cache do
...
end
end
You can't call half of a yielding function in 'before' and the other half in 'after'.
hassox: yeah, a rack middleware component (I refuse to use middleware as a singular noun) would likely do it but I haven't seen a Rack Around Filter and I haven't managed to wrap my brain around the Rack dispatch mechanism yet in order to write one. The occasional Google search for Rack::Around and the like has been fruitless... any pointers?
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a really really simple custom around filter. http://gist.github.com/238624 The important parts of a bit of middleware kit is an initialize that at least takes an app argument (the next app in the chain), and a call instance method that takes one argument, that environment hash.
All you have to do then is make sure that you're returning either the downstream response (as per the gist) or a valid rack response. You can check it out in the spec at http://rack.rubyforge.org/doc/SPEC.html
in sinatra then, according to that middleware you could do:
use MyCustomAroundMiddleware
easy around actions :)
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So simple! Thanks very much.
Here's a Query Caching gist in return: http://gist.github.com/238847
4e50ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah cool yeah that makes sense, i have not needed to implement any middleware like that yet but makes perfect sense.