-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
some questions to get us started #3
Comments
I'll take a stab briefly:
Then much like an else in a case or if-elseif chain, we have an on default which gets run if no other handler gets called.
But being a subclass of Cuba, you could quite literally run anything else, e.g. class App < Cuba
define do
on root do
res.write 'Home'
end
end
end and in your require File.expand_path("app", File.dirname(__FILE__))
run App I guess it boils down to this:
So what Cuba (and Rum, and Rack::URLMap) does is it mutates The
res = catch :foo do
throw :foo, "bar"
end
assert_equal "bar", res So in Cuba context, that would be: response = catch(:halt) do
throw :halt, [200, { "Content-Type" => "text/html" }, ["Hello world"]]
end Anytime an IF on the event we don't match anything, that's where these lines get executed: res.status = 404
res.finish Which results to our app returning a 404.
# PATH_INFO = /users/1
# SCRIPT_NAME = ""
on "users/:id" do |id|
# PATH_INFO = ""
# SCRIPT_NAME = "/users/1"
SubApp.run(req.env) # gets called with PATH_INFO = "", SCRIPT_NAME = "/users/1"
end Anyway hope these answers shed more light on the questions you posted above. |
Thanks @cyx. I had meant these questions to spur others to delve into the code, but I'm glad to hear some of your thinking about the design "from the inside". And it looks like @RoboDisco has started a code dive now. I hadn't quite grasped how the routing works re. the mutation of BTW I think Cuba is a very nice framework. So much leverage in so few lines of code! You are very careful about state, which is refreshing. And it's nicely documented too. I have a whole set of questions as I start to use it on a real-ish project, is this the best place to ask or is there a mailing list/user group I should be looking at for 'best practices' first ? |
Hi Eric, We typically hang out in #cuba.rb on freenode, we can probably give much better advice if we have more context Thanks, On Jul 26, 2012, at 9:14 PM, Eric Gjertsen wrote:
|
\1. The most striking thing about Cuba coming to it from Rails or Sinatra is the 'finite state machine' routing. The Readme does a good job explaining this, but even so, I find it sometimes hard to get my head around concrete examples. Maybe someone would like to walk us through the
cuba-app
example's routes from the top? This gets somewhat into Shield as well (the authentication library).\2. Another starting point is the config.ru. In the
cuba-app
example, this consists of basically one line after loading the app:run Cuba
. Interesting to consider how this differs from Sinatra, where you would subclassSinatra::Base
and run that. Closely related to this is that routes are defined within aCuba.define
block, rather than directly on the class. And the individual routes are not compiled when defined, and then matched to the request, but evaluated upon each request. Can someone walk us throughCuba.define
?\3. Yet another starting point is to consider when a request comes in, how it is matched to a route, and how captures are extracted. The Readme gives a good explanation of how this works, but it's a bit tricky to work out how it's implemented with nested calls to
on
, and thecatch(:halt)
:\4. Another interesting feature of Cuba compared to Sinatra is it allows you to switch to another handler entirely from within a route, while preserving the current env. (At least, I don't know a straightforward way to do this in Sinatra.) In this way it functions somewhat like
Rack::URLMap
(as can be seen in the top-level routes in thecuba-app
example). Also, the inline example in the code shows how to use this to implement aredirect
helper. Anyone care to walk us through this?The text was updated successfully, but these errors were encountered: