release
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
index 8c8d0f6..b58a093 100644 (file)
@@ -1,17 +1,19 @@
 package Catalyst::Helper::Model::DBIC::Schema;
 
+use namespace::autoclean;
 use Moose;
 no warnings 'uninitialized';
 
-our $VERSION = '0.24';
+our $VERSION = '0.30';
 
 use Carp;
 use Tie::IxHash ();
 use Data::Dumper ();
 use List::Util 'first';
-use MooseX::Types::Moose qw/Str HashRef Bool/;
-
-use namespace::clean -except => 'meta';
+use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/;
+use Catalyst::Model::DBIC::Schema::Types 'CreateOption';
+use List::MoreUtils 'firstidx';
+use Scalar::Util 'looks_like_number';
 
 =head1 NAME
 
@@ -20,7 +22,7 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
 =head1 SYNOPSIS
 
   script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
-    [ create=dynamic | create=static ] [ roles=role1,role2... ] \
+    [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \
     [ Schema::Loader opts ] [ dsn user pass ] \
     [ other connect_info args ]
 
@@ -52,7 +54,7 @@ L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
 adapt itself to changes in your database structure.  You can edit
 the generated classes by hand to refine them.
 
-C<roles> is the list of roles to apply to the model, see
+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.
@@ -84,6 +86,10 @@ user and pass can be omitted for sqlite, since they are always empty
     AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
     on_connect_do='["select 1", "select 2"]' quote_char='"'
 
+If using a 2 character quote_char:
+
+  script/myapp_create.pl ... quote_char='[]'
+
 B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>
 
 In C<cmd.exe> the above example would be:
@@ -97,7 +103,7 @@ Same, but with extra Schema::Loader args (separate multiple values by commas):
 
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
     MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
-    exclude='^wibble|wobble$' moniker_map='{ foo => "FOO" }' \
+    exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \
     dbi:Pg:dbname=foodb myuname mypass
 
 See L<DBIx::Class::Schema::Loader::Base> for a list of options
@@ -121,13 +127,14 @@ in your app config, or [not recommended] in the schema itself).
 =cut
 
 has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
-
+has create => (is => 'rw', isa => CreateOption);
+has args => (is => 'ro', isa => ArrayRef);
+has traits => (is => 'rw', isa => ArrayRef);
 has schema_class => (is => 'ro', isa => Str, required => 1);
-
 has loader_args => (is => 'rw', isa => HashRef);
 has connect_info => (is => 'rw', isa => HashRef);
-
 has old_schema => (is => 'rw', isa => Bool, lazy_build => 1);
+has components => (is => 'rw', isa => ArrayRef);
 
 =head1 METHODS
 
@@ -141,46 +148,79 @@ files.
 sub mk_compclass {
     my ($package, $helper, $schema_class, @args) = @_;
 
-    my $self = $package->new(helper => $helper, schema_class => $schema_class);
+    my $self = $package->new(
+        helper => $helper,
+        schema_class => $schema_class,
+        args => \@args
+    );
+
+    $self->run;
+}
 
-    $helper->{schema_class} = $schema_class;
+sub BUILD {
+    my $self   = shift;
+    my $helper = $self->helper;
+    my @args   = @{ $self->args || [] };
+
+    $helper->{schema_class} = $self->schema_class;
 
     @args = $self->_cleanup_args(\@args);
 
-    my $create = '';
-    if ($args[0] && $args[0] =~ /^create=(dynamic|static)\z/) {
-        $create = $1;
-        shift @args;
+    my ($traits_idx, $traits);
+    if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) {
+        my @traits = split /,/ => $traits;
 
-        if ($args[0] && $args[0] =~ /^roles=(.*)\z/) {
-            $helper->{roles} = '['
-                .(join ',' => map { qq{'$_'} } (split /,/ => $1))
-                .']';
-            shift @args;
-        }
+        $self->traits(\@traits);
+
+        $helper->{traits} = '['
+            .(join ',' => map { qq{'$_'} } @traits)
+            .']';
+
+        splice @args, $traits_idx, 1, ();
+    }
+
+    if ($args[0] && $args[0] =~ /^create=(\S*)\z/) {
+        $self->create($1);
+        shift @args;
 
         if (@args) {
             $self->_parse_loader_args(\@args);
 
-            if (first { /^dbi:/i } @args) {
-                $helper->{setup_connect_info} = 1;
+            $helper->{loader_args} = $self->_build_helper_loader_args;
+        }
+    }
 
-                $helper->{connect_info} =
-                    $self->_build_helper_connect_info(\@args);
+    my $dbi_dsn_part;
+    if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) {
+        die
+qq{DSN must start with 'dbi:' not '$dbi_dsn_part' (case matters!)}
+            if $dbi_dsn_part ne 'dbi';
 
-                $self->_parse_connect_info(\@args) if $create eq 'static';
-            }
-        }
+        $helper->{setup_connect_info} = 1;
+
+        $helper->{connect_info} =
+            $self->_build_helper_connect_info(\@args);
+
+        $self->_parse_connect_info(\@args);
     }
 
     $helper->{generator} = ref $self;
     $helper->{generator_version} = $VERSION;
+}
+
+=head2 run
+
+Can be called on an instance to generate the files.
+
+=cut
+
+sub run {
+    my $self = shift;
 
-    if ($create eq 'dynamic') {
+    if ($self->create eq 'dynamic') {
         $self->_print_dynamic_deprecation_warning;
-        $self->helper->{loader_args} = $self->_build_helper_loader_args;
         $self->_gen_dynamic_schema;
-    } elsif ($create eq 'static') {
+    } elsif ($self->create eq 'static') {
         $self->_gen_static_schema;
     }
 
@@ -195,7 +235,7 @@ sub _parse_loader_args {
     while (my ($key, $val) = each %loader_args) {
         next if $key =~ /^(?:components|constraint|exclude)\z/;
 
-        $loader_args{$key} = eval $val;
+        $loader_args{$key} = $self->_eval($val);
         die "syntax error for loader args key '$key' with value '$val': $@"
             if $@;
     }
@@ -203,6 +243,8 @@ sub _parse_loader_args {
     my @components =
     $self->_build_loader_components(delete $loader_args{components});
 
+    $self->components(\@components);
+
     for my $re_opt (qw/constraint exclude/) {
         $loader_args{$re_opt} = qr/$loader_args{$re_opt}/
         if exists $loader_args{$re_opt};
@@ -231,10 +273,12 @@ sub _read_loader_args {
 
     my %loader_args;
 
-    while (@$args && $args->[0] !~ /^dbi:/) {
+    while (@$args && $args->[0] !~ /^dbi:/i) {
         my ($key, $val) = split /=/, shift(@$args), 2;
 
-        if ((my @vals = split /,/ => $val) > 1) {
+        if ($self->_is_struct($val)) {
+            $loader_args{$key} = $val;
+        } elsif ((my @vals = split /,/ => $val) > 1) {
             $loader_args{$key} = \@vals;
         } else {
             $loader_args{$key} = $val;
@@ -292,7 +336,7 @@ sub _build_helper_connect_info {
 
     for (@connect_info) {
         if (/^\s*{.*}\s*\z/) {
-            my $hash = eval $_;
+            my $hash = $self->_eval($_);
             die "Syntax errorr in connect_info hash: $_: $@" if $@;
             my %hash = %$hash;
 
@@ -302,7 +346,7 @@ sub _build_helper_connect_info {
                 if (ref $val) {
                     $val = $self->_data_struct_to_string($val);
                 } else {
-                    $val = qq{'$val'};
+                    $val = $self->_quote($val);
                 }
 
                 $helper_connect_info{$key} = $val;
@@ -313,7 +357,13 @@ sub _build_helper_connect_info {
 
         my ($key, $val) = split /=/, $_, 2;
 
-        $helper_connect_info{$key} = $self->_quote_unless_struct($val);
+        if ($key eq 'quote_char') {
+            $helper_connect_info{$key} = length($val) == 1 ?
+                $self->_quote($val) :
+                $self->_data_struct_to_string([split //, $val]);
+        } else {
+            $helper_connect_info{$key} = $self->_quote_unless_struct($val);
+        }
     }
 
     \%helper_connect_info
@@ -354,7 +404,7 @@ sub _get_dsn_user_pass {
 
     if ($dsn =~ /sqlite/i) {
         ($user, $password) = ('', '');
-        shift @$connect_info while $connect_info->[0] eq '';
+        shift @$connect_info while @$connect_info and $connect_info->[0] eq '';
     } else {
         ($user, $password) = splice @$connect_info, 0, 2;
     }
@@ -374,7 +424,7 @@ sub _parse_connect_info {
 
     for (@connect_info) {
         if (/^\s*{.*}\s*\z/) {
-            my $hash = eval $_;
+            my $hash = $self->_eval($_);
             die "Syntax errorr in connect_info hash: $_: $@" if $@;
 
             %connect_info = (%connect_info, %$hash);
@@ -384,10 +434,12 @@ sub _parse_connect_info {
 
         my ($key, $val) = split /=/, $_, 2;
 
-        if ($key =~ /^(?:quote_char|name_sep)\z/) {
+        if ($key eq 'quote_char') {
+            $connect_info{$key} = length($val) == 1 ? $val : [split //, $val];
+        } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) {
             $connect_info{$key} = $val;
         } else {
-            $connect_info{$key} = eval $val;
+            $connect_info{$key} = $self->_eval($val);
         }
 
         die "syntax error for connect_info key '$key' with value '$val': $@"
@@ -399,12 +451,34 @@ sub _parse_connect_info {
     \%connect_info
 }
 
+sub _is_struct {
+    my ($self, $val) = @_;
+
+    return $val =~ /^\s*[[{]/;
+}
+
+sub _quote {
+    my ($self, $val) = @_;
+
+    return 'q{'.$val.'}';
+}
+
 sub _quote_unless_struct {
     my ($self, $val) = @_;
 
-    $val = qq{'$val'} if $val !~ /^\s*[[{]/;
+    $val = $self->_quote($val) if not $self->_is_struct($val);
+
+    return $val;
+}
+
+sub _eval {
+    my ($self, $code) = @_;
+
+    return $code if looks_like_number $code;
+
+    return $code if $code =~ m{^[\w;:/]*\z};
 
-    $val;
+    return eval "{no strict; $code}";
 }
 
 sub _gen_dynamic_schema {
@@ -497,7 +571,7 @@ Brandon L Black, C<blblack@gmail.com>
 
 Contributors:
 
-Rafael Kitover, C<<rkitover at cpan.org>>
+Rafael Kitover, C<rkitover at cpan.org>
 
 =head1 LICENSE
 
@@ -562,7 +636,7 @@ use base 'Catalyst::Model::DBIC::Schema';
 
 __PACKAGE__->config(
     schema_class => '[% schema_class %]',
-    [% IF roles %]roles => [% roles %],[% END %]
+    [% IF traits %]traits => [% traits %],[% END %]
     [% IF setup_connect_info %]connect_info => {
         [%- FOREACH key = connect_info.keys %]
         [% key %] => [% connect_info.${key} %],