1 package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
4 use MooseX::Types -declare => [qw/
5 ConnectInfo ConnectInfos Replicants SchemaClass CreateOption
9 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
10 use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef Str ClassName/;
11 use MooseX::Types::LoadableClass qw/LoadableClass/;
12 use Scalar::Util 'reftype';
13 use List::MoreUtils 'all';
16 use namespace::clean -except => 'meta';
18 # So I restored the custom Type LoadedClass because 'LoadableClass' doesn't really
19 # exactly do the same thing, which busted the Replication trait. Please don't
20 # "clean this up" -JNAP
26 from Str, # N.B. deliberate paranoia against $_ clobbering below
27 via { my $classname = $_; Module::Runtime::use_module($classname); $classname };
31 where { $_->isa('DBIx::Class::Schema') };
33 class_type Schema, { class => 'DBIx::Class::Schema' };
37 where { exists $_->{dsn} || exists $_->{dbh_maker} },
38 message { 'Does not look like a valid connect_info' };
42 via(\&_coerce_connect_info_from_str),
44 via(\&_coerce_connect_info_from_arrayref),
46 via { +{ dbh_maker => $_ } },
49 # { connect_info => [ ... ] } coercion would be nice, but no chained coercions
51 # Also no coercion from base type (yet,) but in Moose git already.
53 # via { $_->{connect_info} },
56 as ArrayRef[ConnectInfo],
57 message { "Not a valid array of connect_info's" };
61 via { [ _coerce_connect_info_from_str() ] },
63 via { [ +{ dbh_maker => $_ } ] },
68 !ref $_ ? _coerce_connect_info_from_str()
69 : reftype $_ eq 'HASH' ? $_
70 : reftype $_ eq 'CODE' ? +{ dbh_maker => $_ }
71 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
72 : croak 'invalid connect_info'
79 where { /^(?:static|dynamic)\z/ },
80 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
82 sub _coerce_connect_info_from_arrayref {
88 my $slurp_hashes = sub {
90 my $extra = shift @$_;
92 croak "invalid connect_info"
93 unless ref $extra && reftype $extra eq 'HASH';
95 %connect_info = (%connect_info, %$extra);
99 if (!ref $_->[0]) { # array style
100 $connect_info{dsn} = shift @$_;
101 $connect_info{user} = shift @$_ if !ref $_->[0];
102 $connect_info{password} = shift @$_ if !ref $_->[0];
106 croak "invalid connect_info" if @$_;
107 } elsif (ref $_->[0] && reftype $_->[0] eq 'CODE') {
108 $connect_info{dbh_maker} = shift @$_;
112 croak "invalid connect_info" if @$_;
113 } elsif (@$_ == 1 && ref $_->[0] && reftype $_->[0] eq 'HASH') {
116 croak "invalid connect_info";
119 unless ($connect_info{dbh_maker}) {
120 for my $key (qw/user password/) {
121 $connect_info{$key} = ''
122 if not defined $connect_info{$key};
129 sub _coerce_connect_info_from_str {
130 +{ dsn => $_, user => '', password => '' }