| 1 | package Magpie::Plugin::DBI; | ||
| 2 | $Magpie::Plugin::DBI::VERSION = '1.141660'; | ||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: A simple Role for dealing with DBI-backed assets; | ||
| 6 | # | ||
| 7 | use Magpie::Constants; | ||
| 8 | |||
| 9 | has dsn => ( | ||
| 10 | isa => "Str", | ||
| 11 | is => "ro", | ||
| 12 | predicate => "has_dsn", | ||
| 13 | ); | ||
| 14 | |||
| 15 | has extra_args => ( | ||
| 16 | isa => "HashRef|ArrayRef", | ||
| 17 | is => "ro", | ||
| 18 | predicate => "has_extra_args", | ||
| 19 | ); | ||
| 20 | |||
| 21 | has username => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'Maybe[Str]', | ||
| 24 | predicate => 'has_username', | ||
| 25 | ); | ||
| 26 | |||
| 27 | has password => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'Maybe[Str]', | ||
| 30 | predicate => 'has_password', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub _connect_args { | ||
| 34 | my $self = shift; | ||
| 35 | my @args = ( $self->dsn || die "dsn is required" ); | ||
| 36 | |||
| 37 | if ( $self->has_username ) { | ||
| 38 | push @args, user => $self->username; | ||
| 39 | } | ||
| 40 | |||
| 41 | if ( $self->has_password ) { | ||
| 42 | push @args, password => $self->password; | ||
| 43 | } | ||
| 44 | |||
| 45 | if ( $self->has_extra_args ) { | ||
| 46 | my $extra = $self->extra_args; | ||
| 47 | |||
| 48 | if ( ref($extra) eq 'ARRAY' ) { | ||
| 49 | push @args, @$extra; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | push @args, %$extra; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | \@args; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _build_data_source { | ||
| 60 | my $self = shift; | ||
| 61 | my $k = undef; | ||
| 62 | |||
| 63 | try { | ||
| 64 | $k = $self->resolve_asset( service => 'kioku_dir' ); | ||
| 65 | } | ||
| 66 | catch { | ||
| 67 | try { | ||
| 68 | $k = KiokuDB->connect( @{ $self->_connect_args } ); | ||
| 69 | } | ||
| 70 | catch { | ||
| 71 | my $error = "Could not connect to Kioku data source: $_\n"; | ||
| 72 | warn $error; | ||
| 73 | $self->set_error( { status_code => 500, reason => $error } ); | ||
| 74 | }; | ||
| 75 | }; | ||
| 76 | |||
| 77 | return undef if $self->has_error; | ||
| 78 | $self->_kioku_scope( $k->new_scope ); | ||
| 79 | return $k; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ | ||
| 86 | =pod | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | Magpie::Plugin::DBI - A simple Role for dealing with DBI-backed assets; | ||
| 91 | |||
| 92 | =head1 VERSION | ||
| 93 | |||
| 94 | version 1.141660 | ||
| 95 | |||
| 96 | =head1 AUTHORS | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item * | ||
| 101 | |||
| 102 | Kip Hampton <kip.hampton@tamarou.com> | ||
| 103 | |||
| 104 | =item * | ||
| 105 | |||
| 106 | Chris Prather <chris.prather@tamarou.com> | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =head1 COPYRIGHT AND LICENSE | ||
| 111 | |||
| 112 | This software is copyright (c) 2011 by Tamarou, LLC. | ||
| 113 | |||
| 114 | This is free software; you can redistribute it and/or modify it under | ||
| 115 | the same terms as the Perl 5 programming language system itself. | ||
| 116 | |||
| 117 | =cut | ||
| 118 |