refactor translator to use Module::Pluggable to find child classes
Matt S Trout [Fri, 6 Feb 2009 18:18:58 +0000 (18:18 +0000)]
lib/LolCatalyst/Lite/Translator.pm

index 7dd978e..ca6c22b 100644 (file)
@@ -1,6 +1,6 @@
 package LolCatalyst::Lite::Translator;
 
-use LolCatalyst::Lite::Translator::LOLCAT;
+use Module::Pluggable::Object;
 
 use Moose;
 use namespace::clean -except => 'meta';
@@ -15,7 +15,18 @@ has 'translators' => (
 
 sub _build_translators {
   my ($self) = @_;
-  return { LOLCAT => LolCatalyst::Lite::Translator::LOLCAT->new };
+  my $base = __PACKAGE__;
+  my $mp = Module::Pluggable::Object->new(
+    search_path => [ $base ]
+  );
+  my @classes = $mp->plugins;
+  my %translators;
+  foreach my $class (@classes) {
+    Class::MOP::load_class($class);
+    (my $name = $class) =~ s/^\Q${base}::\E//;
+    $translators{$name} = $class->new;
+  }
+  return \%translators;
 }
 
 sub translate {