From: Chip Salzenberg Date: Tue, 12 Jul 2011 20:24:04 +0000 (-0700) Subject: Role::Tiny::With try1 X-Git-Tag: release_0.009009~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a1164a0bb0cba509976456877051a14c2a9980d2;p=gitmo%2FMoo.git Role::Tiny::With try1 --- diff --git a/lib/Role/Tiny.pm b/lib/Role/Tiny.pm index 10ef3c3..429deaf 100644 --- a/lib/Role/Tiny.pm +++ b/lib/Role/Tiny.pm @@ -24,7 +24,7 @@ sub _load_module { sub import { my $target = caller; - my $me = $_[0]; + my $me = shift; strictures->import; return if $INFO{$target}; # already exported into this package # get symbol table reference @@ -230,6 +230,7 @@ sub does_role { } 1; +__END__ =head1 NAME @@ -251,10 +252,10 @@ else where package Some::Class; - require Role::Tiny; + use Role::Tiny::With; # bar gets imported, but not foo - Role::Tiny->apply_role_to_package('Some::Role', __PACKAGE__); + with 'Some::Role'; sub foo { ... } @@ -294,7 +295,7 @@ is to be much simpler, hence disallowing composition of multiple roles at once. Role::Tiny->apply_role_to_package('Some::Package', 'Some::Role'); -Composes role with package +Composes role with package. See also L. =head2 apply_roles_to_object diff --git a/lib/Role/Tiny/With.pm b/lib/Role/Tiny/With.pm new file mode 100644 index 0000000..e4bf82e --- /dev/null +++ b/lib/Role/Tiny/With.pm @@ -0,0 +1,46 @@ +package Role::Tiny; + +use strict; +use warnings FATAL => 'all'; + +use Exporter 'import'; +our @EXPORT = qw( with ); + +sub with { + my $target = caller; + Role::Tiny->apply_role_to_package($target, @_) +} + +1; +__END__ + +=head1 NAME + +Role::Tiny::With - Neat interface for consumers of Role::Tiny roles + +=head1 SYNOPSIS + + package Some::Class; + + use Role::Tiny::With; + + with 'Some::Role'; + + # The role is now mixed in + +=head1 DESCRIPTION + +C is a minimalist role composition tool. C +provides a C function to compose such roles. + +=head1 AUTHORS + +See L for authors. + +=head1 COPYRIGHT AND LICENSE + +See L for the copyright and license. + +=cut + + diff --git a/t/role-tiny-with.t b/t/role-tiny-with.t new file mode 100644 index 0000000..afca4e2 --- /dev/null +++ b/t/role-tiny-with.t @@ -0,0 +1,32 @@ +use strictures 1; +use Test::More; +use Test::Fatal; + +BEGIN { + package MyRole; + + use Role::Tiny; + + sub bar { 'role bar' } + + sub baz { 'role baz' } +} + +BEGIN { + package MyClass; + + use Role::Tiny::With; + + with 'MyRole'; + + sub foo { 'class foo' } + + sub baz { 'class baz' } + +} + +is(MyClass->foo, 'class foo', 'method from class no override'); +is(MyClass->bar, 'role bar', 'method from role'); +is(MyClass->baz, 'class baz', 'method from class'); + +done_testing;