X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FModel%2FDBIC%2FSchema%2FTypes.pm;h=88467d848ebfdde9ce35437b17aa3b709ca206b9;hb=7314403accaf92512542e7ec3181282b60af3316;hp=989d5023a90a3cf06acd9d1ccab55a33f3ea8720;hpb=c4fee9b88f9183e17c6499c077fcf8572a95c165;p=catagits%2FCatalyst-Model-DBIC-Schema.git diff --git a/lib/Catalyst/Model/DBIC/Schema/Types.pm b/lib/Catalyst/Model/DBIC/Schema/Types.pm index 989d502..88467d8 100644 --- a/lib/Catalyst/Model/DBIC/Schema/Types.pm +++ b/lib/Catalyst/Model/DBIC/Schema/Types.pm @@ -1,19 +1,23 @@ -package Catalyst::Model::DBIC::Schema::Types; +package # hide from PAUSE + Catalyst::Model::DBIC::Schema::Types; -use MooseX::Types - -declare => [qw/ConnectInfo ConnectInfos Replicants SchemaClass/]; +use MooseX::Types -declare => [qw/ + ConnectInfo ConnectInfos Replicants LoadedClass CreateOption +/]; +use Carp::Clan '^Catalyst::Model::DBIC::Schema'; use MooseX::Types::Moose qw/ArrayRef HashRef Str ClassName/; use Scalar::Util 'reftype'; use List::MoreUtils 'all'; -use Carp; use namespace::clean -except => 'meta'; -subtype SchemaClass, +class_type 'DBIx::Class::Schema'; + +subtype LoadedClass, as ClassName; -coerce SchemaClass, +coerce LoadedClass, from Str, via { Class::MOP::load_class($_); $_ }; @@ -24,9 +28,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. @@ -40,15 +44,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]; @@ -57,19 +74,28 @@ sub _coerce_connect_info_from_arrayref { for my $i (0..1) { my $extra = shift @$_; last unless $extra; - croak "invalid connect_info" unless reftype $extra eq 'HASH'; + die "invalid connect_info" unless reftype $extra eq 'HASH'; %connect_info = (%connect_info, %$extra); } - croak "invalid connect_info" if @$_; + die "invalid connect_info" if @$_; } elsif (@$_ == 1 && reftype $_->[0] eq 'HASH') { return $_->[0]; } else { - croak "invalid connect_info"; + 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;