Instead of:
builder.person do |b|
b.name("Jim")
b.phone("555-1234")
end
You could write it like this: builder.tag!('person') do |b|
b.tag!('name', "Jim")
b.tag!('phone', "555-1234")
endI think the ActiveRecord example below is the answer I'm looking for, since it creates a method at runtime based on information that did not exist until runtime. But I need to read up on it to understand it.
So I don't view it has cute but more it being succinct.
builder.person do |b|
b.name = "Jim"
b.phone = "555-1234"
end builder.person do |b|
b.name "Jim"
b.phone "555-1234"
endConversely though I find that using it in ActiveRecord is cute (ie. a bad thing!).
Now IMHO I see a builder as a good case for a method_missing mechanism (I think there were a few others mentioned in thread/post). No magic, just a simple one-to-one relationship between code and what it does (result):
b.name "Jim" => <name>Jim</name>
However I feel something like find_by_name_and_address in ActiveRecord is cute (both in nice and ugly sense!). There is so much more going on underneath an ORM that I'd be wary of the extra layers of complexity upon what is already an (acceptable) impedance mismatch.Bottom line, an ORM is important to me and I don't want any loose cogs lying around! I'd prefer to be explicit here:
# using DBIx::Class
my $p = $schema->resultset('Addresses')->find({ name => '...', address => '...' });
So while find_by_name_and_address is a pretty nice aesthetic its feels more plastic cosmetics compared the elegant cosmetics that Builder solutions provide.Disclaimer: I haven't touched Rails for about 4 years now. I don't actually know for certain that something like find_by_name_and_address did or still uses method_missing. It could actually build methods like these straight from schema introspection. I would guess method_missing was used to avoid a performance hit on building all these at startup? If so then this could be a good argument for using method_missing here!