factor can checks out into roles
Arthur Axel 'fREW' Schmidt [Sat, 3 Nov 2012 06:20:38 +0000 (01:20 -0500)]
lib/Log/Contextual.pm
lib/Log/Contextual/Role/Router/SetLogger.pm [new file with mode: 0644]
lib/Log/Contextual/Role/Router/WithLogger.pm [new file with mode: 0644]
lib/Log/Contextual/Router.pm

index 0296af3..70348b6 100644 (file)
@@ -108,22 +108,20 @@ sub after_import { return arg_router()->after_import(@_) }
 
 sub set_logger {
    my $router = arg_router();
-   my $meth = $router->can('set_logger');
 
    die ref($router) . " does not support set_logger()"
-      unless defined $meth;
+      unless $router->does('Log::Contextual::Role::Router::SetLogger');
 
-   return $router->$meth(@_);
+   return $router->set_logger(@_);
 }
 
 sub with_logger {
    my $router = arg_router();
-   my $meth = $router->can('with_logger');
 
    die ref($router) . " does not support with_logger()"
-      unless defined $meth;
+      unless $router->does('Log::Contextual::Role::Router::WithLogger');
 
-   return $router->$meth(@_);
+   return $router->with_logger(@_);
 }
 
 1;
diff --git a/lib/Log/Contextual/Role/Router/SetLogger.pm b/lib/Log/Contextual/Role/Router/SetLogger.pm
new file mode 100644 (file)
index 0000000..cebe6c5
--- /dev/null
@@ -0,0 +1,8 @@
+package Log::Contextual::Role::Router::SetLogger;
+
+use Moo::Role;
+
+requires 'set_logger';
+
+1;
+
diff --git a/lib/Log/Contextual/Role/Router/WithLogger.pm b/lib/Log/Contextual/Role/Router/WithLogger.pm
new file mode 100644 (file)
index 0000000..7268937
--- /dev/null
@@ -0,0 +1,8 @@
+package Log::Contextual::Role::Router::WithLogger;
+
+use Moo::Role;
+
+requires 'with_logger';
+
+1;
+
index 09235ac..08e4577 100644 (file)
@@ -3,7 +3,9 @@ package Log::Contextual::Router;
 use Moo;
 use Scalar::Util 'blessed';
 
-with 'Log::Contextual::Role::Router';
+with 'Log::Contextual::Role::Router',
+     'Log::Contextual::Role::Router::SetLogger',
+     'Log::Contextual::Role::Router::WithLogger';
 
 eval {
    require Log::Log4perl;