made connect_info optional in the case that the underlying schema already has connect...
Brandon L. Black [Wed, 8 Feb 2006 05:29:59 +0000 (05:29 +0000)]
MANIFEST
README
lib/Catalyst/Helper/Model/DBIC/Schema.pm
lib/Catalyst/Model/DBIC/Schema.pm

index 0412395..8161ff7 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -2,10 +2,10 @@ Build.PL
 Changes
 lib/Catalyst/Helper/Model/DBIC/Schema.pm
 lib/Catalyst/Model/DBIC/Schema.pm
+Makefile.PL
 MANIFEST                       This list of files
+META.yml
+README
 t/01use.t
 t/02pod.t
 t/03podcoverage.t
-META.yml
-Makefile.PL
-README
diff --git a/README b/README
index f18d5b2..86e33a5 100644 (file)
--- a/README
+++ b/README
@@ -22,9 +22,14 @@ SYNOPSIS
         # ->schema To access schema methods:
         $c->model('Foo')->schema->source(...);
 
-        # Shortcut to the schema resultset monikers for ->search et al:
-        $c->model('Foo::Bar')->search(...);
-        # is the same as $c->model('Foo')->schema->resultset('Bar')->search(...);
+        # certain ->schema methods (source, resultset, class) have shortcuts
+        $c->model('Foo')->source(...);
+        $c->model('Foo')->resultset(...);
+        $c->model('Foo')->class(...);
+
+        # For resultsets, there's an even quicker shortcut:
+        $c->model('Foo::Bar')
+        # is the same as $c->model('Foo')->resultset('Bar')
 
         # To get the composed schema for making new connections:
         my $newconn = $c->model('Foo')->composed_schema->connect(...);
@@ -49,7 +54,7 @@ CONFIG PARAMETERS
     schema_class
         This is the classname of your DBIx::Class::Schema Schema. It needs
         to be findable in @INC, but it does not need to be underneath
-        "Catalyst::Model::".
+        "Catalyst::Model::". This parameter is required.
 
     connect_info
         This is an arrayref of connection parameters, which are specific to
@@ -58,6 +63,10 @@ CONFIG PARAMETERS
         parameters are your dsn, username, password, and connect options
         hashref.
 
+        This is not required if "schema_class" already has connection
+        information defined in itself (which would be the case for a Schema
+        defined by DBIx::Class::Schema::Loader, for instance).
+
     storage_type
         Allows the use of a different "storage_type" than what is set in
         your "schema_class" (which in turn defaults to "::DBI" if not set in
@@ -67,16 +76,21 @@ CONFIG PARAMETERS
 
 METHODS
     new Instantiates the Model based on the above-documented ->config
-        parameters.
+        parameters. The only required parameter is "schema_class".
+        "connect_info" is required in the case that "schema_class" does not
+        already have connection information defined for it.
 
     schema
         Accessor which returns the connected schema being used by the this
-        model.
+        model. There are already direct shortcuts on the model class itself
+        for schema->resultset, schema->source, and schema->class.
 
     composed_schema
         Accessor which returns the composed schema, which has no connection
         info, which was used in constructing the "schema" above. Useful for
-        creating new connections based on the same schema/model.
+        creating new connections based on the same schema/model. There are
+        direct shortcuts from the model object for composed_schema->clone
+        and composed_schema->connect
 
     clone
         Shortcut for ->composed_schema->clone
@@ -84,6 +98,15 @@ METHODS
     connect
         Shortcut for ->composed_schema->connect
 
+    source
+        Shortcut for ->schema->source
+
+    class
+        Shortcut for ->schema->class
+
+    resultset
+        Shortcut for ->schema->resultset
+
 SEE ALSO
     Catalyst, DBIx::Class, DBIx::Class::Schema, DBIx::Class::Schema::Loader
 
index 4a88467..bf8fecc 100644 (file)
@@ -1,6 +1,8 @@
 package Catalyst::Helper::Model::DBIC::Schema;
 
 use strict;
+use warnings;
+use Carp;
 
 =head1 NAME
 
@@ -8,7 +10,15 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
 
 =head1 SYNOPSIS
 
-    script/create.pl model Foo DBIC::Schema Foo::SchemaClass dsn user password
+    script/create.pl model Foo DBIC::Schema Foo::SchemaClass [ dsn user password ]
+
+    Where:
+      Foo is the short name for the Model class being generated
+      Foo::SchemaClass is the fully qualified classname of your Schema,
+        which isa DBIx::Class::Schema defined elsewhere.
+      dsn, user, and password are optional if connection info is already
+        defined in your Schema class (as it would be in the case of
+        DBIx::Class::Schema::Loader).
 
 =head1 DESCRIPTION
 
@@ -21,11 +31,17 @@ Helper for the DBIC Plain Models.
 =cut
 
 sub mk_compclass {
-    my ( $self, $helper, $schemaclass, $dsn, $user, $pass ) = @_;
-    $helper->{schemaclass} = $schemaclass || '';
-    $helper->{dsn}         = $dsn  || '';
-    $helper->{user}        = $user || '';
-    $helper->{pass}        = $pass || '';
+    my ( $self, $helper, $schema_class, $dsn, $user, $pass ) = @_;
+
+    $helper->{schema_class} = $schema_class || '';
+
+    if(defined($dsn)) {
+        $helper->{setup_connect_info} = 1;
+        $helper->{dsn}         = $dsn  || '';
+        $helper->{user}        = $user || '';
+        $helper->{pass}        = $pass || '';
+    }
+
     my $file = $helper->{file};
     $helper->render_file( 'compclass', $file );
 }
@@ -57,7 +73,8 @@ use strict;
 use base 'Catalyst::Model::DBIC::Schema';
 
 __PACKAGE__->config(
-    schema_class => '[% schemaclass %]',
+    schema_class => '[% schema_class %]',
+    [% IF setup_connect_info %]
     connect_info => [ '[% dsn %]',
                       '[% user %]',
                       '[% pass %]',
@@ -69,6 +86,7 @@ __PACKAGE__->config(
                           AutoCommit         => 1,
                       }
                     ],
+    [% END %]
 );
 
 =head1 NAME
index 83833c9..e7d7a83 100644 (file)
@@ -74,7 +74,7 @@ This is a Catalyst Model for L<DBIx::Class::Schema>-based Models.
 
 This is the classname of your L<DBIx::Class::Schema> Schema.  It needs
 to be findable in C<@INC>, but it does not need to be underneath
-C<Catalyst::Model::>.
+C<Catalyst::Model::>.  This parameter is required.
 
 =item connect_info
 
@@ -83,6 +83,10 @@ C<storage_type>.  For C<::DBI>, which is the only supported C<storage_type>
 in L<DBIx::Class> at the time of this writing, the 4 parameters are your
 dsn, username, password, and connect options hashref.
 
+This is not required if C<schema_class> already has connection information
+defined in itself (which would be the case for a Schema defined by
+L<DBIx::Class::Schema::Loader>, for instance).
+
 =item storage_type
 
 Allows the use of a different C<storage_type> than what is set in your
@@ -99,6 +103,9 @@ people, until other storage backends become available for L<DBIx::Class>.
 =item new
 
 Instantiates the Model based on the above-documented ->config parameters.
+The only required parameter is C<schema_class>.  C<connect_info> is
+required in the case that C<schema_class> does not already have connection
+information defined for it.
 
 =item schema
 
@@ -145,16 +152,24 @@ sub new {
     my $model_name = $class;
     $model_name =~ s/^[\w:]+::(?:Model|M):://;
 
-    foreach (qw/ connect_info schema_class /) {
-        croak "->config->{$_} must be defined for this model"
-            unless $self->{$_};
-    }
+    croak "->config->{schema_class} must be defined for this model"
+        unless $self->{schema_class};
 
     my $schema_class = $self->{schema_class};
 
     $schema_class->require
         or croak "Cannot load schema class '$schema_class': $@";
 
+    if( !$self->{connect_info} ) {
+        if($schema_class->storage && $schema_class->storage->connect_info) {
+            $self->{connect_info} = $schema_class->storage->connect_info;
+        }
+        else {
+            croak "Either ->config->{connect_info} must be defined for $class"
+                  . " or $schema_class must have connection defined on it";
+        }
+    }
+
     $self->composed_schema($schema_class->compose_namespace($class));
     $self->schema($self->composed_schema->clone);
     $self->schema->storage_type($self->{storage_type}) if $self->{storage_type};
@@ -171,10 +186,8 @@ sub new {
     return $self;
 }
 
-# convenience method
 sub clone { shift->composed_schema->clone(@_); }
 
-# convenience method
 sub connect { shift->composed_schema->connect(@_); }
 
 =head1 SEE ALSO