Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass hash ref as option to plugin init method. #63

Merged
merged 1 commit into from Apr 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Teng.pm
Expand Up @@ -33,16 +33,17 @@ sub load_plugin {

$class = ref($class) if ref($class);

my $alias = delete $opt->{alias};
no strict 'refs';
for my $meth ( @{"${pkg}::EXPORT"} ) {
my $dest_meth =
( $opt->{alias} && $opt->{alias}->{$meth} )
? $opt->{alias}->{$meth}
( $alias && $alias->{$meth} )
? $alias->{$meth}
: $meth;
*{"${class}::${dest_meth}"} = $pkg->can($meth);
}

$pkg->init($pkg) if $pkg->can('init');
$pkg->init($class, $opt) if $pkg->can('init');
}

sub new {
Expand Down
37 changes: 37 additions & 0 deletions t/001_basic/033_load_plugin_with_args.t
@@ -0,0 +1,37 @@
use t::Utils;
use Test::More;

BEGIN { use_ok( 'Mock::Basic' ); }

my $dbh = t::Utils->setup_dbh;
my $db = Mock::Basic->new({dbh => $dbh});
$db->setup_test_db;

(ref $db)->load_plugin('ArgsTest');

ok defined &Mock::Basic::args_class;
ok defined &Mock::Basic::args_opt;

is $db->args_class, ref $db;

# unload plugin class;
undef &Mock::Basic::args_class;
undef &Mock::Basic::args_opt;
undef %Teng::Plugin::ArgsTest::;

my %args = (opt1 => 'a', opt2 => 'b');
(ref $db)->load_plugin('ArgsTest',
{
alias => {args_class => 'alias_args_class', args_opt => 'alias_args_opt'},
%args,
});

ok not defined &Mock::Basic::args_class;
ok not defined &Mock::Basic::args_opt;
ok defined &Mock::Basic::alias_args_class;
ok defined &Mock::Basic::alias_args_opt;

is $db->alias_args_class, ref $db;
is_deeply $db->alias_args_opt, \%args;

done_testing;