Permalink
Browse files

Added load-modifiers() method for dynamic plugin support.

  • Loading branch information...
1 parent 7dfa3f5 commit 9dcac97df502264bfb5495de71fc9e3b5b52ae8c @supernovus committed Oct 8, 2010
Showing with 20 additions and 7 deletions.
  1. +17 −1 lib/Flower.pm
  2. +1 −2 t/05-perl-modifier.t
  3. +1 −2 t/06-debug-modifier.t
  4. +1 −2 t/08-utils-list.t
View
@@ -360,9 +360,25 @@ method add-modifier($name, Callable $routine) {
## my $flower = Flower.new(:file('template.xml'));
## $flower.add-modifiers(Flower::Utils::Logic::all());
##
-method add-modifiers(%modifiers) {
+multi method add-modifiers(%modifiers) {
for %modifiers.kv -> $key, $val {
self.add-modifier($key, $val);
}
}
+## The newest method for loading modifiers.
+## Pass it a list of libraries which have all() subs
+## and it will load them.
+## If the library name doesn't have a :: in it,
+## load-modifiers prepends "Flower::Utils::" to it.
+multi method load-modifiers(*@modules) {
+ for @modules -> $module {
+ my $plugin = $module;
+ if $plugin !~~ /'::'/ {
+ $plugin = "Flower::Utils::$plugin";
+ }
+ eval("use $plugin");
+ self.add-modifiers(eval($plugin~'::all()'));
+ }
+}
+
View
@@ -10,7 +10,6 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-use Flower::Utils::Perl;
plan 1;
@@ -23,7 +22,7 @@ my %ahash = {
'anobj' => FooBar.new,
}
-$flower.add-modifiers(Flower::Utils::Perl::all());
+$flower.load-modifiers('Perl');
is $flower.parse(ahash => %ahash), $xml~'<test>Hello world</test>', 'perl: modifier';
View
@@ -4,7 +4,6 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-use Flower::Utils::Debug;
plan 1;
@@ -17,7 +16,7 @@ my %ahash = {
'anarray' => [ 'one', 'two', 'three' ],
}
-$flower.add-modifiers(Flower::Utils::Debug::all());
+$flower.load-modifiers('Debug');
is $flower.parse(object => %ahash), $xml~'<test><dump type="Hash">{"anarray" => ["one", "two", "three"]}</dump></test>', 'dump: and what: modifiers';
View
@@ -4,7 +4,6 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-use Flower::Utils::List;
plan 2;
@@ -14,7 +13,7 @@ my $template = '<table><tr tal:repeat="row group:2 items"><td tal:repeat="col ro
my $flower = Flower.new(:template($template));
-$flower.add-modifiers(Flower::Utils::List::all());
+$flower.load-modifiers('List');
is $flower.parse(:items(['a'..'d'])), $xml~'<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>', 'group: modifier';

0 comments on commit 9dcac97

Please sign in to comment.