Make compose_namespace and install_model_shortcut configurable.
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
index 1c98a0f..f50b2e5 100644 (file)
@@ -4,7 +4,7 @@ use namespace::autoclean;
 use Moose;
 no warnings 'uninitialized';
 
-our $VERSION = '0.48';
+our $VERSION = '0.65';
 $VERSION = eval $VERSION;
 
 use Carp;
@@ -18,6 +18,7 @@ use Scalar::Util 'looks_like_number';
 use File::Find 'finddepth';
 use Try::Tiny;
 use Cwd 'getcwd';
+use Module::Runtime 'use_module';
 
 =head1 NAME
 
@@ -61,14 +62,15 @@ the generated classes by hand to refine them.
 C<traits> is the list of traits to apply to the model, see
 L<Catalyst::Model::DBIC::Schema> for details.
 
-C<Schema::Loader opts> are described in L</TYPICAL EXAMPLES> below.
+C<Schema::Loader opts> are documented in L<DBIx::Class::Schema::Loader::Base>
+and some examples are given in L</TYPICAL EXAMPLES> below.
 
-C<connect_info> arguments are the same as what
-DBIx::Class::Schema::connect expects, and are storage_type-specific.
-For DBI-based storage, these arguments are the dsn, username,
-password, and connect options, respectively.  These are optional for
-existing Schemas, but required if you use either of the C<create=>
-options.
+C<connect_info> arguments are the same as what L<DBIx::Class::Schema/connect>
+expects, and are storage_type-specific. They are documented in
+L<DBIx::Class::Storage::DBI/connect_info>. For DBI-based storage, these
+arguments are the dsn, username, password, and connect options, respectively.
+These are optional for existing Schemas, but required if you use either of the
+C<create=> options.
 
 username and password can be omitted for C<SQLite> dsns.
 
@@ -299,6 +301,47 @@ sub _read_loader_args {
         }
     }
 
+    # Use args after connect_info as loader args as well, because people always
+    # get the order confused.
+    my $i = 1;
+    if ($args->[0] =~ /sqlite/i) {
+        $i++ if $args->[$i] eq '';
+        $i++ if $args->[$i] eq '';
+    }
+    else {
+        $i += 2;
+    }
+
+    my $have_loader = try {
+        use_module('DBIx::Class::Schema::Loader::Base');
+        1;
+    };
+
+    if ($have_loader) {
+        while (defined $args->[$i]) {
+            $i++ while $self->_is_struct($args->[$i]);
+
+            last if not defined $args->[$i];
+
+            my ($key, $val) = split /=/, $args->[$i], 2;
+
+            if (not DBIx::Class::Schema::Loader::Base->can($key)) {
+                $i++;
+                next;
+            }
+
+            if ($self->_is_struct($val)) {
+                $loader_args{$key} = $val;
+            } elsif ((my @vals = split /,/ => $val) > 1) {
+                $loader_args{$key} = \@vals;
+            } else {
+                $loader_args{$key} = $val;
+            }
+
+            splice @$args, $i, 1;
+        }
+    }
+
     wantarray ? %loader_args : \%loader_args;
 }
 
@@ -435,7 +478,7 @@ sub _build_result_namespace {
         File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm';
 
     if (not -f $schema_pm) {
-        eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
+        eval { use_module('DBIx::Class::Schema::Loader') };
 
         return 'Result' if $@;
 
@@ -460,6 +503,7 @@ sub _data_struct_to_string {
 
     local $Data::Dumper::Terse = 1;
     local $Data::Dumper::Quotekeys = 0;
+    local $Data::Dumper::Sortkeys = 1;
     local $Data::Dumper::Indent = 0;
     local $Data::Dumper::Useqq = 1;
 
@@ -580,7 +624,7 @@ sub _gen_static_schema {
     my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
 
     try {
-        Class::MOP::load_class('DBIx::Class::Schema::Loader')
+        use_module('DBIx::Class::Schema::Loader')
     }
     catch {
         die "Cannot load DBIx::Class::Schema::Loader: $_";
@@ -595,6 +639,19 @@ sub _gen_static_schema {
         $self->loader_args,
         [$self->connect_info]
     );
+
+    require lib;
+    lib->import($schema_dir);
+
+    use_module($self->schema_class);
+
+    my @sources = $self->schema_class->sources;
+
+    if (not @sources) {
+        warn <<'EOF';
+WARNING: No tables found, did you forget to specify db_schema?
+EOF
+    }
 }
 
 sub _gen_model {