Show
Ignore:
Timestamp:
03/22/08 10:41:18 (6 years ago)
Author:
charsbar
Message:

lang/perl/Catalyst-Model-Jifty-DBI: prepare Collections before loading any Records; 0.05 -> CPAN

Location:
lang/perl/Catalyst-Model-Jifty-DBI/trunk
Files:
9 added
5 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Catalyst-Model-Jifty-DBI/trunk/Changes

    r8255 r8266  
    11Revision history for Catalyst-Model-Jifty-DBI 
     2 
     30.05 2008/03/22 
     4  - prepare implicit Collections before loading any Records 
     5    so as not for JDBI to fail to create relationships 
    26 
    370.04 2008/03/21 
  • lang/perl/Catalyst-Model-Jifty-DBI/trunk/MANIFEST

    r8255 r8266  
    88t/10_testapp.t 
    99t/20_trace.t 
     10t/30_references.t 
    1011t/99_pod.t 
    1112t/99_podcoverage.t 
     
    2021t/TestApp/lib/TestApp/Schema/BookCollection.pm 
    2122t/TestApp/Makefile.PL 
     23t/TestReference/lib/TestReference/Model/JDBI.pm 
     24t/TestReference/lib/TestReference/Schema/Author.pm 
     25t/TestReference/lib/TestReference/Schema/Book.pm 
  • lang/perl/Catalyst-Model-Jifty-DBI/trunk/lib/Catalyst/Model/Jifty/DBI.pm

    r8255 r8266  
    66use base qw( Catalyst::Model Class::Accessor::Fast ); 
    77 
    8 our $VERSION = '0.04'; 
     8our $VERSION = '0.05'; 
    99 
    1010use NEXT; 
     
    5555  no strict 'refs'; 
    5656  my $schema_base = $self->schema_base; 
    57   foreach my $moniker ( findsubmod $schema_base ) { 
     57 
     58  # prepare implicit collections before loading any records. 
     59  # so as not for JDBI to fail to create relationships. 
     60  my %collections; 
     61  my @monikers = findsubmod $schema_base; 
     62  foreach my $moniker ( @monikers ) { 
     63    if ( $moniker =~ /Collection$/ ) { 
     64      $collections{$moniker} = 1; 
     65    } 
     66    else { 
     67      $collections{$moniker.'Collection'} = 0; 
     68    } 
     69  } 
     70  foreach my $moniker ( keys %collections ) { 
     71    next if $collections{$moniker}; 
     72    # perhaps you're too lazy to create Collection class. 
     73    # now we should try creating default one! 
     74    my $package_body = <<"EOT"; 
     75package $moniker; 
     76use strict; 
     77use base qw( Jifty::DBI::Collection ); 
     781; 
     79EOT 
     80      eval $package_body; 
     81      croak "Can't prepare $moniker: $@" if $@; 
     82  } 
     83 
     84  foreach my $moniker ( @monikers ) { 
     85    $moniker->require or croak $@; 
    5886    next if $moniker =~ /Collection$/; 
     87 
    5988    $moniker =~ s/^$schema_base\:://; 
    60  
    6189    *{"${class}::${moniker}::ACCEPT_CONTEXT"} = sub { 
    6290      shift; 
     
    171199 
    172200  my $package = $self->schema_base.'::'.$moniker; 
    173      $package->require or croak "Can't load $package: $@"; 
    174201     $package->new( handle => $handle ); 
    175202} 
     
    184211 
    185212  my $package = $self->schema_base.'::'.$moniker; 
    186      $package->require; 
    187   if ($@) { 
    188     # perhaps you're too lazy to create Collection class. 
    189     # now we should try creating default one! 
    190     my $package_body = <<"EOT"; 
    191 package $package; 
    192 use strict; 
    193 use base qw( Jifty::DBI::Collection ); 
    194 1; 
    195 EOT 
    196     eval $package_body; 
    197     croak "Can't load $package: $@" if $@; 
    198   } 
    199213  $package->new( handle => $handle ); 
    200214} 
  • lang/perl/Catalyst-Model-Jifty-DBI/trunk/t/TestApp/lib/TestApp/Controller/Multi.pm

    r2067 r8266  
    1515 
    1616  # insert default data 
    17   my $book1 = $c->model('JDBI_M')->record('Book', name => 'db1' ); 
    18      $book1->create( 
    19        name => 'Perl Best Practices', 
    20        isbn => '0-596-00173-8', 
    21      ); 
    22      $book1->create( 
    23        name => 'Perl Hacks', 
    24        isbn => '0-596-52674-1', 
    25      ); 
    26  
    27   my $author1 = $c->model('JDBI_M')->record('Author', name => 'db1' ); 
    28      $author1->create( 
    29        name    => 'Damian Conway', 
    30        pauseid => 'DCONWAY', 
    31      ); 
    32      $author1->create( 
    33        name    => 'chromatic', 
    34        pauseid => 'CHROMATIC', 
    35      ); 
    36  
    37   my $book2 = $c->model('JDBI_M')->record('Book', name => 'db2' ); 
    38      $book2->create( 
    39        name => 'Catalyst', 
    40        isbn => '1-84719-095-2', 
    41      ); 
    42      $book2->create( 
    43        name => 'Higher Order Perl', 
    44        isbn => '1-55860-701-3', 
    45      ); 
    46  
    47   my $author2 = $c->model('JDBI_M')->record('Author', name => 'db2' ); 
    48      $author2->create( 
    49        name    => 'Jonathan Rockway', 
    50        pauseid => 'JROCKWAY', 
    51      ); 
    52      $author2->create( 
    53        name    => 'Mark Jason Dominus', 
    54        pauseid => 'MJD', 
    55      ); 
     17  $c->model('JDBI_M')->record('Author', from => 'db1')->create( 
     18    name    => 'Damian Conway', 
     19    pauseid => 'DCONWAY', 
     20  ); 
     21  $c->model('JDBI_M')->record('Author', from => 'db1')->create( 
     22    name    => 'chromatic', 
     23    pauseid => 'CHROMATIC', 
     24  ); 
     25  $c->model('JDBI_M')->record('Author', from => 'db2')->create( 
     26    name    => 'Jonathan Rockway', 
     27    pauseid => 'JROCKWAY', 
     28  ); 
     29  $c->model('JDBI_M')->record('Author', from => 'db2')->create( 
     30    name    => 'Mark Jason Dominus', 
     31    pauseid => 'MJD', 
     32  ); 
     33 
     34  $c->model('JDBI_M')->record('Book', from => 'db1')->create( 
     35    name => 'Perl Best Practices', 
     36    isbn => '0-596-00173-8', 
     37  ); 
     38  $c->model('JDBI_M')->record('Book', from => 'db1')->create( 
     39    name => 'Perl Hacks', 
     40    isbn => '0-596-52674-1', 
     41  ); 
     42  $c->model('JDBI_M')->record('Book', from => 'db2')->create( 
     43    name => 'Catalyst', 
     44    isbn => '1-84719-095-2', 
     45  ); 
     46  $c->model('JDBI_M')->record('Book', from => 'db2')->create( 
     47    name => 'Higher Order Perl', 
     48    isbn => '1-55860-701-3', 
     49  ); 
    5650 
    5751  $c->response->body( 1 ); 
  • lang/perl/Catalyst-Model-Jifty-DBI/trunk/t/TestApp/lib/TestApp/Controller/Single.pm

    r2067 r8266  
    1313 
    1414  # insert default data 
    15   my $book = $c->model('JDBI::Book'); 
    16      $book->create( 
    17        name => 'Perl Best Practices', 
    18        isbn => '0-596-00173-8', 
    19      ); 
    20      $book->create( 
    21        name => 'Perl Hacks', 
    22        isbn => '0-596-52674-1', 
    23      ); 
     15  $c->model('JDBI::Book')->create( 
     16    name => 'Perl Best Practices', 
     17    isbn => '0-596-00173-8', 
     18  ); 
     19  $c->model('JDBI::Book')->create( 
     20    name => 'Perl Hacks', 
     21    isbn => '0-596-52674-1', 
     22  ); 
    2423 
    25   my $author = $c->model('JDBI::Author'); 
    26      $author->create( 
    27        name    => 'Damian Conway', 
    28        pauseid => 'DCONWAY', 
    29      ); 
    30      $author->create( 
    31        name    => 'chromatic', 
    32        pauseid => 'CHROMATIC', 
    33      ); 
     24  $c->model('JDBI::Author')->create( 
     25    name    => 'Damian Conway', 
     26    pauseid => 'DCONWAY', 
     27  ); 
     28  $c->model('JDBI::Author')->create( 
     29    name    => 'chromatic', 
     30    pauseid => 'CHROMATIC', 
     31  ); 
    3432 
    3533  $c->response->body( 1 );