From: Dave Rolsky Date: Sat, 1 Oct 2011 15:39:07 +0000 (-0500) Subject: Removed Meta recipe2 (an attribute metaclass) X-Git-Tag: 2.0500~85 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fe66eda17ca7486c6e84ba5d669dca7f559256f3;p=gitmo%2FMoose.git Removed Meta recipe2 (an attribute metaclass) I merged all the useful bits from recipe 2 into recipe 3. If we're going to renumber I'll do that in a separate commit after I'm done revising all the recipes that need revision. --- diff --git a/lib/Moose/Cookbook/Meta/Recipe1.pod b/lib/Moose/Cookbook/Meta/Recipe1.pod index ea6341d..85040cb 100644 --- a/lib/Moose/Cookbook/Meta/Recipe1.pod +++ b/lib/Moose/Cookbook/Meta/Recipe1.pod @@ -38,9 +38,8 @@ create your own Moose variant. Let's say that you want to add additional properties to attributes. Specifically, we want to add a "label" property to each attribute, so we can write C<< -My::Class->meta()->get_attribute('size')->label() >>. The first two -recipes show two different ways to do this, one with a full -meta-attribute subclass, and the other with an attribute trait. +My::Class->meta()->get_attribute('size')->label() >>. The first +recipe shows how to do this using an attribute trait. You might also want to add additional properties to your metaclass. For example, if you were writing an ORM based on Moose, you diff --git a/lib/Moose/Cookbook/Meta/Recipe2.pod b/lib/Moose/Cookbook/Meta/Recipe2.pod deleted file mode 100644 index e7b0cec..0000000 --- a/lib/Moose/Cookbook/Meta/Recipe2.pod +++ /dev/null @@ -1,277 +0,0 @@ -package Moose::Cookbook::Meta::Recipe2; - -# ABSTRACT: A meta-attribute, attributes with labels - -__END__ - - -=pod - -=head1 SYNOPSIS - - package MyApp::Meta::Attribute::Labeled; - use Moose; - extends 'Moose::Meta::Attribute'; - - has label => ( - is => 'rw', - isa => 'Str', - predicate => 'has_label', - ); - - package Moose::Meta::Attribute::Custom::Labeled; - sub register_implementation {'MyApp::Meta::Attribute::Labeled'} - - package MyApp::Website; - use Moose; - - has url => ( - metaclass => 'Labeled', - is => 'rw', - isa => 'Str', - label => "The site's URL", - ); - - has name => ( - is => 'rw', - isa => 'Str', - ); - - sub dump { - my $self = shift; - - my $meta = $self->meta; - - my $dump = ''; - - for my $attribute ( map { $meta->get_attribute($_) } - sort $meta->get_attribute_list ) { - - if ( $attribute->isa('MyApp::Meta::Attribute::Labeled') - && $attribute->has_label ) { - $dump .= $attribute->label; - } - else { - $dump .= $attribute->name; - } - - my $reader = $attribute->get_read_method; - $dump .= ": " . $self->$reader . "\n"; - } - - return $dump; - } - - package main; - - my $app = MyApp::Website->new( url => "http://google.com", name => "Google" ); - -=head1 SUMMARY - -In this recipe, we begin to delve into the wonder of meta-programming. -Some readers may scoff and claim that this is the arena of only the -most twisted Moose developers. Absolutely not! Any sufficiently -twisted developer can benefit greatly from going more meta. - -Our goal is to allow each attribute to have a human-readable "label" -attached to it. Such labels would be used when showing data to an end -user. In this recipe we label the C attribute with "The site's -URL" and create a simple method showing how to use that label. - -The proper, modern way to extend attributes (using a role instead of a -subclass) is described in L, but that recipe -assumes you've read and at least tried to understand this one. - -=head1 META-ATTRIBUTE OBJECTS - -All the attributes of a Moose-based object are actually objects -themselves. These objects have methods and attributes. Let's look at -a concrete example. - - has 'x' => ( isa => 'Int', is => 'ro' ); - has 'y' => ( isa => 'Int', is => 'rw' ); - -Internally, the metaclass for C has two -L. There are several methods for getting -meta-attributes out of a metaclass, one of which is -C. This method is called on the metaclass object. - -The C method returns a list of attribute names. You can -then use C to get the L object itself. - -Once you have this meta-attribute object, you can call methods on it like this: - - print $point->meta->get_attribute('x')->type_constraint; - => Int - -To add a label to our attributes there are two steps. First, we need a -new attribute metaclass that can store a label for an -attribute. Second, we need to create attributes that use that -attribute metaclass. - -=head1 RECIPE REVIEW - -We start by creating a new attribute metaclass. - - package MyApp::Meta::Attribute::Labeled; - use Moose; - extends 'Moose::Meta::Attribute'; - -We can subclass a Moose metaclass in the same way that we subclass -anything else. - - has label => ( - is => 'rw', - isa => 'Str', - predicate => 'has_label', - ); - -Again, this is standard Moose code. - -Then we need to register our metaclass with Moose: - - package Moose::Meta::Attribute::Custom::Labeled; - sub register_implementation { 'MyApp::Meta::Attribute::Labeled' } - -This is a bit of magic that lets us use a short name, "Labeled", when -referring to our new metaclass. - -That was the whole attribute metaclass. - -Now we start using it. - - package MyApp::Website; - use Moose; - use MyApp::Meta::Attribute::Labeled; - -We have to load the metaclass to use it, just like any Perl class. - -Finally, we use it for an attribute: - - has url => ( - metaclass => 'Labeled', - is => 'rw', - isa => 'Str', - label => "The site's URL", - ); - -This looks like a normal attribute declaration, except for two things, -the C and C