1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants LoadedClass CreateOption
8 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
9 use MooseX::Types::Moose qw/ArrayRef HashRef Str ClassName/;
10 use Scalar::Util 'reftype';
11 use List::MoreUtils 'all';
13 use namespace::clean -except => 'meta';
15 class_type 'DBIx::Class::Schema';
22 via { Class::MOP::load_class($_); $_ };
26 where { exists $_->{dsn} },
27 message { 'Does not look like a valid connect_info' };
31 via(\&_coerce_connect_info_from_str),
33 via(\&_coerce_connect_info_from_arrayref);
35 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
37 # Also no coercion from base type (yet,) but in Moose git already.
39 # via { $_->{connect_info} },
42 as ArrayRef[ConnectInfo],
43 message { "Not a valid array of connect_info's" };
47 via { [ _coerce_connect_info_from_str() ] },
50 !ref $_ ? _coerce_connect_info_from_str()
51 : reftype $_ eq 'HASH' ? $_
52 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
53 : die 'invalid connect_info'
60 where { /^(?:static|dynamic)\z/ },
61 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
63 sub _coerce_connect_info_from_arrayref {
69 if (!ref $_->[0]) { # array style
70 $connect_info{dsn} = shift @$_;
71 $connect_info{user} = shift @$_ if !ref $_->[0];
72 $connect_info{password} = shift @$_ if !ref $_->[0];
75 my $extra = shift @$_;
77 die "invalid connect_info" unless reftype $extra eq 'HASH';
79 %connect_info = (%connect_info, %$extra);
82 die "invalid connect_info" if @$_;
83 } elsif (@$_ == 1 && reftype $_->[0] eq 'HASH') {
86 die "invalid connect_info";
89 for my $key (qw/user password/) {
90 $connect_info{$key} = ''
91 if not defined $connect_info{$key};
97 sub _coerce_connect_info_from_str {
98 +{ dsn => $_, user => '', password => '' }