Listing all models in a repository (reflection)

44 views
Skip to first unread message

jbrennan

unread,
May 2, 2012, 9:25:14 AM5/2/12
to datam...@googlegroups.com
I'm trying to whip up some debug pages based around all the models in my repository and I'm wondering if I can do this dynamically. Is there a way to list all the models in a given repository?

I'm not looking to list all the *objects* in the repo (not yet at least), but all the different kinds of models and their properties. I poked around the docs a little bit but was unable to find what I was looking for.

Any way to do this?

Thanks.

Rhett Sutphin

unread,
May 2, 2012, 10:24:27 AM5/2/12
to datam...@googlegroups.com
Hi,
You can use `DataMapper::Model.descendants` to get all the registered model classes.

Rhett

>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups "DataMapper" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/4BA_b0TnyzsJ.
> To post to this group, send email to datam...@googlegroups.com.
> To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.

Jason Brennan

unread,
May 2, 2012, 12:19:06 PM5/2/12
to datam...@googlegroups.com
Aha! This seems to work for me, thanks.

I checked the docs for the DescendantSet class, and it appears all its methods are private. Is there a proper way to iterate over all its elements?

I see the -each method, but since that's private I'm wondering if this is the best way to go (in case, for example, it changes in the future).

Thanks so much for your help!
Jason.

Markus Schirp

unread,
May 2, 2012, 12:36:00 PM5/2/12
to datam...@googlegroups.com
DescendantSet includes Enumerable so you just do

DataMapper::Model.descendants.each do |model|
model.repository ...
end
> --
> You received this message because you are subscribed to the Google Groups "DataMapper" group.
> To post to this group, send email to datam...@googlegroups.com.
> To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
>

--
Markus Schirp

Phone: +49 201 / 360 379 14
Fax: +49 201 / 360 379 16
Web: www.seonic.net
Email: m...@seonic.net
Twitter: twitter.com/_m_b_j_
OS-Code: github.com/mbj

Seonic IT-Systems GbR
Anton Shatalov & Markus Schirp
Altendorferstrasse 44
D-45127 Essen
Reply all
Reply to author
Forward
0 new messages