From: Dave Rolsky Date: Tue, 6 Jan 2009 05:38:50 +0000 (+0000) Subject: tidy code in pod for consistency X-Git-Tag: 0.65~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6a7e399986337f67a5ae7d59c4a38e9c5013b830;p=gitmo%2FMoose.git tidy code in pod for consistency --- diff --git a/lib/Moose/Cookbook/Extending/Recipe1.pod b/lib/Moose/Cookbook/Extending/Recipe1.pod index e94fc01..d79d9d7 100644 --- a/lib/Moose/Cookbook/Extending/Recipe1.pod +++ b/lib/Moose/Cookbook/Extending/Recipe1.pod @@ -139,10 +139,10 @@ metaclass and attribute metaclass traits: use Moose -traits => [ 'Big', 'Blue' ]; - has 'animal' => - ( traits => [ 'Big', 'Blue' ], - ... - ); + has 'animal' => ( + traits => [ 'Big', 'Blue' ], + ... + ); If your extension applies to any other metaclass, or the object base class, you cannot use the trait mechanism. @@ -185,7 +185,7 @@ subclasses: Moose::Exporter->setup_import_methods( also => 'Moose' ); sub init_meta { - shift; # just your package name + shift; # just your package name my %options = @_; return Moose->init_meta( @@ -217,13 +217,13 @@ extension can easily use it with other role-based extensions. use MooseX::Embiggen::Role::Meta::Class; use MooseX::Embiggen::Role::Meta::Attribute; - use MooseX::Embiggen::Role::Meta::Method::Constructor + use MooseX::Embiggen::Role::Meta::Method::Constructor; use MooseX::Embiggen::Role::Object; Moose::Exporter->setup_import_methods( also => 'Moose' ); sub init_meta { - shift; # just your package name + shift; # just your package name my %options = @_; Moose->init_meta(%options); diff --git a/lib/Moose/Cookbook/Extending/Recipe2.pod b/lib/Moose/Cookbook/Extending/Recipe2.pod index 0bdeae5..4d3326b 100644 --- a/lib/Moose/Cookbook/Extending/Recipe2.pod +++ b/lib/Moose/Cookbook/Extending/Recipe2.pod @@ -28,14 +28,13 @@ Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class ); } - package MooseX::Debugging::Role::Object; after 'BUILD' => sub { my $self = shift; warn "Made a new " . ref $self . " object\n"; - } + }; =head1 DESCRIPTION diff --git a/lib/Moose/Cookbook/Meta/Recipe2.pod b/lib/Moose/Cookbook/Meta/Recipe2.pod index d5f3754..791d90a 100644 --- a/lib/Moose/Cookbook/Meta/Recipe2.pod +++ b/lib/Moose/Cookbook/Meta/Recipe2.pod @@ -7,61 +7,62 @@ Moose::Cookbook::Meta::Recipe2 - A meta-attribute, attributes with labels =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; - use MyApp::Meta::Attribute::Labeled; - - has url => ( - metaclass => 'Labeled', - is => 'rw', - isa => 'Str', - label => "The site's URL", - ); - - has name => ( - is => 'rw', - isa => 'Str', - ); - - sub dump { - my $self = shift; - - # iterate over all the attributes in $self - my %attributes = %{ $self->meta->get_attribute_map }; - while (my ($name, $attribute) = each %attributes) { - - # print the label if available - if ($attribute->isa('MyApp::Meta::Attribute::Labeled') - && $attribute->has_label) { - print $attribute->label; - } - # otherwise print the name - else { - print $name; - } - - # print the attribute's value - my $reader = $attribute->get_read_method; - print ": " . $self->$reader . "\n"; - } - } - - package main; - my $app = MyApp::Website->new(url => "http://google.com", name => "Google"); - $app->dump; + 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; + use MyApp::Meta::Attribute::Labeled; + + has url => ( + metaclass => 'Labeled', + is => 'rw', + isa => 'Str', + label => "The site's URL", + ); + + has name => ( + is => 'rw', + isa => 'Str', + ); + + sub dump { + my $self = shift; + + # iterate over all the attributes in $self + my %attributes = %{ $self->meta->get_attribute_map }; + while ( my ( $name, $attribute ) = each %attributes ) { + + # print the label if available + if ( $attribute->isa('MyApp::Meta::Attribute::Labeled') + && $attribute->has_label ) { + print $attribute->label; + } + + # otherwise print the name + else { + print $name; + } + + # print the attribute's value + my $reader = $attribute->get_read_method; + print ": " . $self->$reader . "\n"; + } + } + + package main; + my $app = MyApp::Website->new( url => "http://google.com", name => "Google" ); + $app->dump; =head1 SUMMARY @@ -81,8 +82,8 @@ All the attributes of a Moose-based object are actually objects themselves. These objects have methods and (surprisingly) attributes. Let's look at a concrete example. - has 'x' => (isa => 'Int', is => 'ro'); - has 'y' => (isa => 'Int', is => 'rw'); + has 'x' => ( isa => 'Int', is => 'ro' ); + has 'y' => ( isa => 'Int', is => 'rw' ); Ahh, the veritable x and y of the Point example. Internally, every Point has an x object and a y object. They have methods (such as "get_value") and attributes @@ -94,23 +95,23 @@ and forget that there's a lot of machinery lying in such methods. So you have a C<$point> object, which has C and C methods. How can you actually access the objects behind these attributes? Here's one way: - $point->meta->get_attribute_map() + $point->meta->get_attribute_map() C returns a hash reference that maps attribute names to their objects. In our case, C might return something that looks like the following: - { - x => Moose::Meta::Attribute=HASH(0x196c23c), - y => Moose::Meta::Attribute=HASH(0x18d1690), - } + { + x => Moose::Meta::Attribute=HASH(0x196c23c), + y => Moose::Meta::Attribute=HASH(0x18d1690), + } 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('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) @@ -134,18 +135,18 @@ Let's start dissecting the recipe's code. We get the ball rolling by creating a new attribute metaclass. It starts off somewhat ungloriously. - package MyApp::Meta::Attribute::Labeled; - use Moose; - extends 'Moose::Meta::Attribute'; + package MyApp::Meta::Attribute::Labeled; + use Moose; + 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?) - has label => ( - is => 'rw', - isa => 'Str', - predicate => 'has_label', - ); + has label => ( + is => 'rw', + isa => 'Str', + predicate => 'has_label', + ); 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. @@ -156,8 +157,8 @@ 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' } + package Moose::Meta::Attribute::Custom::Labeled; + sub register_implementation { 'MyApp::Meta::Attribute::Labeled' } This lets Moose discover our new metaclass. That way attributes can actually use it. More on what this is doing in a moment. @@ -165,19 +166,19 @@ 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 not particularly difficult lines, either. Now to start using the metaclass. - package MyApp::Website; - use Moose; - use MyApp::Meta::Attribute::Labeled; + package MyApp::Website; + use Moose; + use MyApp::Meta::Attribute::Labeled; Nothing new here. We do have to actually load our metaclass to be able to use it. - has url => ( - metaclass => 'Labeled', - is => 'rw', - isa => 'Str', - label => "The site's URL", - ); + has url => ( + metaclass => 'Labeled', + is => 'rw', + isa => 'Str', + label => "The site's URL", + ); Ah ha! Now we're using the metaclass. We're adding a new attribute, C, to C. C lets you set the metaclass of the attribute. @@ -199,35 +200,35 @@ your own namespaces. Finally, we see that C is setting our new meta-attribute, C