X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FCookbook%2FRecipe11.pod;h=b2d1e4ce6446429250e6f99947d64c0ad85181c2;hb=759e4e8ff4171bfabf1cb3d3843f760425676fe0;hp=138819997ae39f27a3c9b5ec4e233cec443ff4f1;hpb=0cd28189b889c80e73927b8eec225dcafce43940;p=gitmo%2FMoose.git diff --git a/lib/Moose/Cookbook/Recipe11.pod b/lib/Moose/Cookbook/Recipe11.pod index 1388199..b2d1e4c 100644 --- a/lib/Moose/Cookbook/Recipe11.pod +++ b/lib/Moose/Cookbook/Recipe11.pod @@ -12,7 +12,7 @@ Moose::Cookbook::Recipe11 - The meta-attribute example extends 'Moose::Meta::Attribute'; has label => ( - is => 'ro', + is => 'rw', isa => 'Str', predicate => 'has_label', ); @@ -105,11 +105,12 @@ looks like the following: y => Moose::Meta::Attribute=HASH(0x18d1690), } -Here's one thing you can do now that you can interact with the attribute's -object directly: +Another way to get a handle on an attribute's object is +C<< $self->meta->get_attribute('name') >>. Here's one thing you can do now that +you can interact with the attribute's object directly: - print $point->meta->get_attribute_map->{x}->type_constraint; - => Int + print $point->meta->get_attribute('x')->type_constraint; + => Int (As an aside, it's not called C<< ->isa >> because C<< $obj->isa >> is already taken) @@ -138,29 +139,27 @@ somewhat ungloriously. extends 'Moose::Meta::Attribute'; You subclass metaclasses the same way you subclass regular classes. (Extra -credit: how in the actual hell can you use the MOP to extend itself?) Moving -on. +credit: how in the actual hell can you use the MOP to extend itself?) has label => ( - is => 'ro', + is => 'rw', isa => 'Str', predicate => 'has_label', ); -Now things get a little icky. We're adding a attribute to the attribute -metaclass. For clarity, I'm going to call this a meta-attribute. +Hey, this looks pretty reasonable! This is plain jane Moose code. Recipe 1 +fare. This is merely making a new attribute. An attribute that attributes have. +A meta-attribute. It may sound scary, but it really isn't! Reread +L if this really is terrifying. -This creates a new meta-attribute in the C -metaclass. The new meta-attribute's name is 'label'. The predicate just creates -a method that asks the question "Does this attribute have a value?" - -Of course, if you step a foot back, you can see that this is really just adding -an attribute to a class. Don't be alarmed! +The name is "label", it will have a regular accessor, and is a string. +C is a standard part of C. It just creates a method that asks +the question "Does this attribute have a value?" package Moose::Meta::Attribute::Custom::Labeled; sub register_implementation { 'MyApp::Meta::Attribute::Labeled' } -This registers the new metaclass with Moose. That way attributes can actually +This lets Moose discover our new metaclass. That way attributes can actually use it. More on what this is doing in a moment. Note that we're done defining the new metaclass! Only nine lines of code, and @@ -193,16 +192,23 @@ package, C. So Moose uses that metaclass for the attribute. It may seem a bit convoluted, but the alternative would be to use C<< metaclass => 'MyApp::Meta::Attribute::Labeled' >> on every attribute. As usual, Moose optimizes in favor of the end user, not the metaprogrammer. :) +We also could have just defined the metaclass in +C, but it's probably better to keep to +your own namespaces. Finally, we see that C is setting our new meta-attribute, C