Re: CDBI::Relationship::Glue ?

[prev] [thread] [next] [Date index for 2004/06/22]

From: Yuval Kogman
Subject: Re: CDBI::Relationship::Glue ?
Date: 17:49 on 22 Jun 2004
--N1GIdlSm9i+YlY4t
Content-Type: multipart/mixed; boundary="eqp4TxRxnD4KrmFZ"
Content-Disposition: inline


--eqp4TxRxnD4KrmFZ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jun 22, 2004 at 21:59:51 +0100, Tony Bowden wrote:
> Go for it!

How about this?

It's being used in my demo site for Maypole/manymany at the moment.

--=20
 ()  Yuval Kogman <nothingmuch@xxxxxxxx.xxx> 0xEBD27418  perl hacker &
 /\  kung foo master: /methinks long and hard, and runs away: neeyah!!!


--eqp4TxRxnD4KrmFZ
Content-Type: application/x-perl
Content-Disposition: attachment; filename="Link.pm"
Content-Transfer-Encoding: quoted-printable

package Class::DBI::Relationship::Link;=0A=0Ause strict;=0Ause warnings;=0A=
=0Ause Lingua::EN::Inflect::Number qw(to_PL);=0A=0Aour $USE_MONIKER =3D 1;=
=0A=0Asub Class::DBI::defines_link {=0A	my $class =3D shift;=0A	$class =3D =
ref $class if ref $class;=0A=0A	my %link;=0A	if (@_ =3D=3D 2){=0A		%link =
=3D map { $_->moniker =3D> $_ } @_;=0A	} elsif (@_ =3D=3D 4){=0A		%link =3D=
 @_;=0A=0A		if (grep { ref && ref ne 'ARRAY' } values %link){=0A			$class->=
_croak("The value parameter for a column may either be a scalar, or an arra=
y reference.");=0A		}=0A	} else { $class->_croak("a lookup table links betw=
een two and only two tables") }=0A=0A	=0A	my ($this, $that) =3D keys %link;=
=0A		=0A	warn("$class has_a $this =3D> $link{$this}");=0A=0A	$class->has_a(=
 $this =3D> $link{$this} );=0A	$class->has_a( $that =3D> $link{$that} );=0A=
=0A	my ($these, $those);=0A=0A	if (ref $link{$this}){=0A		$these =3D $link{=
$this}[1];=0A		$link{$this} =3D $link{$this}[0];=0A	}=0A	if (ref $link{$tha=
t}){=0A		$those =3D $link{$that}[1];=0A		$link{$that} =3D $link{$that}[0];=
=0A	};=0A	=0A	if ($USE_MONIKER){=0A		$these ||=3D $link{$this}->plural_moni=
ker;=0A		$those ||=3D $link{$that}->plural_moniker;=0A	} else {=0A		$these =
=3D to_PL($this);=0A		$those =3D to_PL($that);=0A	}=0A	=0A	warn("$link{$thi=
s} has_many $those =3D> [ $class =3D> $that ]");=0A	=0A	$link{$this}->has_m=
any( $those =3D> [ $class =3D> $that ] );=0A	$link{$that}->has_many( $these=
 =3D> [ $class =3D> $this ] );=0A	=0A	1;=0A}=0A=0A__PACKAGE__; # keep your =
mother happy=0A=0A__END__;=0A=0A=3Dpod=0A=0A=3Dhead1 NAME=0A=0AClass::DBI::=
Relationship::Link - a Class::DBI mixin for terse many to many relationship=
 declaration.=0A=0A=3Dhead1 SYNOPSIS=0A=0A	Role->defines_link( 'Actor', 'Fi=
lm' );=0A=0A=0A	# and then=0A=0A	my @actors =3D $film->actors;	=0A=0A=3Dhea=
d1 DESCRIPTION=0A=0AThis module creates a lazy interface to L<Class::DBI> m=
any to many=0Arelationships.=0A=0AUsing L<Class::DBI> you declare what the =
lookup table has (two=0AL<Class::DBI/has_a> relationships), and then map th=
e two classes so that they=0AL<Class::DBI/has_many> of each other, with a m=
apping through the lookup table=0Aclass. Read about this process in L<Class=
::DBI/"MANY TO MANY RELATIONSHIPS">.=0A=0AThis interface is just a short cu=
t around that, nothing more.=0A=0A=3Dhead1 METHODS=0A=0A=3Dover 4=0A=0A=3Di=
tem defines_link COLUMN =3D> [ CLASS, ACCESSOR ], COLUMN =3D> [ CLASS, ACCE=
SSOR ]=0A=0A=3Ditem defines_link COLUMN =3D> CLASS, COLUMN =3D> CLASS=0A=0A=
=3Ditem defines_link CLASS, CLASS=0A=0AThis method is called on the class t=
hat is the lookup table, and has=0Athree argument list forms.=0A=0AThe firs=
t form and the second form are actually interchangeable, and=0Aspecify a co=
lumn in the lookup table, and the class it talks about, in=0Akey-value-pair=
 form. The key is always the column in the class we're=0Atalking about, and=
 the value is either the package name for the class,=0Aor an array referenc=
e. The array reference form contains the perl=0Apackage name for the class =
as the first argument, and the plural form of=0Athe accessor to generate as=
 the second.=0A=0AThe plural form of an accessor is specified when you don'=
t want the=0Ahas_many accessors generated in the other classes to be the=0A=
L<UNIVERSAL::moniker/plural_moniker> of their counterparts, or a=0AL<Lingua=
::EN::Inflect::Number/to_PL> transformation of the column in the=0Alookup t=
able class.=0A=0AThe two argument form is simply two package names for the =
classes. The=0Aaccessor for each class will be the L<UNIVERSAL::moniker/plu=
ral_moniker>=0Aof the other, and the colum name in the lookup table is assu=
med to be=0Athe L<UNIVERSAL::moniker/moniker> of these classes.=0A=0A=3Dbac=
k=0A=0A=3Dhead1 CLASS VARIABLES=0A=0A=3Dover 4=0A=0A=3Ditem $Class::DBI::Re=
lationship::Link::USE_MONIKER=0A=0ATells L<defines_link> whether to use the=
 plural_moniker of the class to=0Agenerate the plural accessor of the has_m=
any relationship, or to=0Apluralize the column name instead.=0A=0AThe defau=
lt is true, that is, to prefer the plural_moniker, when=0AL<defines_link> b=
uilds the relationships.=0A=0AAlternatively, use the long form of class def=
inition via an array=0Areference to supply your own accessor names. This co=
uld be useful for=0A=0A	$film->stars; # isa actor=0A	$flim->supporting; # a=
lso isa actor, different relationship=0A=0A=3Dback=0A=0A=3Dhead1 AUTHOR=0A=
=0AYuval Kogman <nothingmuch@xxxxxxxx.xxx>=0A=0A=3Dhead1 SEE ALSO=0A=0AL<Cl=
ass::DBI>, L<Class::DBI::Relationship>,=0AL<Class::DBI::Loader::Relationshi=
p>.=0A=0A=3Dhead1 COPYRIGHT & LICENSE=0A=0ABlah blah, same terms as perl.=
=0A=0A=3Dcut=0A
--eqp4TxRxnD4KrmFZ--

--N1GIdlSm9i+YlY4t
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFA2HErVCwRwOvSdBgRAn8XAKCLjhT9EesjGEGoh2wWPIaoP7r4FACfb/R3
zyfn57CSR7aXUbN704qZWlg=
=lyzY
-----END PGP SIGNATURE-----

--N1GIdlSm9i+YlY4t--

(message missing)

CDBI::Relationship::Glue ?
Yuval Kogman 20:45 on 21 Jun 2004

Re: CDBI::Relationship::Glue ?
Tony Bowden 20:59 on 22 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 22:59 on 21 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 17:49 on 22 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 19:45 on 22 Jun 2004

Re: CDBI::Relationship::Glue ?
Tony Bowden 20:02 on 23 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 20:11 on 22 Jun 2004

Re: CDBI::Relationship::Glue ?
Tony Bowden 20:57 on 23 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 23:19 on 22 Jun 2004

Re: CDBI::Relationship::Glue ?
Tony Bowden 07:16 on 24 Jun 2004

Re: CDBI::Relationship::Glue ?
Yuval Kogman 08:16 on 23 Jun 2004

RE: CDBI::Relationship::Glue ?
Addison, Mark 14:54 on 23 Jun 2004

Re: CDBI::Relationship::Glue ?
Tony Bowden 15:24 on 23 Jun 2004

RE: CDBI::Relationship::Glue ?
Addison, Mark 15:52 on 23 Jun 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52