From: Dave Rolsky Date: Sat, 1 Oct 2011 16:11:15 +0000 (-0500) Subject: Use a metaclass trait and don't do "also => Moose" X-Git-Tag: 2.0500~81 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose.git;a=commitdiff_plain;h=9e0a571102793bb9b703f30d40bce9f802a8e30e Use a metaclass trait and don't do "also => Moose" --- diff --git a/lib/Moose/Cookbook/Extending/Recipe4.pod b/lib/Moose/Cookbook/Extending/Recipe4.pod index 2ae476c..a716106 100644 --- a/lib/Moose/Cookbook/Extending/Recipe4.pod +++ b/lib/Moose/Cookbook/Extending/Recipe4.pod @@ -11,41 +11,34 @@ __END__ package MyApp::Mooseish; - use Moose (); use Moose::Exporter; Moose::Exporter->setup_import_methods( - with_meta => ['has_table'], - also => 'Moose', + with_meta => ['has_table'], + class_metaroles => { + class => ['MyApp::Meta::Class::Trait::HasTable'], + }, ); - sub init_meta { - shift; - return Moose->init_meta( @_, metaclass => 'MyApp::Meta::Class' ); - } - sub has_table { my $meta = shift; $meta->table(shift); } - package MyApp::Meta::Class; - use Moose; + package MyApp::Meta::Class::Trait::HasTable; + use Moose::Role; - extends 'Moose::Meta::Class'; - - has 'table' => ( is => 'rw' ); + has table => ( + is => 'rw', + isa => 'Str', + ); =head1 DESCRIPTION This recipe expands on the use of L we saw in -L. Instead of providing our own -object base class, we provide our own metaclass class, and we also -export a C sugar function. - -Given the above code, you can now replace all instances of C with C. Similarly, C is now -replaced with C. +L and the class metaclass trait we saw in +L. In this example we provide our own +metaclass trait, and we also export a C sugar function. The C parameter specifies a list of functions that should be wrapped before exporting. The wrapper simply ensures that the @@ -61,6 +54,9 @@ interface. Here's what it would look like in actual use: package MyApp::User; + use namespace::autoclean; + + use Moose; use MyApp::Mooseish; has_table 'User'; @@ -70,11 +66,6 @@ interface. Here's what it would look like in actual use: sub login { ... } - no MyApp::Mooseish; - -All of the normal Moose sugar (C, C, etc) is available -when you C. - =head1 CONCLUSION Providing sugar functions can make your extension look much more