garybernhardt/raptor — pull request #49 by tcrayford (2012-04-09 02:57:26+00:00)
Title: Alternate delegation syntax

This introduces new delegation syntax in the router, which looks somewhat like this:

Routes = Raptor::App.new(self) do
path "post" do
create Records::Post, :create!
end

There are several advantages to this syntax:

1) The user can explicitly inject into delegates:

Routes = Raptor::App.new(self) do
queue = #... some sort of persistent queue initialized here
crawler = WebCrawler.new(queue)

path "to-be-crawled" do
create crawler, :enqueue!, :require => :logged_in
end
end

This is the big advantage from my point of view. I want to be able to inject dependencies at startup time, rather than having to write an injector that statically injects an instance of an object.

2) We no longer use strings for describing delegates, instead giving an object and method name

create Records::Posts, :new

is better (imo) than

create :to => 'Records::Posts.new'

3) The syntax is more 'Rubyish'

Instead of specifying classes/objects with strings, we specify them with objects and method names.

Downsides:
----------------

a)
When using class methods: the class *has* to be declared before the router declaration, e.g. the following throws errors:

Routes = Raptor::App.new(self) do
path "post" do
create Records::Post, :create!
end

class Records::Post #first declaration of this class
end

I don't think this is a big issue in practice though, when I was building an app all my records/etc were declared in other files that were required at the start of the Raptor::App file. Might be an issue for small apps though.

b)
I'm somewhat unsure if this is a good idea, even if just because of how icky the implementation is (check out https://github.com/tcrayford/raptor/commit/9cb533cb84a15094d96432d01c1e3e1ddf7c5451#L1R152 !).

The syntax is also *slightly* less terse than our current syntax, but I don't think that's much of an issue from my PoV.

c)
You can't inject instance methods and have raptor instantiate the required object (there's probably some cleanup required to remove this if this goes in).