1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants SchemaClass LoadedClass CreateOption
8 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
9 use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef Str ClassName/;
10 use Scalar::Util 'reftype';
11 use List::MoreUtils 'all';
13 use namespace::clean -except => 'meta';
20 via { Class::MOP::load_class($_); $_ };
24 where { $_->isa('DBIx::Class::Schema') };
26 SchemaClass->coercion(LoadedClass->coercion);
30 where { exists $_->{dsn} || exists $_->{dbh_maker} },
31 message { 'Does not look like a valid connect_info' };
35 via(\&_coerce_connect_info_from_str),
37 via(\&_coerce_connect_info_from_arrayref),
39 via { +{ dbh_maker => $_ } },
42 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
44 # Also no coercion from base type (yet,) but in Moose git already.
46 # via { $_->{connect_info} },
49 as ArrayRef[ConnectInfo],
50 message { "Not a valid array of connect_info's" };
54 via { [ _coerce_connect_info_from_str() ] },
56 via { [ +{ dbh_maker => $_ } ] },
59 !ref $_ ? _coerce_connect_info_from_str()
60 : reftype $_ eq 'HASH' ? $_
61 : reftype $_ eq 'CODE' ? +{ dbh_maker => $_ }
62 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
63 : croak 'invalid connect_info'
70 where { /^(?:static|dynamic)\z/ },
71 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
73 sub _coerce_connect_info_from_arrayref {
79 my $slurp_hashes = sub {
81 my $extra = shift @$_;
83 croak "invalid connect_info"
84 unless ref $extra && reftype $extra eq 'HASH';
86 %connect_info = (%connect_info, %$extra);
90 if (!ref $_->[0]) { # array style
91 $connect_info{dsn} = shift @$_;
92 $connect_info{user} = shift @$_ if !ref $_->[0];
93 $connect_info{password} = shift @$_ if !ref $_->[0];
97 croak "invalid connect_info" if @$_;
98 } elsif (ref $_->[0] && reftype $_->[0] eq 'CODE') {
99 $connect_info{dbh_maker} = shift @$_;
103 croak "invalid connect_info" if @$_;
104 } elsif (@$_ == 1 && ref $_->[0] && reftype $_->[0] eq 'HASH') {
107 croak "invalid connect_info";
110 unless ($connect_info{dbh_maker}) {
111 for my $key (qw/user password/) {
112 $connect_info{$key} = ''
113 if not defined $connect_info{$key};
120 sub _coerce_connect_info_from_str {
121 +{ dsn => $_, user => '', password => '' }