File Coverage

blib/lib/Fey/DBIManager.pm
Criterion Covered Total %
statement 36 37 97.3
branch 6 6 100.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
1             package Fey::DBIManager;
2             BEGIN {
3 1     1   62096 $Fey::DBIManager::VERSION = '0.16';
4             }
5              
6 1     1   9 use strict;
  1         3  
  1         36  
7 1     1   4 use warnings;
  1         3  
  1         27  
8 1     1   869092 use namespace::autoclean;
  1         140041  
  1         6  
9              
10 1     1   16637 use Fey::Exceptions qw( object_state_error param_error );
  1         725654  
  1         92  
11 1     1   11 use Scalar::Util qw( blessed );
  1         4  
  1         48  
12              
13 1     1   1058 use Fey::DBIManager::Source;
  1         4  
  1         91  
14              
15 1     1   14 use Moose 0.90;
  1         32  
  1         10  
16 1     1   7795 use MooseX::SemiAffordanceAccessor;
  1         3  
  1         12  
17 1     1   3757 use MooseX::StrictConstructor;
  1         4  
  1         6  
18              
19             has _sources => (
20             traits => ['Hash'],
21             is => 'ro',
22             isa => 'HashRef[Fey::DBIManager::Source]',
23             default => sub { {} },
24             init_arg => undef,
25             handles => {
26             get_source => 'get',
27             add_source => 'set',
28             remove_source => 'delete',
29             _source_count => 'count',
30             has_source => 'exists',
31             sources => 'values',
32             },
33             );
34              
35             around 'add_source' => sub {
36             my $orig = shift;
37             my $self = shift;
38              
39             my $source;
40             if ( @_ > 1 ) {
41             $source = Fey::DBIManager::Source->new(@_);
42             }
43             else {
44             $source = shift;
45             }
46              
47             my $name;
48             if ( blessed $source && $source->can('name') ) {
49             $name = $source->name();
50              
51             param_error qq{You already have a source named "$name".}
52             if $self->has_source($name);
53             }
54              
55             my $return = $self->$orig( $name => $source );
56              
57             return $return;
58             };
59              
60             sub default_source {
61 9     9 1 2142 my $self = shift;
62              
63 9 100       460 if ( $self->_source_count() == 0 ) {
    100          
    100          
64 1         8 object_state_error
65             'This manager has no default source because it has no sources at all.';
66             }
67             elsif ( $self->_source_count() == 1 ) {
68              
69             # Need to force scalar context for the return value
70 1         43 return ( $self->sources() )[0];
71             }
72             elsif ( my $source = $self->get_source('default') ) {
73 5         30 return $source;
74             }
75             else {
76 2         9 object_state_error
77             'This manager has multiple sources, but none are named "default".';
78             }
79              
80 0         0 return;
81             }
82              
83             sub source_for_sql {
84 2     2 1 1280 my $self = shift;
85              
86 2         7 return $self->default_source();
87             }
88              
89             __PACKAGE__->meta()->make_immutable();
90              
91             1;
92              
93             # ABSTRACT: Manage a set of DBI handles
94              
95              
96              
97             =pod
98              
99             =head1 NAME
100              
101             Fey::DBIManager - Manage a set of DBI handles
102              
103             =head1 VERSION
104              
105             version 0.16
106              
107             =head1 SYNOPSIS
108              
109             my $manager = Fey::DBIManager->new();
110              
111             $manager->add_source( dsn => 'dbi:Pg:dbname=MyDB' );
112              
113             my $source = $manager->default_source();
114              
115             my $source = $manager->source_for_sql($select_sql);
116              
117             =head1 DESCRIPTION
118              
119             C<Fey::DBIManager> manager a set of L<Fey::DBIManager::Source>
120             objects, each of which in turn represents a single C<DBI> handle.
121              
122             It's main purpose is to provide a single interface to one or more data
123             sources, allowing you to easily define your database connections in
124             one spot.
125              
126             =head1 METHODS
127              
128             This class provides the following methods:
129              
130             =head2 Fey::DBIManager->new()
131              
132             Returns a new C<Fey::DBIManager> object.
133              
134             =head2 $manager->add_source(...)
135              
136             This method adds a new L<Fey::DBIManager::Source> object to the
137             manager. It can either accept an instantiated
138             L<Fey::DBIManager::Source> object, or a set of parameters needed to
139             create a new source.
140              
141             Sources are identified by name, and if you try to add one that already
142             exists in the manager, an error will be thrown.
143              
144             =head2 $manager->get_source($name)
145              
146             Given a source name, this returns the named source, if it exists in
147             the manager.
148              
149             =head2 $manager->remove_source($name)
150              
151             Removes the named source, if it exists in the manager.
152              
153             =head2 $manager->has_source($name)
154              
155             Returns true if the manager has the named source.
156              
157             =head2 $manager->sources()
158              
159             Returns all of the source in the manager.
160              
161             =head2 $manager->default_source()
162              
163             This method returns the default source for the manager. If the manager
164             has only one source, then this is the default. Otherwise it looks for
165             a source named "default". If no such source exists, or if the manager
166             has no sources at all, then an exception is thrown.
167              
168             =head2 $manager->source_for_sql($sql_object)
169              
170             This method accepts a single C<Fey::SQL> object and returns an
171             appropriate source.
172              
173             By default, this method simply returns the default source. It exists
174             to provide a spot for subclasses which want to do something more
175             clever, such as use one source for reads and another for writes.
176              
177             =head1 BUGS
178              
179             Please report any bugs or feature requests to
180             C<bug-fey-dbimanager@rt.cpan.org>, or through the web interface at
181             L<http://rt.cpan.org>. I will be notified, and then you'll
182             automatically be notified of progress on your bug as I make changes.
183              
184             =head1 AUTHOR
185              
186             Dave Rolsky <autarch@urth.org>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is Copyright (c) 2011 by Dave Rolsky.
191              
192             This is free software, licensed under:
193              
194             The Artistic License 2.0 (GPL Compatible)
195              
196             =cut
197              
198              
199             __END__
200