Creating Multiple Properties With One Call to #property

Skip to first unread message

Michael Boutros

Feb 1, 2008, 12:47:57 PM2/1/08
to DataMapper
Hello everyone. I was looking over the DataMapper source and first of
all, I have to say awesome job to Sam Smoot and anyone else who
contributed. It really does look great, and it's always readable and I
found my way through the code very easily.

So I started to play around with DM and I noticed that I couldn't
assign multiple properties that all have the same type and options
with one call. Granted, I can do this:
[:property_one, :property_two].each {|p| property p, :string, ...} but
I didn't really like it although I do see how a lot of people would.
So I did what any self-respecting geek would do: I wrote a small
patch. The patch allowed me to do both of the following, with the same

property :name, :string, :length => 30
property :name, :title, :string, :length => 30

The first example is the normal one, and it'd return the new property.
In the second example, an array of the new properties is returned.
However, I went into the IRC channel and some people wanted this:

property [:name, :title], :string, :length => 30

So I went back and worked on it a bit, and now I'm glad to say that
all three of these scenarios work using the _exact_ same code, with no
extra logic required except when the script returns the new properties
(it returns the new property if there's only one, but an array of them
if there's more than one). I wrote a test for it and its all working
dandy, and I'm off to benchmark it right now.

What do you guys think of this? Useful? Pointless? Even if the
community decides not to implement it into the source, I know for sure
I'll be using it in my projects all the time :)

Matthew B Gardner

Feb 1, 2008, 2:41:20 PM2/1/08
to DataMapper
I find it very useful. The second syntax is similar to how Og does it,
and I was a bit disappointed to see that DM didn't. Allowing for all
three is the way to go -- should please everyone.

Christian von Kleist

Feb 4, 2008, 11:09:36 AM2/4/08

This is very nice. I think the array form is most readable, but
either way I'll definitely use it. :)

Reply all
Reply to author
0 new messages