package Admin_server::Parser;
use MooseX::FollowPBP; # Simon Cozens Perl Best Practices.
use Moose; # Sugar!!
use Module::Pluggable::Object; # To enable application plugins.
use aliased 'Admin_server::Interface::ParserInterface';
#----------------------------------------------------------
# Define attributes.
#----------------------------------------------------------
has 'plugins' => ( is => 'ro', lazy_build => 1, isa => 'HashRef' );
#----------------------------------------------------------
# Define methods.
#----------------------------------------------------------
sub _build_plugins {
#=== FUNCTION ====================================
# NAME: _build_plugins
# PURPOSE: To build up the applications hash.
# PARAMETERS: None.
# RETURNS: Nothing.
# DESCRIPTION: Populates the applications hash with the list of
# available application parser plugins.
# THROWS: no exceptions
# COMMENTS: none
#===============================================
my ($self) = @_;
my $base = __PACKAGE__ . '::Plugin';# Location of parser plugins.
my %parser_plugins; # Stores the instantiated plugins.
# Scan plugins at $base.
my $mpo = Module::Pluggable::Object->new( search_path => [$base] );
my @available_plugins = $mpo->plugins();
print 'Found plugins ...', "@available_plugins", "\n";
# Load the available plugins.
foreach my $plugin (@available_plugins) {
print "Loading plugin $plugin", "\n";
Class::MOP::load_class($plugin);
print "finished loading $plugin" . "\n"
if ( Class::MOP::is_class_loaded($plugin) );
# Ensure that each class confirms to the plugin API.
confess("Plugin $plugin in ${base}"
. ' namespace doesn\'t implement ParserInterface.' )
unless ( $plugin->does(ParserInterface) );
# Record and instantiate the valid plugin objects in a hash.
( my $plugin_name = $plugin ) =~ s/\Q${base}::\E//;
$parser_plugins{$plugin_name} = $plugin->new();
}
print STDOUT sort keys %parser_plugins, "\n";
return \%parser_plugins;
} # ---------- end of subroutine _build_applications ----------
####
$prove -lv t/Parser.t
t/Parser.t ..
1..4
ok 1 - use Admin_server::Parser;
ok 2 - Admin_server::Parser can be instantiated.
ok 3 - Instantiated object is of type Admin_server::Parser
Found plugins ...Admin_server::Parser::Plugin::FeedHandler
Loading plugin Admin_server::Parser::Plugin::FeedHandler
Can't locate object method "does" via package "Admin_server::Parser::Plugin::FeedHandler" (perhaps you forgot to load "Admin_server::Parser::Plugin::FeedHandler"?) at /Volumes/UserData/Users/tintin/Programs/Perl/Work_area/Admin_server/lib/Admin_server/Parser.pm line 68.
# Looks like you planned 4 tests but ran 3.
# Looks like your test exited with 255 just after 3.
Dubious, test returned 255 (wstat 65280, 0xff00)
Failed 1/4 subtests
Test Summary Report
-------------------
t/Parser.t (Wstat: 65280 Tests: 3 Failed: 0)
Non-zero exit status: 255
Parse errors: Bad plan. You planned 4 tests but ran 3.
Files=1, Tests=3, 1 wallclock secs ( 0.03 usr 0.01 sys + 0.24 cusr 0.02 csys = 0.30 CPU)
Result: FAIL
####
############################################################
########### PRAGMATA ########
############################################################
use strict;
use warnings;
use Test::More tests => 4; # last test to print
############################################################
########### VARIABLES USED ########
############################################################
############################################################
########### TESTING BEGINS ########
############################################################
#----------------------------------------------------------
# Ensure that Admin_server::Parser can be use'd.
#----------------------------------------------------------
BEGIN {
use_ok('Admin_server::Parser');
}
#----------------------------------------------------------
# Check if the plugin can be instantiated.
#----------------------------------------------------------
ok(
my $parser = Admin_server::Parser->new(),
'Admin_server::Parser can be instantiated.'
);
#----------------------------------------------------------
# Conform that the new object is of the correct type.
#----------------------------------------------------------
is( ref($parser), 'Admin_server::Parser',
'Instantiated object is of type Admin_server::Parser' );
#----------------------------------------------------------
# Check whether plugins are correctly loaded.
#----------------------------------------------------------
ok( $parser->get_plugins(), 'Plugin hash can be accessed.' );
#ok( exists ${ $parser->get_plugins() }->{'FeedHandler'},
# 'Feed handler plugin found.' );
done_testing();
####
$ls -1R
bin
config
lib
logs
t
./bin:
Master_server.pl
Master_server.pl.bkp
./config:
admin_server.conf
./lib:
Admin_server
./lib/Admin_server:
Interface
Parser
Parser.pm
./lib/Admin_server/Interface:
ParserInterface.pm
./lib/Admin_server/Parser:
Plugin
./lib/Admin_server/Parser/Plugin:
FeedHandler.pm
./logs:
Application
admin_server.log
./logs/Application:
./t:
01app.t
Interface
Parser.t
Plugin
./t/Interface:
ParserInterface.t
./t/Plugin:
FeedHandler.t