DBIC::Schema - add tests for helper
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Types.pm
index 9eb76a3..88a7a00 100644 (file)
@@ -1,7 +1,9 @@
 package Catalyst::Model::DBIC::Schema::Types;
 
-use MooseX::Types
- -declare => [qw/ConnectInfo ConnectInfos Replicants SchemaClass CursorClass/];
+use MooseX::Types -declare => [qw/
+    ConnectInfo ConnectInfos Replicants SchemaClass CursorClass
+    CreateOption
+/];
 
 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
 use MooseX::Types::Moose qw/ArrayRef HashRef Str ClassName/;
@@ -31,9 +33,9 @@ subtype ConnectInfo,
 
 coerce ConnectInfo,
     from Str,
-    via { +{ dsn => $_ } },
+    via(\&_coerce_connect_info_from_str),
     from ArrayRef,
-    via \&_coerce_connect_info_from_arrayref;
+    via(\&_coerce_connect_info_from_arrayref);
 
 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
 # yet.
@@ -47,15 +49,28 @@ subtype ConnectInfos,
 
 coerce ConnectInfos,
     from Str,
-    via  { [ { dsn => $_ } ] },
-    from ArrayRef[Str],
-    via { [ map +{ dsn => $_ }, @$_ ] },
-    from ArrayRef[ArrayRef],
-    via { [ map \&_coerce_connect_info_from_arrayref, @$_ ] };
+    via  { [ _coerce_connect_info_from_str() ] },
+    from ArrayRef,
+    via { [ map {
+        !ref $_ ? _coerce_connect_info_from_str()
+            : reftype $_ eq 'HASH' ? $_
+            : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
+            : die 'invalid connect_info'
+    } @$_ ] };
+
+# Helper stuff
+
+subtype CreateOption,
+    as Str,
+    where { /^(?:static|dynamic)\z/ },
+    message { "Invalid create option, must be one of 'static' or 'dynamic'" };
 
 sub _coerce_connect_info_from_arrayref {
     my %connect_info;
 
+    # make a copy
+    $_ = [ @$_ ];
+
     if (!ref $_->[0]) { # array style
         $connect_info{dsn}      = shift @$_;
         $connect_info{user}     = shift @$_ if !ref $_->[0];
@@ -76,7 +91,16 @@ sub _coerce_connect_info_from_arrayref {
         die "invalid connect_info";
     }
 
+    for my $key (qw/user password/) {
+        $connect_info{$key} = ''
+            if not defined $connect_info{$key};
+    }
+
     \%connect_info;
 }
 
+sub _coerce_connect_info_from_str {
+    +{ dsn => $_, user => '', password => '' }
+}
+
 1;