in reply to Date conversion with Class::DBI

I just occured to be that the best solution wouldn't have to be a symmetrical get/set or inflate/deflate pair

Maybe data_normalization using cees's multi-format parser + an overriden accessor (but unchanged mutator)

The reason I'm still thinking about this is that my previous accessor solution required an ugly loop for validation which mimicked the innards of Class::DBI::validate_column_values. I'd have to eval and collect the per column errors. Ick.

Brad

  • Comment on Idea Re: Date conversion with Class::DBI

Replies are listed 'Best First'.
Re: Idea Re: Date conversion with Class::DBI
by cees (Curate) on Jul 09, 2003 at 21:41 UTC
    Maybe data_normalization using cees's multi-format parser + an overriden accessor (but unchanged mutator)

    I guess that would probably work as well. I think if I were to take this approach, I would try to be consistent about the functionality change and override the accessor for every has_a relationship (ones that don't relate to another Class::DBI table object). The easiest place to do this is probably in the 'get' method. All accessors appearantly call 'get' to fetch the value (ie $self->date() gets turned into $self->get('date'); ). Also note that 'get' can accept multiple keys to fetch at once.

    sub get { my ($self, @keys) = @_; my @values = $self->SUPER::get(@keys); foreach (@values) { # if $_ is an object, # and it is not a Class::DBI object # and it overloads "" if (UNIVERSAL::isa($_, 'UNIVERSAL') && !UNIVERSAL::isa($_, 'Class::DBI') && overload::Method($_,'""')) { $_ = "$_"; # stringify the object } } return shift @values if @keys == 1; return @values; }

    I did a quick test, and it seems to work. Effectively it will check to see if the value is an object, and stringify it if "" has been overloaded in the object. So it shouldn't care what object you use in a has_a reloationship. As long as it overrides "" it will get stringified before it gets returned.

    Personally I am happy with the way has_a lets you work with objects instead of just plain strings, so I wouldn't go this route myself. But everyone has their own style...

    Cheers,

    Cees