File Coverage

blib/lib/Plugins/Factory.pm
Criterion Covered Total %
statement 21 40 52.5
branch 0 12 0.0
condition 0 6 0.0
subroutine 7 10 70.0
pod n/a
total 28 68 41.1


line stmt bran cond sub pod time code
1             package Plugins::Factory;
2              
3 1     1   27610 use strict;
  1         2  
  1         37  
4 1     1   4 use warnings;
  1         3  
  1         42  
5              
6             our $VERSION = '0.01';
7              
8 1     1   3020 use Module::Load;
  1         1739  
  1         9  
9             require Module::Pluggable;
10              
11             # save all plugin and package
12             $Plugins::Factory::job_name = {};
13              
14             # save all code ref
15             $Plugins::Factory::ref_code = {};
16              
17             sub import {
18 1     1   79 my ($class,%opts) = @_;
19 1         5 my ($call_pkg) = caller;
20 0           map {
21 1         15 my $pl_name = $_;
22 0 0         my $namespace = $opts{$pl_name}->{'plugin_namespace'} if exists $opts{$pl_name}->{'plugin_namespace'};
23 0 0 0       next unless (ref $opts{$pl_name} eq 'HASH' and $namespace);
24 0           my $method = $opts{$pl_name}->{'init_method'};
25            
26 0           Module::Pluggable->import(search_path => [$namespace]);
27 0           foreach my $pkg (__PACKAGE__->plugins) {
28 0           load $pkg;
29 0           (my $short_name = $pkg) =~ s/^${namespace}:://;
30 0           push @{$Plugins::Factory::job_name->{$call_pkg}->{$pl_name}},$pkg;
  0            
31 0     0     $Plugins::Factory::ref_code->{$call_pkg}->{$pl_name}->{$short_name} =
32             $Plugins::Factory::ref_code->{$call_pkg}->{$pl_name}->{$pkg} =
33 0 0 0 0     ($method and $pkg->can($method)) ? sub {$pkg->$method(@_)} : sub {$pkg};
  0            
34             }
35            
36 1     1   469 no strict 'refs';
  1         1  
  1         34  
37 1     1   5 no warnings qw(redefine prototype);
  1         3  
  1         257  
38             # add function in traget package
39 0           *{"$call_pkg\::$pl_name"} = sub {
40 0     0     my ($class,$name,@init) = @_;
41 0 0         return $Plugins::Factory::job_name->{$call_pkg}->{$pl_name} unless $name;
42 0 0         return $Plugins::Factory::ref_code->{$call_pkg}->{$pl_name}->{$name}->(@init) if exists $Plugins::Factory::ref_code->{$call_pkg}->{$pl_name}->{$name};
43 0 0         } if exists $Plugins::Factory::ref_code->{$call_pkg}->{$pl_name};
44 1     1   6 use strict 'refs';
  1         2  
  1         95  
45 0           1;
46             } keys %opts;
47            
48             }
49              
50              
51             1;
52             __END__