blib/lib/Morris/Plugin/DBI.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran n/a Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 42 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Morris::Plugin::DBI;
2 use Moose;
3 use AnyEvent::DBI;
4 use namespace::clean -except => qw(meta);
5  
6 extends 'Morris::Plugin';
7  
8 # This plugin does not respond to anything, but it just acts as a
9 # shared resource for others
10  
11 has instances => (
12 traits => ['Hash'],
13 is => 'ro',
14 isa => 'HashRef',
15 handles => {
16 get_instance => 'get',
17 }
18 );
19  
20 around BUILDARGS => sub {
21 my ($next, $self, @args) = @_;
22 my $args = $next->($self, @args);
23  
24 # convert each instance into a real DBI handle
25 my $instances = $args->{instances} ||= delete $args->{instance};
26 foreach my $name ( keys %$instances ) {
27 my $config = $instances->{$name};
28 $instances->{$name} = AnyEvent::DBI->new(
29 $config->{dsn},
30 $config->{username},
31 $config->{password},
32 (%{ $config->{options} || {} }, exec_server => 1 ),
33 );
34 }
35 return $args;
36 };
37  
38 after register => sub {
39 my ($self, $conn) = @_;
40 # Add a get_dbh method by reblessing the connection instance to
41 # a newly created anon class
42 my $meta = $conn->meta;
43 my $new_meta = Moose::Meta::Class->create_anon_class(
44 superclasses => [ $meta->name ],
45 cache => 1,
46 methods => {
47 get_dbh => sub {
48 my $name = $_[1];
49 $self->get_instance( $name );
50 }
51 }
52 );
53 $new_meta->rebless_instance( $conn );
54 };
55  
56 __PACKAGE__->meta->make_immutable();
57  
58 1;
59  
60 __END__