File Coverage

blib/lib/Data/Phrasebook/Loader.pm
Criterion Covered Total %
statement 37 38 97.3
branch 9 10 90.0
condition 2 2 100.0
subroutine 7 7 100.0
pod 1 1 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1             package Data::Phrasebook::Loader;
2 12     12   11673 use strict;
  12         26  
  12         467  
3 12     12   61 use warnings FATAL => 'all';
  12         24  
  12         433  
4 12     12   66 use base qw( Data::Phrasebook::Debug );
  12         27  
  12         1213  
5 12     12   73 use Carp qw( croak );
  12         25  
  12         8487  
6              
7 12     12   12115 use Module::Pluggable search_path => ['Data::Phrasebook::Loader'];
  12         163453  
  12         99  
8              
9 12     12   1100 use vars qw($VERSION);
  12         25  
  12         4569  
10             $VERSION = '0.35';
11              
12             =head1 NAME
13              
14             Data::Phrasebook::Loader - Plugin Loader module
15              
16             =head1 SYNOPSIS
17              
18             my $loader = Data::Phrasebook::Loader->new( class => 'Text' );
19              
20             =head1 DESCRIPTION
21              
22             C acts as an autoloader for phrasebook plugins.
23              
24             =head1 CONSTRUCTOR
25              
26             =head2 new
27              
28             C takes one optional named argument: the class. It returns a new
29             instance to the class. Any further arguments to C are given to
30             the C method of the appropriate class.
31              
32             If no class is specified the default class of 'Text' is used.
33              
34             my $loader = Data::Phrasebook::Loader->new();
35              
36             OR
37              
38             my $loader = Data::Phrasebook::Loader->new( class => 'Text' );
39              
40             =cut
41              
42             my $DEFAULT_CLASS = 'Text';
43              
44             sub new {
45 27     27 1 957 my $self = shift;
46 27         97 my %args = @_;
47 27   100     132 my $class = delete $args{class} || 'Text';
48              
49 27 100       150 if($self->debug) {
50 1         8 $self->store(3,"$self->new IN");
51 1         7 $self->store(4,"$self->new class=[$class]");
52             }
53              
54             # in the event we have been subclassed
55 27         180 $self->search_path( add => "$self" );
56              
57 27         423 my $plugin;
58 27         115 my @plugins = $self->plugins();
59 27         283697 for(@plugins) {
60 60 100       596 $plugin = $_ if($_ =~ /\b$class$/);
61             }
62              
63 27 100       366 croak("no loader available of that name\n") unless($plugin);
64              
65             eval {
66 26         136 (my $file = $plugin) =~ s|::|/|g;
67 26         6480 require $file . '.pm';
68 26         436 $plugin->import();
69 26         145 1;
70 26 50       65 } or do {
71 0         0 croak "Couldn't require $plugin : $@";
72             };
73              
74 26 100       245 $self->store(4,"$self->new plugin=[$plugin]") if($self->debug);
75 26         209 return $plugin->new( %args );
76             }
77              
78             1;
79              
80             __END__