Class-DBI-Loader-Relationship-1.2/0040755000175000017500000000000010020665036015716 5ustar simonsimonClass-DBI-Loader-Relationship-1.2/Relationship.pm0100644000175000017500000000702010020664555020716 0ustar simonsimonpackage Class::DBI::Loader::Relationship; use 5.006; use strict; use warnings; our $VERSION = '1.2'; our $DEBUG = 0; 1; =head1 NAME Class::DBI::Loader::Relationship - Easier relationship specification in CDBI::L =head1 SYNOPSIS use Class::DBI::Loader::Relationship; my $loader = Class::DBI::Loader->new( dsn => "mysql:beerdb", namespace => "BeerDB"); Now instead of saying BeerDB::Brewery->has_many(beers => "BeerDB::Beer"); BeerDB::Beer->has_a(brewery => "BeerDB::Brewery"); BeerDB::Handpump->has_a(beer => "BeerDB::Beer"); BeerDB::Handpump->has_a(pub => "BeerDB::Pub"); BeerDB::Pub->has_many(beers => [ BeerDB::Handpump => 'beer' ]); BeerDB::Beer->has_many(pubs => [ BeerDB::Handpump => 'pub' ]); Just say $loader->relationship( "a brewery produces beers" ); $loader->relationship( "a pub has beers on handpumps" ); =head1 DESCRIPTION This module acts as a mix-in, adding the C method to C. Since C knows how to map between table names and class names, there ought to be no need to replicate the names. In addition, it is common (but not universal) to want reverse relationships defined for has-many relationships, and for has-a relationships to be defined for the linkages surrounding a many-to-many table. The aim of C is to simplify the declaration of common database relationships by providing both of these features. The C takes a string. It recognises table names (singular or plural, for convenience) and extracts them from the "sentence". =cut package Class::DBI::Loader::Generic; use Lingua::EN::Inflect::Number qw(PL to_PL to_S); use Carp; sub relationship { my $self = shift; my $text = shift; my %tables = map { $_ => $_, PL($_) => $_ } $self->tables; my $table_re = join "|", map quotemeta, sort { length $b <=> length $a } keys %tables; croak "Couldn't understand the first object you were talking about" unless $text =~ s/^((an?|the)\s+)?($table_re)\s*//i; my $from = $tables{$3}; my $from_c = $self->find_class($from); $text =~ s/^(might\s+)?\w+(\s+an?)?\s+//i; my $method = "has_many"; $method = "has_a" if $2; $method = "might_have" if $1; croak "Couldn't understand the second object you were talking about" unless $text =~ s/.*?($table_re)\b//i; my $to = $tables{$1}; my $to_c = $self->find_class($to); my $mapper = $method eq "has_many" ? to_PL($to) : to_S($to); if ($text =~ /($table_re)/i) { my $via = $tables{$1}; my $via_c = $self->find_class($via); return "$via_c->has_a(".to_S($from)." => $from_c)\n". "$via_c->has_a(".to_S($to)." => $to_c)\n". "$from_c->$method($mapper => [ $via_c => ".to_S($to)." ])\n". "$to_c->has_many(".to_PL($from)." => [ $via_c => ".to_S($from)." ])\n" if $DEBUG; $via_c->has_a(to_S($from) => $from_c); $via_c->has_a(to_S($to) => $to_c); $from_c->$method($mapper => [ $via_c => to_S($to) ]); $to_c->has_many(to_PL($from) => [ $via_c => to_S($from) ]); return; } return "$from_c->$method($mapper => $to_c);\n". ($method ne "has_a" && "$to_c->has_a(".to_S($from)." => $from_c);\n") if $DEBUG; $from_c->$method($mapper => $to_c); $to_c->has_a(to_S($from) => $from_c) unless $method eq "has_a"; } 1; =head1 AUTHOR Simon Cozens, C =head1 SEE ALSO L. =cut Class-DBI-Loader-Relationship-1.2/README0100644000175000017500000000176507774576372016636 0ustar simonsimonClass/DBI/Loader/Relationship version 0.01 ========================================== The README is used to introduce the module and provide instructions on how to install the module, any machine dependencies it may have (for example C compilers and installed libraries) and any other information that should be provided before the module is installed. A README file is required for CPAN modules since CPAN extracts the README file from a module distribution so that people browsing the archive can use it get an idea of the modules uses. It is usually a good idea to provide version information here so that people can decide whether fixes for the module are worth downloading. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: blah blah blah COPYRIGHT AND LICENCE Put the correct copyright and licence information here. Copyright (C) 2003 A. U. Thor blah blah blah Class-DBI-Loader-Relationship-1.2/Makefile.PL0100644000175000017500000000061407776625062017711 0ustar simonsimonuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Class::DBI::Loader::Relationship', 'VERSION_FROM' => 'Relationship.pm', # finds $VERSION 'PREREQ_PM' => { Class::DBI::Loader => 0, Lingua::EN::Inflect::Number => 1, }, # e.g., Module::Name => 1.1 ); Class-DBI-Loader-Relationship-1.2/Changes0100644000175000017500000000057207776627264017243 0ustar simonsimonRevision history for Perl extension Class::DBI::Loader::Relationship. See http://cvs.simon-cozens.org/viewcvs.cgi/Class-DBI-Loader-Relationship/ for full log 1.1 Tue Jan 6 21:54:47 GMT 2004 - Make reverse relationship has_a, not has_many. 0.01 Wed Dec 31 16:44:42 2003 - original version; created by h2xs 1.21 with options -AX -n Class::DBI::Loader::Relationship Class-DBI-Loader-Relationship-1.2/MANIFEST0100644000175000017500000000021310020665036017040 0ustar simonsimonChanges Makefile.PL MANIFEST README Relationship.pm test.pl META.yml Module meta-data (added by MakeMaker) Class-DBI-Loader-Relationship-1.2/test.pl0100644000175000017500000000200507776627116017251 0ustar simonsimonuse Test::More 'no_plan'; use_ok("Class::DBI::Loader::Relationship"); use Class::DBI::Loader::Generic; my $fake = bless { CLASSES => { reverse( BeerDB::Brewery => "brewery", BeerDB::Beer => "beer", BeerDB::Handpump => "handpump", BeerDB::Pub => "pub" ) } }, 'Class::DBI::Loader::Generic'; $Class::DBI::Loader::Relationship::DEBUG = 1; my $crib1 = <has_many(beers => BeerDB::Beer); BeerDB::Beer->has_a(brewery => BeerDB::Brewery); EOF sub test { my($text, $crib) = @_; is($fake->relationship($text), $crib, $text)} test("a brewery produces beers", $crib1); test("breweries produce beer", $crib1); test("a brewery has a beer", "BeerDB::Brewery->has_a(beer => BeerDB::Beer);\n"); my $crib2 = <has_a(pub => BeerDB::Pub) BeerDB::Handpump->has_a(beer => BeerDB::Beer) BeerDB::Pub->has_many(beers => [ BeerDB::Handpump => beer ]) BeerDB::Beer->has_many(pubs => [ BeerDB::Handpump => pub ]) EOF test("pubs have beer on handpumps", $crib2); Class-DBI-Loader-Relationship-1.2/META.yml0100644000175000017500000000061210020665036017163 0ustar simonsimon# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Class-DBI-Loader-Relationship version: 1.2 version_from: Relationship.pm installdirs: site requires: Class::DBI::Loader: 0 Lingua::EN::Inflect::Number: 1 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.21