Mojolicious-Plugin-Mango

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/Mango.pm  view on Meta::CPAN

package Mojolicious::Plugin::Mango;
use Modern::Perl;
use Mango;
use Mojo::Base 'Mojolicious::Plugin';
use namespace::clean;

our $VERSION = '0.0.2'; # VERSION
# ABSTRACT: provide mango helpers to Mojolicious

sub register {
  my $self = shift;
  my $app  = shift;
  my $conf = shift || {};
  $conf->{helper} ||= 'db';
  $conf->{default_db} ||= 'test';
  $app->attr('_mango' => sub {
      my $m = Mango->new($conf->{mango});
      $m->default_db($conf->{default_db});
      $m->hosts($conf->{hosts}) if $conf->{hosts};
      $m;
    }
  );
  $app->helper('mango' => sub { shift; Mango->new(@_) });
  $app->helper($conf->{helper} => sub {
      my $self = shift;
      return $self->app->_mango->db(@_);
    }
  );
  $app->helper('hosts' => sub {
      my $self = shift;
      return $self->app->_mango->hosts(@_);
    });
  $app->helper('default_db' => sub {
      my $self = shift;
      return $self->app->_mango->default_db(@_);
    });
  $app->helper('coll' => sub {
      my $self = shift;
      return $self->app->_mango->db->collection(@_);
    });
  for my $helper (qw/get_more kill_cursors query/) {
    next if (defined ($conf->{"no_$helper"}));
    $app->helper($helper => sub {
        my $self = shift;
        $self->app->_mango->$helper(@_);
      })
  }
  for my $helper (qw/collection collection_names command dereference gridfs stats/) {
    next if (defined ($conf->{"no_$helper"}));
    $app->helper($helper => sub {
        my $self = shift;
        $self->app->_mango->db->$helper(@_);
      })
  }
}

1;

__END__

=pod



( run in 0.714 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )