| 1 | package MooX::Role::DBIConnection; | ||||||||||
| 2 | 3 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use Filter::signatures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use feature 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | no warnings 'experimental::signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | MooX::Role::DBIConnection - handy mixin for objects with a DB connection | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | { package My::Example; | ||||||||||
| 17 | use Moo 2; | ||||||||||
| 18 | with 'MooX::Role::DBIConnection'; | ||||||||||
| 19 | }; | ||||||||||
| 20 | |||||||||||
| 21 | # Connect using the parameters | ||||||||||
| 22 | my $writer = My::Example->new( | ||||||||||
| 23 | dbh => { | ||||||||||
| 24 | dsn => '...', | ||||||||||
| 25 | user => '...', | ||||||||||
| 26 | password => '...', | ||||||||||
| 27 | options => '...', | ||||||||||
| 28 | }, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | # ... or alternatively if you have a connection already | ||||||||||
| 32 | my $writer2 = My::Example->new( | ||||||||||
| 33 | dbh => $dbh, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | This module enhances your class constructor by allowing you to pass in either | ||||||||||
| 37 | a premade C<dbh> or the parameters needed to create one. | ||||||||||
| 38 | |||||||||||
| 39 | It will create the C<dbh> accessor | ||||||||||
| 40 | |||||||||||
| 41 | =head1 NOTE | ||||||||||
| 42 | |||||||||||
| 43 | This module will likely be spun out of the Weather::MOSMIX distribution | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | has 'dbh' => ( | ||||||||||
| 49 | is => 'lazy', | ||||||||||
| 50 | default => \&_connect_db, | ||||||||||
| 51 | coerce => sub { | ||||||||||
| 52 | 0 | ▶ | my $dbh = $_[0]; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 53 | 0 | ▶ | if( ref($dbh) eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | $dbh = DBI->connect( @{$dbh}{qw{dsn user password options}}); | |||||||||
| 55 | } | ||||||||||
| 56 | $dbh | ||||||||||
| 57 | 0 | } | |||||||||
| 58 | 3 | ); | |||||||||
| 59 | |||||||||||
| 60 | #has 'dsn' => ( | ||||||||||
| 61 | # is => 'ro', | ||||||||||
| 62 | #); | ||||||||||
| 63 | # | ||||||||||
| 64 | #has 'user' => ( | ||||||||||
| 65 | # is => 'ro', | ||||||||||
| 66 | #); | ||||||||||
| 67 | # | ||||||||||
| 68 | #has 'password' => ( | ||||||||||
| 69 | # is => 'ro', | ||||||||||
| 70 | #); | ||||||||||
| 71 | # | ||||||||||
| 72 | #has 'options' => ( | ||||||||||
| 73 | # is => 'ro', | ||||||||||
| 74 | #); | ||||||||||
| 75 | # | ||||||||||
| 76 | #sub _connect_db( $self ) { | ||||||||||
| 77 | # my $dbh = DBI->connect( | ||||||||||
| 78 | # $self->dsn, $self->user, $self->password, $self->options | ||||||||||
| 79 | # ); | ||||||||||
| 80 | #} | ||||||||||
| 81 | |||||||||||
| 82 | 3 | 1; | |||||||||
| 83 | |||||||||||
| 84 | =head1 REPOSITORY | ||||||||||
| 85 | |||||||||||
| 86 | The public repository of this module is | ||||||||||
| 87 | L<https://github.com/Corion/weather-mosmix>. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 SUPPORT | ||||||||||
| 90 | |||||||||||
| 91 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||
| 92 | |||||||||||
| 93 | =head1 BUG TRACKER | ||||||||||
| 94 | |||||||||||
| 95 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||
| 96 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Weather-MOSMIX> | ||||||||||
| 97 | or via mail to L<www-Weather-MOSMIX@rt.cpan.org|mailto:Weather-MOSMIX@rt.cpan.org>. | ||||||||||
| 98 | |||||||||||
| 99 | =head1 AUTHOR | ||||||||||
| 100 | |||||||||||
| 101 | Max Maischein C<corion@cpan.org> | ||||||||||
| 102 | |||||||||||
| 103 | =head1 COPYRIGHT (c) | ||||||||||
| 104 | |||||||||||
| 105 | Copyright 2019-2020 by Max Maischein C<corion@cpan.org>. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 LICENSE | ||||||||||
| 108 | |||||||||||
| 109 | This module is released under the same terms as Perl itself. | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||