1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants SchemaClass CursorClass
9 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
10 use MooseX::Types::Moose qw/ArrayRef HashRef Str ClassName/;
11 use Scalar::Util 'reftype';
12 use List::MoreUtils 'all';
14 use namespace::clean -except => 'meta';
16 class_type 'DBIx::Class::Schema';
23 via { Class::MOP::load_class($_); $_ };
30 via { Class::MOP::load_class($_); $_ };
34 where { exists $_->{dsn} },
35 message { 'Does not look like a valid connect_info' };
39 via(\&_coerce_connect_info_from_str),
41 via(\&_coerce_connect_info_from_arrayref);
43 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
45 # Also no coercion from base type (yet,) but in Moose git already.
47 # via { $_->{connect_info} },
50 as ArrayRef[ConnectInfo],
51 message { "Not a valid array of connect_info's" };
55 via { [ _coerce_connect_info_from_str() ] },
58 !ref $_ ? _coerce_connect_info_from_str()
59 : reftype $_ eq 'HASH' ? $_
60 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
61 : die 'invalid connect_info'
68 where { /^(?:static|dynamic)\z/ },
69 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
71 sub _coerce_connect_info_from_arrayref {
77 if (!ref $_->[0]) { # array style
78 $connect_info{dsn} = shift @$_;
79 $connect_info{user} = shift @$_ if !ref $_->[0];
80 $connect_info{password} = shift @$_ if !ref $_->[0];
83 my $extra = shift @$_;
85 die "invalid connect_info" unless reftype $extra eq 'HASH';
87 %connect_info = (%connect_info, %$extra);
90 die "invalid connect_info" if @$_;
91 } elsif (@$_ == 1 && reftype $_->[0] eq 'HASH') {
94 die "invalid connect_info";
97 for my $key (qw/user password/) {
98 $connect_info{$key} = ''
99 if not defined $connect_info{$key};
105 sub _coerce_connect_info_from_str {
106 +{ dsn => $_, user => '', password => '' }