ViewLoader/Auto.pm

filter: Statementstmt 100% 35 / 35 Branchbran 100% 6 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 53 / 53 CC 5 SCAR 11.0
CC
5
Cov
100%
load_views
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
↑ Summary ViewLoader.pm »
1 package Class::DBI::ViewLoader::Auto;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.01';
7  
8 =head1 NAME
9  
10 Class::DBI::ViewLoader::Auto - Load views for existing Class::DBI classes
11  
12 =head1 SYNOPSIS
13  
14 package MyMovieClass;
15  
16 use strict;
17 use warnings;
18  
19 use Class::DBI;
20 use Class::DBI::ViewLoader::Auto;
21  
22 our @ISA = qw( Class::DBI );
23  
24 __PACKAGE__->connection('dbi:Pg:dbname=mymoviedb', 'me', 'mypasswd');
25  
26 # load views from database mymoviedb to MyMovieClass::*
27 @loaded = __PACKAGE__->load_views();
28  
29 # load only views starting with film_
30 @loaded = __PACKAGE__->load_views(qr/^film_/);
31  
32 # or pass more options:
33 @loaded = __PACKAGE__->load_views(
34 namespace => 'MyMovieClass::View',
35 exclude => qr(^test_),
36 );
37  
38 =head2 DESCRIPTION
39  
40 This module provides a simpler interface to Class::DBI::ViewLoader.
41  
42 =cut
43  
44 7 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 7 ▶use Class::DBI::ViewLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 7 our @ISA = qw( Exporter );
50 7 our @EXPORT = qw( load_views );
51  
52 =head1 EXPORTS
53  
54 This module exports the load_views method into the calling package
55  
56 =cut
57  
58 =head1 METHODS
59  
60 =head2 load_views
61  
62 $loader = $cdbi_class->load_views( %opts or $include )
63  
64 Loads views from the database connection in $cdbi_class.
65  
66 The default namespace is the same as the calling class.
67  
68 %opts is passed to the Class::DBI::Loader constructor. If a scalar argument is
69 given instead of a hash or hashref, it is interpreted as being the include
70 pattern.
71  
72 The options dsn, username, password and options are silently ignored.
73  
74 $cdbi_class should always be the leftmost base class of the generated classes.
75 base_classes and left_base_classes options are supported, but it might make more
76 sense to add those bases to the calling class manually.
77  
78 Returns the same as Class::DBI::ViewLoader->load_views, i.e. a list of loaded
79 classes.
80  
81 =cut
82  
83 # Class::DBI::ViewLoader options to ignore
84 7 my @unsupported = qw( dsn username password options );
85  
86 6 ▶sub load_views {
Subroutine load_views: called CC=5 SCAR=16.1
Pod documented
87 6 my $class = shift;
88 6 my %opts;
89  
90 6 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 3 3 6
91 3 my $proto = shift;
92 3 ▶ if (ref $proto eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto eq 'HASH') { } 2 1 3
93 2 %opts = %$proto;
94 }
95 else {
96 1 $opts{'include'} = $proto;
97 }
98 }
99 else {
100 3 %opts = @_;
101 }
102  
103 6 delete @opts{@unsupported};
104 6 ▶ $opts{'namespace'} ||= $class;
Condition100%3/3
$opts{"namespace"} ||= $class
l!l&&r!l&&!r
231
MC/DC100%2/2
$opts{"namespace"} ||= $class
$opts{"namespace"} $class
Truth table100%3/3
A ||= B
A= $opts{"namespace"}B= $class
ABresult
1X1
011
000
105  
106 6 Class::DBI::ViewLoader->_compat(\%opts);
107  
108 6 ▶ my $sub = $class->can('db_Main')
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $class->can("db_Main") or croak("$class has no connection") 4 1 5
109 or croak "$class has no connection";
110  
111 4 my $dbh = &$sub($class);
112 4 my $driver = $dbh->{'Driver'}->{'Name'};
113  
114 4 return Class::DBI::ViewLoader->new(%opts)
115 ->_load_driver($driver)
116 ->_set_dbi_handle($dbh)
117 ->_set_keepalive(1)
118 ->add_left_base_classes($class)
119 ->load_views;
120 }
121  
122 7 1;
123  
124 __END__
↑ Summary ViewLoader.pm »