File Coverage

blib/lib/Helios/ObjectDriver.pm
Criterion Covered Total %
statement 29 54 53.7
branch 0 8 0.0
condition 0 3 0.0
subroutine 10 16 62.5
pod 1 6 16.6
total 40 87 45.9


line stmt bran cond sub pod time code
1             package Helios::ObjectDriver;
2              
3 1     1   13 use 5.008;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         16  
5 1     1   2 use warnings;
  1         1  
  1         18  
6              
7 1     1   2 use Helios::Config;
  1         2  
  1         21  
8 1     1   4 use Helios::ObjectDriver::DBI;
  1         1  
  1         4  
9 1     1   19 use Helios::Error;
  1         1  
  1         16  
10 1     1   3 use Helios::Error::ObjectDriverError;
  1         1  
  1         10  
11              
12             our $VERSION = '2.80';
13              
14             =head1 NAME
15              
16             Helios::ObjectDriver - class to return Helios::ObjectDriver::DBI objects pointing to the collective database
17              
18             =head1 SYNOPSIS
19              
20             # in code that needs a Helios::ObjectDriver::DBI object
21             my $driver = Helios::ObjectDriver->getDriver();
22            
23             # if the config parameters have already been parsed,
24             # save the trouble of reparsing them by passing the configuration
25             my $conf = Helios::Config->parseConfig(service_name => 'MyService');
26             my $driver = Helios::ObjectDriver->getDriver(config => $conf);
27              
28             =head1 DESCRIPTION
29              
30             The Helios::ObjectDriver class provides methods to create
31             Helios::ObjectDriver::DBI driver objects that connect to the Helios collective
32             database. Having this code in a central class reduces the amount of duplicated
33             code in various other Helios classes and provides a level of abstraction
34             between the Helios API classes and Data::ObjectDriver, the current ORM.
35              
36             Like Helios::Config and Helios::Logger classes, Helios::ObjectDriver is not
37             designed to be instantiated and thus provides only class methods.
38              
39             =head1 ACCESSOR METHODS
40              
41             debug
42             set/getConfig
43             set/getDriver
44              
45             =cut
46              
47             my $Debug = 0;
48 0 0   0 0   sub debug { my $self = shift; @_ ? $Debug = shift : $Debug; }
  0            
49              
50             my $Config;
51             sub setConfig {
52 0     0 0   my $var = $_[0]."::Config";
53 1     1   104 no strict 'refs';
  1         2  
  1         50  
54 0           $$var = $_[1];
55             }
56             sub getConfig {
57 0     0 0   my $var = $_[0]."::Config";
58 1     1   3 no strict 'refs';
  1         2  
  1         47  
59 0           return $$var;
60             }
61              
62             my $Driver;
63             sub setDriver {
64 0     0 0   my $var = $_[0]."::Driver";
65 1     1   3 no strict 'refs';
  1         1  
  1         197  
66 0           $$var = $_[1];
67             }
68              
69             sub getDriver {
70 0     0 0   initDriver(@_);
71             }
72              
73              
74             =head2 initDriver([config => $config_hashref])
75              
76             The initDriver() method is the method that actually instantiates the driver
77             object connecting to the database.
78              
79             If a config is specified, Helios::ObjectDriver will use the parameters in the
80             config hash to connect to the database and instantiate the driver object. If
81             config is not specified, initDriver() will call Helios::Config->parseConfig()
82             to (re)parse the current Helios configuration itself. If the Helios config has
83             been parsed already, specifying it in the initDriver() call will save some
84             time.
85              
86             THROWS: Helios::Error::ObjectDriverError if the driver creation fails.
87              
88             =cut
89              
90             sub initDriver {
91 0     0 1   my $self = shift;
92 0           my %params = @_;
93 0           my $conf;
94             my $driver;
95            
96             # handle config
97 0   0       $conf = $params{config} || $self->getConfig() || Helios::Config->parseConfig();
98 0           $self->setConfig($conf);
99              
100 0 0         if ($self->debug) { print __PACKAGE__.'->initDriver('.$conf->{dsn}.','.$conf->{user}.")\n"; }
  0            
101              
102             eval {
103             $driver = Helios::ObjectDriver::DBI->new(
104             dsn => $conf->{dsn},
105             username => $conf->{user},
106             password => $conf->{password}
107 0           );
108 0 0         if ($self->debug) { print __PACKAGE__.'->initDriver() DRIVER: ',$driver,"\n"; }
  0            
109 0           1;
110 0 0         } or do {
111 0           my $E = $@;
112 0           Helios::Error::ObjectDriverError->throw("$E");
113             };
114            
115 0           $self->setDriver($driver);
116 0           return $driver;
117             }
118              
119              
120              
121             1;
122             __END__