File Coverage

blib/lib/Test/DBIx/Class/Types.pm
Criterion Covered Total %
statement 36 39 92.3
branch 8 16 50.0
condition 0 3 0.0
subroutine 8 9 88.8
pod n/a
total 52 67 77.6


line stmt bran cond sub pod time code
1             package Test::DBIx::Class::Types;
2              
3 17     17   66 use strict;
  17         21  
  17         1387  
4 17     17   56 use warnings;
  17         1033  
  17         327  
5              
6 17     17   55 use Class::MOP;
  17         21  
  17         298  
7 17     17   138 use Scalar::Util qw(reftype);
  17         18  
  17         840  
8 17     17   6950 use MooseX::Types::Moose qw(Str Int ClassName ArrayRef HashRef);
  17         605448  
  17         128  
9 17         98 use MooseX::Types -declare => [qw/
10             TestBuilder SchemaManagerClass ConnectInfo FixtureClass
11             ReplicantsConnectInfo
12 17     17   63218 /];
  17         23  
13 17     17   63505 use Module::Runtime qw(use_module);
  17         27  
  17         140  
14              
15             subtype TestBuilder,
16             as class_type('Test::Builder');
17              
18             subtype SchemaManagerClass,
19             as ClassName;
20              
21             coerce SchemaManagerClass,
22             from Str,
23             via {
24             my $type = $_;
25             return use_module($type);
26             };
27              
28             subtype FixtureClass,
29             as ClassName;
30              
31             coerce FixtureClass,
32             from Str,
33             via {
34             my $type = $_;
35             $type = "Test::DBIx::Class::FixtureCommand".$type if $type =~m/^::/;
36             return use_module($type);
37             };
38              
39             ## ConnectInfo cargo culted from "Catalyst::Model::DBIC::Schema::Types"
40             subtype ConnectInfo,
41             as HashRef,
42             where { exists $_->{dsn} },
43             message { 'Does not look like a valid connect_info' };
44              
45             coerce ConnectInfo,
46             from Str,
47             via(\&_coerce_connect_info_from_str),
48             from ArrayRef,
49             via(\&_coerce_connect_info_from_arrayref);
50              
51              
52             sub _coerce_connect_info_from_arrayref {
53 28     28   19610 my %connect_info;
54              
55             # make a copy
56 28         84 $_ = [ @$_ ];
57              
58 28 50 0     98 if (!ref $_->[0]) { # array style
    0          
59 28         89 $connect_info{dsn} = shift @$_;
60 28 50       126 $connect_info{user} = shift @$_ if !ref $_->[0];
61 28 50       115 $connect_info{password} = shift @$_ if !ref $_->[0];
62              
63 28         98 for my $i (0..1) {
64 32         54 my $extra = shift @$_;
65 32 100       138 last unless $extra;
66 4 50       20 die "invalid connect_info" unless reftype $extra eq 'HASH';
67              
68 4         27 %connect_info = (%connect_info, %$extra);
69             }
70              
71 28 50       83 die "invalid connect_info" if @$_;
72             } elsif (@$_ == 1 && reftype $_->[0] eq 'HASH') {
73 0         0 return $_->[0];
74             } else {
75 0         0 die "invalid connect_info";
76             }
77              
78 28         77 for my $key (qw/user password/) {
79             $connect_info{$key} = ''
80 56 50       167 if not defined $connect_info{$key};
81             }
82              
83 28         413 \%connect_info;
84             }
85              
86             sub _coerce_connect_info_from_str {
87 0     0     +{ dsn => $_, user => '', password => '' }
88             }
89              
90              
91             subtype ReplicantsConnectInfo,
92             as ArrayRef[ConnectInfo];
93              
94             coerce ReplicantsConnectInfo,
95             from Int,
96             via { [map { +{dsn=>'', user=>'', password=>''} } (1..$_)] },
97             from ArrayRef[Str],
98             via {
99             [map { &_coerce_connect_info_from_str($_) } @$_];
100             },
101             from ArrayRef[ArrayRef],
102             via {
103             [map { &_coerce_connect_info_from_arrayref($_) } @$_];
104             };
105              
106             1;
107              
108             =head1 NAME
109              
110             Test::DBIx::Class::Types - Type Constraint Library
111              
112             =head1 DESCRIPTION
113              
114             L<MooseX::Types> based type constraint library
115              
116             =head1 AUTHOR
117              
118             John Napiorkowski C<< <jjnapiork@cpan.org> >>
119              
120             Code gratuitously cargo culted from L<Catalyst::Model::DBIC::Schema::Types>
121              
122             =head1 COPYRIGHT & LICENSE
123              
124             Copyright 2009, John Napiorkowski C<< <jjnapiork@cpan.org> >>
125              
126             This program is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself.
128              
129             =cut
130