1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants SchemaClass LoadedClass CreateOption
9 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
10 use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef Str ClassName/;
11 use Scalar::Util 'reftype';
12 use List::MoreUtils 'all';
14 use namespace::clean -except => 'meta';
20 from Str, # N.B. deliberate paranoia against $_ clobbering below
21 via { my $classname = $_; Class::MOP::load_class($classname); $classname };
25 where { $_->isa('DBIx::Class::Schema') };
27 SchemaClass->coercion(LoadedClass->coercion);
29 class_type Schema, { class => 'DBIx::Class::Schema' };
33 where { exists $_->{dsn} || exists $_->{dbh_maker} },
34 message { 'Does not look like a valid connect_info' };
38 via(\&_coerce_connect_info_from_str),
40 via(\&_coerce_connect_info_from_arrayref),
42 via { +{ dbh_maker => $_ } },
45 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
47 # Also no coercion from base type (yet,) but in Moose git already.
49 # via { $_->{connect_info} },
52 as ArrayRef[ConnectInfo],
53 message { "Not a valid array of connect_info's" };
57 via { [ _coerce_connect_info_from_str() ] },
59 via { [ +{ dbh_maker => $_ } ] },
64 !ref $_ ? _coerce_connect_info_from_str()
65 : reftype $_ eq 'HASH' ? $_
66 : reftype $_ eq 'CODE' ? +{ dbh_maker => $_ }
67 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
68 : croak 'invalid connect_info'
75 where { /^(?:static|dynamic)\z/ },
76 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
78 sub _coerce_connect_info_from_arrayref {
84 my $slurp_hashes = sub {
86 my $extra = shift @$_;
88 croak "invalid connect_info"
89 unless ref $extra && reftype $extra eq 'HASH';
91 %connect_info = (%connect_info, %$extra);
95 if (!ref $_->[0]) { # array style
96 $connect_info{dsn} = shift @$_;
97 $connect_info{user} = shift @$_ if !ref $_->[0];
98 $connect_info{password} = shift @$_ if !ref $_->[0];
102 croak "invalid connect_info" if @$_;
103 } elsif (ref $_->[0] && reftype $_->[0] eq 'CODE') {
104 $connect_info{dbh_maker} = shift @$_;
108 croak "invalid connect_info" if @$_;
109 } elsif (@$_ == 1 && ref $_->[0] && reftype $_->[0] eq 'HASH') {
112 croak "invalid connect_info";
115 unless ($connect_info{dbh_maker}) {
116 for my $key (qw/user password/) {
117 $connect_info{$key} = ''
118 if not defined $connect_info{$key};
125 sub _coerce_connect_info_from_str {
126 +{ dsn => $_, user => '', password => '' }