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