Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #63 from ktat/plugin_init_option

pass hash ref as option to plugin init method.
  • Loading branch information...
commit ca80164f0184bcd77ba4464292358ee676e65303 2 parents 09f178c + d1f1622
@nekokak authored
Showing with 41 additions and 3 deletions.
  1. +4 −3 lib/Teng.pm
  2. +37 −0 t/001_basic/033_load_plugin_with_args.t
View
7 lib/Teng.pm
@@ -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 {
View
37 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;
Please sign in to comment.
Something went wrong with that request. Please try again.