Created
August 7, 2011 09:19
-
-
Save issm/1130238 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::Plugin::MongoDB; | |
use strict; | |
use warnings; | |
use utf8; | |
use MongoDB; | |
sub init { | |
my ($class, $c, $params) = @_; | |
no strict 'refs'; | |
*{"$c\::db"} = \&_db; | |
} | |
sub _db { | |
my ($self) = @_; | |
if ( !defined $self->{db} ) { | |
my $conf = $self->config->{'MongoDB'} | |
or die "missing configuration for 'MongoDB'"; | |
my $dbname = $conf->{database} | |
or die "missing 'MongoDB.database'"; | |
my %params_conn = ( | |
host => $conf->{host} || 'localhost', | |
port => $conf->{port} || '27017', | |
); | |
$params_conn{username} = $conf->{username} if $conf->{username}; | |
$params_conn{password} = $conf->{password} if $conf->{password}; | |
my $conn = MongoDB::Connection->new(%params_conn); | |
$self->{db} = $conn->$dbname; | |
} | |
return $self->{db}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment