Mojolicious-Plugin-Mandel

 view release on metacpan or  search on metacpan

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

package Mojolicious::Plugin::Mandel;
use Modern::Perl;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Loader qw(data_section find_modules load_class);

our $VERSION = '0.1.1'; # VERSION
# ABSTRACT: A plugin for mango document model called Mandel.


has mandels => sub { {} };


has mandel_documents => sub { [] };


sub register {
  my ($plugin, $app, $conf) = @_;
  for my $mandel_name (keys %$conf) {
    my $mandel_classes = $conf->{$mandel_name};
    for my $class (keys %$mandel_classes) {
      my $error = load_class($class);
      die $error if ref $error;
      my $obj = $class->connect($mandel_classes->{$class});
      my @docs = $obj->all_document_names;
      for my $doc_name (@docs) {
        my $fullname = "$mandel_name.$doc_name";
        push @{$plugin->mandel_documents}, $fullname;
        my $collection = $obj->collection($doc_name);
        if (defined $plugin->mandels->{$doc_name}) {
          $plugin->mandels->{$doc_name} = 0;
        } else {
          $plugin->mandels->{$doc_name} = $collection;
        }
        if (defined $plugin->mandels->{$fullname}) {
          die "$mandel_name.$doc_name seems exists";
        } else {
          $plugin->mandels->{$fullname} = $collection;
        }
      }
    }
  }
  $app->helper(
    mandel => sub {
      my ($self, $name) = @_;
      my $mandel;
      return $mandel if ($mandel = $plugin->mandels->{$name});
      my $message = "$name seems not exists in any mandel";
      $self->flash(message => $message);
      $self->app->log->fatal($message);
      die;
    }
  );
  $app->helper(
    mandel_documents => sub {
      @{$plugin->mandel_documents};
    }
  );
}
1;

__END__



( run in 0.780 second using v1.01-cache-2.11-cpan-ceb78f64989 )