Created
November 5, 2010 01:18
-
-
Save slaskis/663508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataMapper | |
class Collection | |
# A query friendlier group_by method | |
def group_by | |
assoc = {} | |
self.each do |item| | |
key = yield(item) | |
if assoc.has_key? key | |
assoc[key] << item | |
else | |
q = {} | |
item.class.key.each_with_index {|k,i| q[k.name] = item.key[i] } | |
assoc[key] = all(q) | |
end | |
end | |
assoc | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment