As promised! There's a single commit in the indexgen branch that does  
this. Here's an example, taken from the code that inspired the feature:


--- phone_number.rb

class Hobo::PhoneNumber < String

  COLUMN_TYPE = :string

  def format
    data = self.gsub(/[.() -]*/, '')
    data =~ /^\d{10}$/ ? data.gsub(/^(\d{3})(\d{3})(\d{4})$/, '\1-\2- 
\3') : self
  end

  def validate
    "is not a valid phone number" unless
      self.blank? || self =~ /^[2-9]\d{2}-\d{3}-\d{4}$/
  end

end

HoboFields.field_types[:phone_number] = Hobo::PhoneNumber

-------------------------

Not tested formatting anything *other* than strings; maybe somebody  
can try it out for the money example from this thread?

--Matt Jones





On Jun 13, 2009, at 6:07 PM, Matt Jones wrote:

> I'm working on migrating a 0.7.2 codebase to current Hobo that does  
> some things that sound a lot like this; look for more info when I  
> get the code cleaned up some...
>
> --Matt Jones
>
> On Jun 10, 2009, at 10:49 PM, Ritchie Young wrote:
>
>> I appreciate all the feedback on this but the more I thought about  
>> it, the more I realised I wasn't really sure what I was asking for.
>>
>> I think I meant for Hobo to detect if the rich-type had particular  
>> callback methods and, if so, to hook them into the model's read and  
>> write methods for that field. In other words, to do automatically,  
>> what I was doing in my example.
>>
>> This behaviour could be confusing though because the application  
>> author might have some other valid reason for wanting to override  
>> those reader and writer methods. I don't know if there's a  
>> resilient way to do this.
>>
>> Then I started to think that what I really wanted was a hook into  
>> the view/controller layers. The <view/> tag and I think the <input/ 
>> > tags already call to_s on the object being displayed. So by  
>> overriding that in the rich-type we're half-way there. If we could  
>> have a from_s method on the rich type that was called when data was  
>> written we could do the conversion there. Only, from_s would have  
>> to be a class method or a constructor because the rich-types  
>> inherit from the base types.
>>
>> At this point I read about ActiveRecord's Aggregate functionality,  
>> which seems to overlap this somewhat, and got really confused.  
>> Which is why I haven't logged an enhancement request on lighthouse.
>>
>> I'm getting by quite nicely, for now, with my original solution:  
>> store it as an integer and convert it in the field getters and  
>> setters.
>>
>>
>> /Ritchie
>>
>> >>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to