From: Dave Rolsky Date: Sat, 1 Oct 2011 15:47:11 +0000 (-0500) Subject: Remove meta recipe 4 and merge its relevant bits into meta recipe 5 X-Git-Tag: 2.0500~84 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6d84892de9081d1bf41a1c3d6511a201366e1efa;hp=fe66eda17ca7486c6e84ba5d669dca7f559256f3;p=gitmo%2FMoose.git Remove meta recipe 4 and merge its relevant bits into meta recipe 5 --- diff --git a/lib/Moose/Cookbook/Meta/Recipe4.pod b/lib/Moose/Cookbook/Meta/Recipe4.pod deleted file mode 100644 index 3e5b3e3..0000000 --- a/lib/Moose/Cookbook/Meta/Recipe4.pod +++ /dev/null @@ -1,67 +0,0 @@ -package Moose::Cookbook::Meta::Recipe4; - -# ABSTRACT: Adding a "table" attribute to the metaclass - -__END__ - - -=pod - -=head1 SYNOPSIS - - package MyApp::Meta::Class; - use Moose; - extends 'Moose::Meta::Class'; - - has table => ( - is => 'rw', - isa => 'Str', - ); - -=head1 DESCRIPTION - -In this recipe, we'll create a new metaclass which has a "table" -attribute. This metaclass is for classes associated with a DBMS table, -as one might do for an ORM. - -In this example, the table name is just a string, but in a real ORM -the table might be an object describing the table. - -=head1 THE METACLASS - -This really is as simple as the recipe L shows. The trick -is getting your classes to use this metaclass, and providing some sort -of sugar for declaring the table. This is covered in -L, which shows how to make a -module like C itself, with sugar like C. - -=head2 Using this Metaclass in Practice - -Accessing this new C attribute is quite simple. Given a class -named C, we could simply write the following: - - my $table = MyApp::User->meta->table; - -As long as C has arranged to use C as -its metaclass, this method call just works. If we want to be more -careful, we can check the metaclass's class: - - $table = MyApp::User->meta->table - if MyApp::User->meta->isa('MyApp::Meta::Class'); - -=head1 CONCLUSION - -Creating custom metaclass is trivial. Using it is a little harder, and -is covered in other recipes. We will also talk about applying traits -to a class metaclass, which is a more flexible and cooperative -implementation. - -=head1 SEE ALSO - -L - The "table" attribute implemented -as a metaclass trait - -L - Acting like Moose.pm and -providing sugar Moose-style - -=pod diff --git a/lib/Moose/Cookbook/Meta/Recipe5.pod b/lib/Moose/Cookbook/Meta/Recipe5.pod index 5075343..21b3fd4 100644 --- a/lib/Moose/Cookbook/Meta/Recipe5.pod +++ b/lib/Moose/Cookbook/Meta/Recipe5.pod @@ -1,6 +1,6 @@ package Moose::Cookbook::Meta::Recipe5; -# ABSTRACT: The "table" attribute as a metaclass trait +# ABSTRACT: Adding a "table" attribute as a metaclass trait __END__ @@ -27,47 +27,44 @@ __END__ =head1 DESCRIPTION -This recipe takes the metaclass table attribute from -L and implements it as a metaclass -trait. Traits are just roles, as we saw in -L. +In this recipe, we'll create a class metaclass trait which has a "table" +attribute. This trait is for classes associated with a DBMS table, as one +might do for an ORM. -The advantage of using traits is that it's easy to combine multiple -traits, whereas combining multiple metaclass subclasses requires -creating yet another subclass. With traits, Moose takes care of -applying them to your metaclass. +In this example, the table name is just a string, but in a real ORM +the table might be an object describing the table. -=head2 Using this Metaclass Trait in Practice +=head1 THE METACLASS TRAIT -Once this trait has been applied to a metaclass, it looks exactly like -the example we saw in L: +This really is as simple as the recipe L shows. The trick is +getting your classes to use this metaclass, and providing some sort of sugar +for declaring the table. This is covered in +L, which shows how to make a module like +C itself, with sugar like C. - my $table = MyApp::User->meta->table; +=head2 Using this Metaclass Trait in Practice - # the safe version - $table = MyApp::User->meta->table - if MyApp::User->meta->meta->can('does') - and MyApp::User->meta->meta->does('MyApp::Meta::Class'); -The safe version is a little complicated. We have to check that the -metaclass object's metaclass has a C method, in which case we -can ask if the the metaclass does a given role. +Accessing this new C
attribute is quite simple. Given a class +named C, we could simply write the following: -It's simpler to just write: + my $table = MyApp::User->meta->table; + +As long as C has arranged to apply the +C to its metaclass, this method call just +works. If we want to be more careful, we can check that the class metaclass +object has a C
method: $table = MyApp::User->meta->table if MyApp::User->meta->can('table'); -In theory, this is a little less correct, since the metaclass might be -getting its C
method from a I role. In practice, you -are unlikely to encounter this sort of problem. +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 SEE ALSO L - Labels implemented via attribute traits -L - Adding a "table" attribute to the -metaclass - =pod