Skip to content

Instantly share code, notes, and snippets.

@issm
Created August 7, 2011 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save issm/1130238 to your computer and use it in GitHub Desktop.
Save issm/1130238 to your computer and use it in GitHub Desktop.
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