switch to CX::Component::Traits
[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
fb691af9 16class_type 'DBIx::Class::Schema';
17
0fbbc8d5 18subtype SchemaClass,
19 as ClassName;
20
21coerce SchemaClass,
22 from Str,
23 via { Class::MOP::load_class($_); $_ };
24
3f139b02 25subtype CursorClass,
26 as ClassName;
27
28coerce CursorClass,
29 from Str,
30 via { Class::MOP::load_class($_); $_ };
31
0fbbc8d5 32subtype ConnectInfo,
33 as HashRef,
34 where { exists $_->{dsn} },
35 message { 'Does not look like a valid connect_info' };
36
37coerce ConnectInfo,
38 from Str,
7b1fe8c2 39 via(\&_coerce_connect_info_from_str),
0fbbc8d5 40 from ArrayRef,
7b1fe8c2 41 via(\&_coerce_connect_info_from_arrayref);
0fbbc8d5 42
43# { connect_info => [ ... ] } coercion would be nice, but no chained coercions
c4fee9b8 44# yet.
45# Also no coercion from base type (yet,) but in Moose git already.
0fbbc8d5 46# from HashRef,
47# via { $_->{connect_info} },
48
c4fee9b8 49subtype ConnectInfos,
50 as ArrayRef[ConnectInfo],
51 message { "Not a valid array of connect_info's" };
52
53coerce ConnectInfos,
54 from Str,
7b1fe8c2 55 via { [ _coerce_connect_info_from_str() ] },
56 from ArrayRef,
57 via { [ map {
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'
62 } @$_ ] };
c4fee9b8 63
4cbe63e7 64# Helper stuff
65
66subtype CreateOption,
67 as Str,
68 where { /^(?:static|dynamic)\z/ },
69 message { "Invalid create option, must be one of 'static' or 'dynamic'" };
70
c4fee9b8 71sub _coerce_connect_info_from_arrayref {
72 my %connect_info;
73
7b1fe8c2 74 # make a copy
75 $_ = [ @$_ ];
76
c4fee9b8 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];
81
82 for my $i (0..1) {
83 my $extra = shift @$_;
84 last unless $extra;
39f5f008 85 die "invalid connect_info" unless reftype $extra eq 'HASH';
c4fee9b8 86
87 %connect_info = (%connect_info, %$extra);
88 }
89
39f5f008 90 die "invalid connect_info" if @$_;
c4fee9b8 91 } elsif (@$_ == 1 && reftype $_->[0] eq 'HASH') {
92 return $_->[0];
93 } else {
39f5f008 94 die "invalid connect_info";
c4fee9b8 95 }
96
7b1fe8c2 97 for my $key (qw/user password/) {
98 $connect_info{$key} = ''
99 if not defined $connect_info{$key};
100 }
101
c4fee9b8 102 \%connect_info;
103}
104
7b1fe8c2 105sub _coerce_connect_info_from_str {
106 +{ dsn => $_, user => '', password => '' }
107}
108
0fbbc8d5 1091;