Magritte pre-validation hooks?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Magritte pre-validation hooks?

Ken Treis
Is there an easy way in Magritte to clean up user-supplied data  
before validation? I'm doing something like this:

> Address>>state: aString
> state := aString ifNotNil: [:s | s trimBlanks]
>
> Address class>>descriptionState
> ^(MAStringDescription auto: 'state' label: 'State')
> addCondition: [:value | value trimBlanks size = 2] labelled:  
> 'Please enter a two-character abbreviation';
> yourself

In Rails, ActiveRecord offers a before_validation hook for this kind  
of cleanup. I have mostly used it to coerce empty strings to nil, but  
it looks like Magritte's default string reader at least does that  
part for me.

Should I implement my own StringReader to accomplish this in one place?

--
Ken Treis
Miriam Technologies, Inc.


_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte pre-validation hooks?

Lukas Renggli-2
> Is there an easy way in Magritte to clean up user-supplied data
> before validation? I'm doing something like this:
>
>> Address>>state: aString
>> state := aString ifNotNil: [:s | s trimBlanks]
>>
>> Address class>>descriptionState
>> ^(MAStringDescription auto: 'state' label: 'State')
>> addCondition: [:value | value trimBlanks size = 2] labelled:
>> 'Please enter a two-character abbreviation';
>> yourself
>
> In Rails, ActiveRecord offers a before_validation hook for this kind
> of cleanup. I have mostly used it to coerce empty strings to nil, but
> it looks like Magritte's default string reader at least does that
> part for me.
>
> Should I implement my own StringReader to accomplish this in one  
> place?

There is no such thing in Magritte. Of course there are many places  
where you could introduce such a thing. The StringReader is something  
else, it transforms a string to a real object (e.g. a date object, a  
number, etc).

It would be probably the best if you create a new mechanism, similar  
to the one of the StringReader, that is responsible to do that?

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch



_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: Magritte pre-validation hooks?

Ken Treis
On Jun 2, 2007, at 1:02 AM, Lukas Renggli wrote:

>> Should I implement my own StringReader to accomplish this in one
>> place?
>
> There is no such thing in Magritte. Of course there are many places
> where you could introduce such a thing. The StringReader is something
> else, it transforms a string to a real object (e.g. a date object, a
> number, etc).
>
> It would be probably the best if you create a new mechanism, similar
> to the one of the StringReader, that is responsible to do that?

Hi Lukas,

Even though Rails implements something more generic (pre-validation  
hooks where any transformation could happen), I realized that I  
really only needed it for this one specific situation (trimming  
blanks off of strings and coercing them to nil if they are empty).

So I implemented my own StringReader subclass that overrides one method:

PruningStringReader>>visitStringDescription: aDescription
        | trimmed |
        trimmed := self contents trimBlanks.
        self object: (trimmed isEmpty ifTrue: [nil] ifFalse: [trimmed])

This solves the problem that set me out on this quest in the first  
place. I'm using this as my default string reader, since I can't  
think of any situations in my apps where leading or trailing blanks  
would be significant.

Thanks,

--
Ken Treis
Miriam Technologies, Inc.


_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki