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