DataMapper is Viewable
Even when I’m panicking I’m a fan of datamapper. I like how clean it generally makes my code, but I do notice that I tend to have the same queries cropping up from time to time either in the same application or in other apps using my model library. This just wasn’t dry enough for me, so I made dm-is-viewable. It gives sql-like view functionality to DataMapper Resources. It’s a pretty simple plugin, but lets take a look, um kay?
Let’s start with a really simple User class…
class User include DataMapper::Resource belongs_to :location property :id, Serial property :name, String property :username, String property :password, String property :gender, String property :age, Integer property :favorite_color, String property :favorite_number, Integer end class Location include DataMapper::Resource has n, :users property :id, Serial property :name, String property :desc, String end
Making your resource viewable is pretty easy, first grab the dm-is-viewable gem, then say that your resource is viewable. Views take the exact same parameters that you could pass to Resource.all. The only difference is that dm-is-viewable stores them until they are called.
class User include DataMapper::Resource is :viewable #lets create some views create_view :legal_women, :gender => 'female', :age.gt => 18 create_view :serial_killers, :favorite_number => 666, :favorite_color => 'black' #... Resource code *SNIP SNIP* end
See the befores and afters below:
# Legal Women Before User.all(:gender => 'female', :age.gt => 18) # Legal Women After User.view :legal_women # Serial killers before User.all(:favorite_number => 666, :favorite_color => 'black') # Serial killers after User.view :serial_killers # You can also pass further limiting query parameters to #view. # Legal Local Women Before User.all(:gender => 'female', :age.gt => 18, User.location.name => 'Los Angeles') # Legel Local Women After User.view :legal_women, User.location.name => 'Los Angeles' # Passing additional parameters FURTHERS the limitation of records, so.. User.view :legal_women, :gender => 'male' # => Would return nil, the query would essentially be generated as: # SELECT * from users where gender = 'male' and gender = 'female'
Simply, clean, useful. Woot.
Tags: datamapper, dm-is-viewable, ruby

December 5th, 2008 at 2:06 pm
really really spiffy. This should somehow integrate with the named scope stuff too
see http://datamapper.org/doku.php?id=docs:finders#named_scopes
February 8th, 2011 at 12:07 pm
I know im a little off topic, but i just wanted to say i love the layout of your blog.
February 9th, 2011 at 5:52 pm
Off topic is A-ok, and thanks for the compliment!
May 6th, 2011 at 9:13 pm
Certainly imagine which which you stated. Your preferred reason appeared to be on the web the simplest thing to be mindful of. I say to you, I absolutely get irked though individuals think about concerns that these folks just do not comprehend regarding. You maintained to hit the toe nail upon the top as well as defined out the whole issue with no having side-effects , people may take a signal. Can possible be back to get more.