Phlip
3/5/2008 6:04:00 AM
> bc.find_all(search_specs) do |record|
> activate("field1") # this is what the proxy object would do
> record["field1"] = "some value"
> activate("field2") # this is what the proxy object would do
> puts record["field2"]
> end
I'll take a stab at this one without (>cough<) reading the entire thread. We
need a DSL with two requirements - premature optimization, and a declarative
syntax. It might solve the OP's problem, or maybe someone else's problem.
Call it like this:
bc.find_all(search_specs) do |r|
foo = r.get("field1")
r.set("field2", "some value")
bar = r.get("field4") # this is what the proxy object would do
puts bar
end
I threw away the common syntactic sugar, like = for assignment. I think we
could get it back.
The DSL would work in several passes. The first time find_all calls its
block, it passes a write-only object for the 'r'. That object records
interest in field1, ~2, and ~4. And it sticks either 'nil' into 'foo' and
'bar', or it sticks some sentinel value which is impossible for databases to
stick in.
Then find_all sets up a database cursor that SELECTs only those three
fields. Then it plugs the cursor into a real, read-write object for 'r'.
This passes into the block once per record, and efficiently reads or writes
those fields.
The goal of this DSL is users of find_all can write many different search
specs, and can mix and match many different fields inside the block, without
worrying about the pernicious effects of certain database layers (such as
>cough< ActiveRecord) that tend to read everything in a database when the
wind blows.
> You could instead yield to the given block /before/ doing the actual query
> and pass in an object which would simply record the method calls. Then you
> could figure out the fields that need to be activated from that, do the
> query, and replay the method calls on the actual records returned. This
> would get difficult if you tried to do anything beyond basic one-liners
> within the block, but it might work.
Yeah, what he said.
Also, DSLs like ActiveRecord have shown us that a lot more syntactic sugar
is available, beyond my feeb get and set calls...
--
Phlip