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