DataMapper extras

Although I no longer use DataMapper I thought I would share some of my code for it in case it can help anyone who has experienced the same problems.

Today I created a GitHub project which includes two helpers:

1. Adding an all_error_messages() method to objects. By default DM’s validation only shows you errors about the object you validate, so if you have, say, an object with several children which you are trying to save, the save fails if any of the children are invalid. This method returns the errors for all related objects.

 

2. Adding a wash() method to the classes. You pass a hash to the method and it returns a hash of only data that maps to the object’s properties (and its potential children). By default DM will simply return an error if you attempt to set properties that don’t exist so this method is useful for stripping data to only valid properties before running something like update()

 

I will add more helpers in due course.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">