File Coverage

blib/lib/Mojolicious/Plugin/Mandel.pm
Criterion Covered Total %
statement 37 39 94.8
branch 5 8 62.5
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 49 54 90.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Mandel;
2 2     2   3437 use Modern::Perl;
  2         2  
  2         13  
3 2     2   610 use Mojo::Base 'Mojolicious::Plugin';
  2         1379  
  2         9  
4 2     2   1036 use Mojo::Loader qw(data_section find_modules load_class);
  2         40101  
  2         813  
5              
6             our $VERSION = '0.1.1'; # VERSION
7             # ABSTRACT: A plugin for mango document model called Mandel.
8              
9              
10             has mandels => sub { {} };
11              
12              
13             has mandel_documents => sub { [] };
14              
15              
16             sub register {
17 1     1 1 30 my ($plugin, $app, $conf) = @_;
18 1         2 for my $mandel_name (keys %$conf) {
19 1         1 my $mandel_classes = $conf->{$mandel_name};
20 1         2 for my $class (keys %$mandel_classes) {
21 1         2 my $error = load_class($class);
22 1 50       79023 die $error if ref $error;
23 1         9 my $obj = $class->connect($mandel_classes->{$class});
24 1         539 my @docs = $obj->all_document_names;
25 1         322 for my $doc_name (@docs) {
26 1         3 my $fullname = "$mandel_name.$doc_name";
27 1         2 push @{$plugin->mandel_documents}, $fullname;
  1         4  
28 1         6 my $collection = $obj->collection($doc_name);
29 1 50       3036 if (defined $plugin->mandels->{$doc_name}) {
30 0         0 $plugin->mandels->{$doc_name} = 0;
31             } else {
32 1         3 $plugin->mandels->{$doc_name} = $collection;
33             }
34 1 50       5 if (defined $plugin->mandels->{$fullname}) {
35 0         0 die "$mandel_name.$doc_name seems exists";
36             } else {
37 1         6 $plugin->mandels->{$fullname} = $collection;
38             }
39             }
40             }
41             }
42             $app->helper(
43             mandel => sub {
44 2     2   12848 my ($self, $name) = @_;
45 2         3 my $mandel;
46 2 100       6 return $mandel if ($mandel = $plugin->mandels->{$name});
47 1         7 my $message = "$name seems not exists in any mandel";
48 1         6 $self->flash(message => $message);
49 1         132 $self->app->log->fatal($message);
50 1         230 die;
51             }
52 1         18 );
53             $app->helper(
54             mandel_documents => sub {
55 1     1   7699 @{$plugin->mandel_documents};
  1         3  
56             }
57 1         35 );
58             }
59             1;
60              
61             __END__