From: Dave Rolsky Date: Sun, 19 Feb 2012 23:49:37 +0000 (-0600) Subject: Add CAVEAT to Meta::Table_MetaclassTrait about issues when all code is in one file. X-Git-Tag: 2.0500~53 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1910e831217014839c1e3b9077a1071059b08e3b;p=gitmo%2FMoose.git Add CAVEAT to Meta::Table_MetaclassTrait about issues when all code is in one file. Also add tests for the code. --- diff --git a/lib/Moose/Cookbook/Meta/Table_MetaclassTrait.pod b/lib/Moose/Cookbook/Meta/Table_MetaclassTrait.pod index 30a8c3b..0437357 100644 --- a/lib/Moose/Cookbook/Meta/Table_MetaclassTrait.pod +++ b/lib/Moose/Cookbook/Meta/Table_MetaclassTrait.pod @@ -7,8 +7,23 @@ __END__ =pod +=begin testing-SETUP + +BEGIN { + package MyApp::Meta::Class::Trait::HasTable; + use Moose::Role; + + has table => ( + is => 'rw', + isa => 'Str', + ); +} + +=end testing-SETUP + =head1 SYNOPSIS + # in lib/MyApp/Meta/Class/Trait/HasTable.pm package MyApp::Meta::Class::Trait::HasTable; use Moose::Role; @@ -20,6 +35,7 @@ __END__ package Moose::Meta::Class::Custom::Trait::HasTable; sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' } + # in lib/MyApp/User.pm package MyApp::User; use Moose -traits => 'HasTable'; @@ -44,7 +60,6 @@ make a module like C itself, with sugar like C. =head2 Using this Metaclass Trait in Practice - Accessing this new C attribute is quite simple. Given a class named C, we could simply write the following: @@ -62,9 +77,24 @@ In theory, this is not entirely correct, since the metaclass might be getting its C
method from a I trait. In practice, you are unlikely to encounter this sort of problem. +=head1 RECIPE CAVEAT + +This recipe doesn't work when you paste it all into a single file. This is +because the C<< use Moose -traits => 'HasTable'; >> line ends up being +executed before the C
attribute is defined. + +When the two packages are separate files, this just works. + =head1 SEE ALSO L - Labels implemented via attribute traits =pod + +=begin testing + +can_ok( MyApp::User->meta, 'table' ); +is( MyApp::User->meta->table, 'User', 'My::User table is User' ); + +=end testing