On Sep 2, 2007, at 5:17 AM, ry dahl wrote:
> I have a magical proposal for the anti-magic web framework.
>
Personally, I appreciate magic when it's scientifically reasonable :)
> Controller methods do not use arguments and yet expect arguments. This
> is handled through this params hash because we don't know in advance
> what parameters a client could pass to Merb. But in almost every
> instance, it is too much to know all the query parameters - one
> doesn't care if the user threw in an extraneous variable - one only
> cares about the variables we need to process the action.
>
> Instead of using params we could make actions more functional by
> taking advantage of this cool programming concept called 'arguments'.
> An example
>
> class ProductController
> def show(id) # GET /products/show?id=12
> @product = Product.find(id)
> render
> end
>
> def index(page = 1) # GET /products?page=2
> @products = Product.paginate_all(page)
> render
> end
> end
>
I would like this a lot. One side effect might be, however, that the
'params' hash currently in use in the views will no longer be
available. We could, of course, just leave the params hash in as
well, but let's assume we remove it completely. Does this mean that
controllers would have to wrap arguments in instance variables if
it's necessary to pass them along to the view? e.g.:
def index(page = 1, show_extra = true)
@show_extra = show_extra
# ...
end
Perhaps these arguments should become "locals" in the view as well?
> Getting a parameter list for a method is rather difficult, but using
> zenspider's ParseTree, it is possible. (Attached is a proof of
> concept. It is also viewable at http://pastie.caboo.se/93218 )
>
> ParseTree is slow, but it only needs to be run once at load - we can
> cache the argument lists for each method.
>
Would this make Merb a *nix-only framework?
> It is extremely magical, but the magic is limited in scope - we are
> not going to be using this all around the the source tree. Only for
> controller actions.
>
> It does not increase the frame stack and should have zero effect on
> performance.
>
> Advantages:
> - Less to type. No more params[:username], just username
> - Cleaner API. At a glance it is visible what parameters actions take.
The cleaner API is what makes my preference tip towards magic in this
case. Controllers become very readable with this addition.
> - params in its current state is slightly dangerous. Clients can pass
> to it whatever (key, value) pairs they want and Merb will blindly
> symbolizes the keys. These symbols are never garbage collected.
Hmm. I didn't know that.
> - We can automatically raise a BadRequest if the user does not supply
> all of the arguments needed.
>
Nice.
> I would happily implement this if Ezra and the list thinks that this
> would be a welcome feature.
>
Just to throw it out there for discussion, is it possible to prepare
the framework in such a way that we could turn a ParseTree controller
implementation into a plugin?
Regards,
Duane Johnson
(canadaduane)
_______________________________________________
Merb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/merb-devel