Permalink
Browse files

support Class and Role in Moose

  • Loading branch information...
1 parent 89b9889 commit fd2ec86a06cff9464655a74a2ce69227b44d4acd @shelling committed Jan 10, 2013
Showing with 57 additions and 4 deletions.
  1. +14 −3 lib/Namespace/Dispatch.pm
  2. +1 −1 t/01_simple.t
  3. +30 −0 t/02_role.t
  4. +4 −0 t/lib/Foo/Class.pm
  5. +4 −0 t/lib/Foo/ClassWithRole.pm
  6. +4 −0 t/lib/Foo/Role.pm
View
@@ -5,11 +5,22 @@ use 5.010;
use UNIVERSAL::filename;
sub import {
+ my $pkg = shift;
my $caller = caller;
-
- for (qw(has_leaf dispatch leaves)) {
- *{"$caller::$_"} = *{$_};
+ my $meta = ref($caller->meta) if $caller->can("meta");
+
+ if ( $meta && $meta =~ m/(Class$|Role$)/ ) {
+ eval qq{
+ package $pkg;
+ use Moose::Role;
+ };
+ Moose::Util::apply_all_roles($caller, $pkg, {-excludes => "import"});
+ } else {
+ for (qw(has_leaf dispatch leaves)) {
+ *{$caller . "::" . $_} = *{$_};
+ }
}
+
}
sub has_leaf {
View
@@ -10,7 +10,7 @@ use Foo;
is_deeply (
Foo->leaves,
- [qw(add del help modify)],
+ [qw(add class classwithrole del help modify role)],
"Foo has two submodules Add and Del",
);
View
@@ -0,0 +1,30 @@
+use Modern::Perl;
+use Test::More;
+use Try::Tiny;
+
+use lib qw(lib t/lib);
+use Foo::Class;
+use Foo::ClassWithRole;
+
+is (
+ ref(Namespace::Dispatch->meta),
+ "Moose::Meta::Role",
+ "Namespace::Dispatch is a Moose::Role",
+);
+
+ok (
+ Foo::Class->does("Namespace::Dispatch"),
+ "Foo::Class does Namespace::Dispatch",
+);
+
+ok (
+ Foo::Role->meta->does_role("Namespace::Dispatch"),
+ "Foo::Role does Namespace::Dispatch",
+);
+
+ok (
+ Foo::ClassWithRole->does("Namespace::Dispatch"),
+ "Foo::ClassWithRole does Namespace::Dispatch",
+);
+
+done_testing;
View
@@ -0,0 +1,4 @@
+package Foo::Class;
+use Moose;
+use Namespace::Dispatch;
+1;
@@ -0,0 +1,4 @@
+package Foo::ClassWithRole;
+use Moose;
+with qw(Foo::Role);
+1;
View
@@ -0,0 +1,4 @@
+package Foo::Role;
+use Moose::Role;
+use Namespace::Dispatch;
+1;

0 comments on commit fd2ec86

Please sign in to comment.