Re: How to avoid $class->new($value) with inflate

[prev] [thread] [next] [Date index for 2005/03/03]

From: Charles Bailey
Subject: Re: How to avoid $class->new($value) with inflate
Date: 14:45 on 03 Mar 2005
--On March 3, 2005 1:18:06 PM +0100 Matija Grabnar 
<matija.grabnar@xxxxx.xx> wrote:

> I have this in my class definition:
> use Date::Manip;
>
> __PACKAGE__->has_a(changed=>'Date::Manip',
>            inflate=>sub {UnixDate(ParseDate(shift),"%e")},
>            deflate=>sub {UnixDate(ParseDate("epoch ".(shift)),
>                       "%Y%m%d%H%M%S")});
> The problem is, Date::Manip doesn't support a routine called 'new', so I
> get
> this error:
> after_set_changed trigger error: Can't locate object method "new" via
> package "Date::Manip" at /usr/share/perl5/Class/DBI/Relationship/HasA.pm
> line 46.
>  at import.pl line 124
>         Class::DBI::_croak('MRTGT::Collection=HASH(0x8b738fc)',
> 'after_set_changed trigger error: Can\'t locate object method ...',
> 'err', '') called at /usr/share/perl5/Class/DBI.pm line 878
>
> "changed" (the field I'm trying to inflate/deflate) is in no way a
> primary key - it's just a data key in the table.
>
> I guess part of my proble is that I don't actualy need a class there -
> the object is a string when deflated, and an integer
> when inflated. But I can't work out how to tell that to has_a...

Your problem here is in the nature of has_a; the notion that the "target" 
attribute is an object is central, so it keeps trying to inflate it to an 
object.  That's deliberate: it lets you chain a custom inflate method with 
the target class's constructor to create the new object.

If you just want to transform a value into a different (non-object) form, 
you can use triggers directly.

I've seen this come up a few times in the last several weeks; maybe it's 
worth writing a small Class::DBI::Relationship::Transform that'd simplify 
the process.  I'll have a crack at it if there's general consensus that 
it'd be a Good Thing.  Any alternate ideas about the name?

--
Regards,
Charles Bailey  < bailey _at_ newman _dot_ upenn _dot_ edu >
Newman Center at the University of Pennsylvania

(message missing)

How to avoid $class->new($value) with inflate
Matija Grabnar 12:18 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Charles Bailey 14:45 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
William McKee 15:17 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Matt S Trout 15:31 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
William McKee 15:40 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Charles Bailey 16:10 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Perrin Harkins 16:15 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
William McKee 16:50 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Perrin Harkins 17:00 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Charles Bailey 17:29 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Perrin Harkins 19:34 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
William Ross 17:10 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Michael Peters 17:08 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Perrin Harkins 17:12 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Perrin Harkins 19:49 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Carl Johnstone 09:56 on 04 Mar 2005

Re: How to avoid $class->new($value) with inflate
Frank Carnovale 23:19 on 03 Mar 2005

Re: How to avoid $class->new($value) with inflate
Frank Carnovale 23:44 on 06 Mar 2005

Re: How to avoid $class->new($value) with inflate
Matt S Trout 17:34 on 03 Mar 2005

Generated at 20:12 on 07 Mar 2005 by mariachi v0.52