Stop using deprecated Class::MOP::load_module
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Types.pm
CommitLineData
113354d6 1package # hide from PAUSE
2 Catalyst::Model::DBIC::Schema::Types;
0fbbc8d5 3
4cbe63e7 4use MooseX::Types -declare => [qw/
45b10191 5 ConnectInfo ConnectInfos Replicants SchemaClass CreateOption
d816d7bf 6 Schema
4cbe63e7 7/];
0fbbc8d5 8
bd309c0c 9use Carp::Clan '^Catalyst::Model::DBIC::Schema';
2fa0a1f1 10use MooseX::Types::Moose qw/ArrayRef HashRef CodeRef Str ClassName/;
45b10191 11use MooseX::Types::LoadableClass qw/LoadableClass/;
0fbbc8d5 12use Scalar::Util 'reftype';
c4fee9b8 13use List::MoreUtils 'all';
0fbbc8d5 14
15use namespace::clean -except => 'meta';
16
2fa0a1f1 17subtype SchemaClass,
45b10191 18 as LoadableClass,
2fa0a1f1 19 where { $_->isa('DBIx::Class::Schema') };
20
d816d7bf 21class_type Schema, { class => 'DBIx::Class::Schema' };
22
0fbbc8d5 23subtype ConnectInfo,
24 as HashRef,
2fa0a1f1 25 where { exists $_->{dsn} || exists $_->{dbh_maker} },
0fbbc8d5 26 message { 'Does not look like a valid connect_info' };
27
28coerce ConnectInfo,
29 from Str,
7b1fe8c2 30 via(\&_coerce_connect_info_from_str),
0fbbc8d5 31 from ArrayRef,
2fa0a1f1 32 via(\&_coerce_connect_info_from_arrayref),
33 from CodeRef,
34 via { +{ dbh_maker => $_ } },
35;
0fbbc8d5 36
37# { connect_info => [ ... ] } coercion would be nice, but no chained coercions
c4fee9b8 38# yet.
39# Also no coercion from base type (yet,) but in Moose git already.
0fbbc8d5 40# from HashRef,
41# via { $_->{connect_info} },
42
c4fee9b8 43subtype ConnectInfos,
44 as ArrayRef[ConnectInfo],
45 message { "Not a valid array of connect_info's" };
46
47coerce ConnectInfos,
48 from Str,
2fa0a1f1 49 via { [ _coerce_connect_info_from_str() ] },
50 from CodeRef,
51 via { [ +{ dbh_maker => $_ } ] },
61fed351 52 from HashRef,
53 via { [ $_ ] },
7b1fe8c2 54 from ArrayRef,
55 via { [ map {
56 !ref $_ ? _coerce_connect_info_from_str()
57 : reftype $_ eq 'HASH' ? $_
2fa0a1f1 58 : reftype $_ eq 'CODE' ? +{ dbh_maker => $_ }
7b1fe8c2 59 : reftype $_ eq 'ARRAY' ? _coerce_connect_info_from_arrayref()
2fa0a1f1 60 : croak 'invalid connect_info'
7b1fe8c2 61 } @$_ ] };
c4fee9b8 62
4cbe63e7 63# Helper stuff
64
65subtype CreateOption,
66 as Str,
67 where { /^(?:static|dynamic)\z/ },
68 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
69
c4fee9b8 70sub _coerce_connect_info_from_arrayref {
71 my %connect_info;
72
7b1fe8c2 73 # make a copy
74 $_ = [ @$_ ];
75
2fa0a1f1 76 my $slurp_hashes = sub {
c4fee9b8 77 for my $i (0..1) {
78 my $extra = shift @$_;
79 last unless $extra;
2fa0a1f1 80 croak "invalid connect_info"
81 unless ref $extra && reftype $extra eq 'HASH';
c4fee9b8 82
83 %connect_info = (%connect_info, %$extra);
84 }
2fa0a1f1 85 };
c4fee9b8 86
2fa0a1f1 87 if (!ref $_->[0]) { # array style
88 $connect_info{dsn} = shift @$_;
89 $connect_info{user} = shift @$_ if !ref $_->[0];
90 $connect_info{password} = shift @$_ if !ref $_->[0];
91
92 $slurp_hashes->();
93
94 croak "invalid connect_info" if @$_;
95 } elsif (ref $_->[0] && reftype $_->[0] eq 'CODE') {
96 $connect_info{dbh_maker} = shift @$_;
97
98 $slurp_hashes->();
99
100 croak "invalid connect_info" if @$_;
101 } elsif (@$_ == 1 && ref $_->[0] && reftype $_->[0] eq 'HASH') {
c4fee9b8 102 return $_->[0];
103 } else {
2fa0a1f1 104 croak "invalid connect_info";
c4fee9b8 105 }
106
2fa0a1f1 107 unless ($connect_info{dbh_maker}) {
108 for my $key (qw/user password/) {
109 $connect_info{$key} = ''
110 if not defined $connect_info{$key};
111 }
7b1fe8c2 112 }
113
c4fee9b8 114 \%connect_info;
115}
116
7b1fe8c2 117sub _coerce_connect_info_from_str {
118 +{ dsn => $_, user => '', password => '' }
119}
120
0fbbc8d5 1211;