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