File Coverage

blib/lib/Catalyst/Model/DBIx/Connector.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Catalyst::Model::DBIx::Connector;
2              
3 1     1   22178 use namespace::autoclean;
  1         31743  
  1         8  
4 1     1   679 use DBIx::Connector;
  0            
  0            
5             use Moose;
6              
7             extends qw( Catalyst::Model );
8              
9             our $VERSION = '0.01';
10              
11              
12             has dsn => ( is => 'ro', isa => 'Str', required => 1 );
13             has username => ( is => 'ro', isa => 'Str' );
14             has password => ( is => 'ro', isa => 'Str' );
15             has options => ( is => 'ro', isa => 'HashRef' );
16             has connector => ( is => 'ro', isa => 'DBIx::Connector', lazy_build => 1, handles => [qw( dbh )] );
17              
18              
19             sub _build_connector {
20             my ( $self ) = @_;
21              
22             DBIx::Connector->new(
23             map { $self->$_ } qw( dsn username password options ) );
24             }
25              
26              
27             __PACKAGE__->meta->make_immutable;
28              
29             1
30             __END__
31              
32             =pod
33              
34             =head1 NAME
35              
36             Catalyst::Model::DBIx::Connector - Catalyst model base class for DBI connections using DBIx::Connector
37              
38             =head1 SYNOPSIS
39              
40             # in MyApp.pm
41              
42             __PACKAGE__->config(
43             'Model::MyModel' => {
44             dsn => 'dbi:Oracle:ORCL',
45             username => 'scott', # optional
46             password => 'tiger', # optional
47             options => { AutoCommit => 0 }, # optional
48             },
49             );
50              
51              
52             # in MyApp/Model/MyModel.pm
53              
54             package MyApp::Model::MyModel;
55              
56             use namespace::autoclean;
57             use Moose;
58             extends qw( Catalyst::Model::DBIx::Connector );
59              
60             sub model_method {
61             my ( $self ) = @_;
62              
63             my $dbh = $self->dbh;
64              
65             my $sth = $dbh->prepare( '...' );
66             $sth->execute;
67              
68             # ...
69              
70             $dbh->disconnect;
71             }
72              
73             =head1 DESCRIPTION
74              
75             Catalyst::Model::DBIx::Connector is a simple base class that
76             can be used to easily add DBI connections to your Catalyst apps. It
77             uses C<DBIx::Connector> to add disconnect detection and automatic
78             reconnection to the database once a connection has dropped.
79              
80             =head1 SEE ALSO
81              
82             =over 4
83              
84             =item L<Catalyst>
85              
86             =item L<Catalyst::Model>
87              
88             =item L<DBIx::Connector>
89              
90             =back
91              
92             =head1 AUTHOR
93              
94             jason hord E<lt>pravus@cpan.orgE<gt>
95              
96             =head1 COPYRIGHT
97              
98             Copyright (c) 2012-2014, jason hord
99              
100             Permission is hereby granted, free of charge, to any person obtaining a copy
101             of this software and associated documentation files (the "Software"), to deal
102             in the Software without restriction, including without limitation the rights
103             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
104             copies of the Software, and to permit persons to whom the Software is
105             furnished to do so, subject to the following conditions:
106              
107             The above copyright notice and this permission notice shall be included in
108             all copies or substantial portions of the Software.
109              
110             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
111             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
112             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
113             AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
114             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
115             OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
116             THE SOFTWARE.
117              
118             =cut