"Imitation is the sincerest form of flattery."
I've just committed 3 model plugins to the repository:
* identity_map: Ensures a 1=1 correspondence between model objects
and database rows. Also handles cached primary key lookups
and cached lookups of many_to_one associated records. Makes the
following code true:
Sequel::Model.with_identity_map do
Album.filter{(id > 0) & (id < 2)}.first.object_id == \
Album.first(:id=>1).object_id
end
* tactical_eager_loading: Similar to DataMapper's Strategic Eager
Loading, the tactical_eager_loading plugin allows the user to not
call eager on the dataset when loading the records initially and
still have them eagerly loaded. So the following will only issue
two queries:
Sequel::Model.with_identity_map do
Album.filter{id<100}.all do |a|
a.artists
end
end
* lazy_attributes: Allows attributes to be retrieved lazily from
the database, on an as needed basis. Works with the
tactical_eager_loading plugin to load lazy attributes for groups
of objects at once, so the following will only issue two queries:
Album.lazy_attributes :review
Sequel::Model.with_identity_map do
Album.filter{id<100}.all do |a|
a.review
end
end
The tactical_eager_loading plugin depends on the identity_map plugin
and the lazy_attribute plugin depends on both the
tactical_eager_loading and identity_map plugins. Currently Sequel
doesn't have specific support for plugin dependence, so just use the
following if you want to test this stuff out:
Sequel::Model.plugin :identity_map
Sequel::Model.plugin :tactical_eager_loading
Sequel::Model.plugin :lazy_attributes
Make sure to do that before you load your model subclasses, or you
should do so in each model you need.
Here's a Rack middleware that you can use to wrap each request in
an identity map block:
class SequelIdentityMap
def initialize(app)
@app = app
end
def call(env)
Sequel::[email protected](env)}
end
end
For most of the above features to work, you have to be inside a
with_identity_map block, which creates the identity map and ensures
it is removed at the end of the block.
What's amazing is how little Sequel needed to be modified to
support these features. It only took about 6 existing lines, and
the lines of code for each plugin are all quite small:
* identity_map: 55 lines
* tactical_eager_loading: 31 lines
* lazy_attributes: 29 lines
Looks like DataMapper will have to find some other ways to
differentiate itself[1]. ;p
Jeremy
[1] http://datamapper.org/doku.php?id=why_datamapper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---