File Coverage

blib/lib/Catalyst/Model/DBR.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Catalyst::Model::DBR;
2              
3 1     1   2752 use Moose;
  0            
  0            
4             use Carp;
5             extends 'Catalyst::Model';
6             has dbrconf => ( is => 'ro', required => 1);
7             has dbr => ( is => 'rw', isa => 'DBR' );
8             has schema_name => ( is => 'ro' );
9             has autoload_ok => ( is => 'rw' );
10              
11             use DBR;
12             use DBR::Util::Logger;
13              
14             our $VERSION = '1.0';
15             our $AUTOLOAD;
16              
17             sub BUILD{
18             my ($self) = @_;
19              
20             my $logger = new DBR::Util::Logger(-logpath => '/tmp/dbr_catalyst.log', -logLevel => 'debug2') or confess "Failed to create logger";
21              
22             my $dbr = new DBR(
23             -logger => $logger,
24             -conf => $self->dbrconf,
25             ) or confess "Failed to create DBR";
26              
27             $self->dbr( $dbr );
28             $self->autoload_ok( 0 ) unless $self->schema_name;
29             }
30              
31             sub connect {
32             my ($self,$schema_name) = @_;
33             my $s = ($self->schema_name || $schema_name) or croak "Schema identifier is required";
34             return $self->dbr->connect( $s ) or confess "Failed to connect to $s";
35             }
36              
37             sub AUTOLOAD {
38             my $self = shift;
39             my $method = $AUTOLOAD;
40             $self->autoload_ok or croak "Autoload is not allowed";
41              
42             my @params = @_;
43              
44             $method =~ s/.*:://;
45             return unless $method =~ /[^A-Z]/; # skip DESTROY and all-cap methods
46              
47             my $dbr = $self->dbr;
48             my $inst = $dbr ->get_instance( $self->schema_name ) or confess "failed to retrieve instance for schema " . $self->schema_name;
49             my $schema = $inst ->schema or croak ("Cannot autoload '$method' when no schema is defined");
50             my $table = $schema ->get_table ( $method ) or croak ("no such table '$method' exists in this schema");
51              
52             my $object = DBR::Interface::Object->new(
53             session => $dbr->session,
54             instance => $inst,
55             table => $table,
56             ) or confess('failed to create query object');
57              
58             return $object;
59             }
60              
61             =pod
62              
63             =head1 NAME
64              
65             Catalyst::Model::DBR - DBR Model Class
66              
67             =head1 SYNOPSIS
68              
69              
70             =head1 DESCRIPTION
71              
72             This is the C<DBR> model class.
73              
74             =head1 METHODS
75              
76             =over 1
77              
78             =item new
79              
80             Initializes DBR object
81              
82             =back
83              
84             =head1 SEE ALSO
85              
86             L<Catalyst>, L<DBR>
87              
88             =head1 AUTHOR
89              
90             Daniel, C<impious@cpan.org>
91              
92             =head1 LICENSE
93              
94             This program is free software, you can redistribute it and/or modify it
95             under the same terms as Perl itself.
96              
97             =cut
98              
99             1;