package MyApp::Website;
use Moose;
- use MyApp::Meta::Attribute::Labeled;
has url => (
metaclass => 'Labeled',
sub dump {
my $self = shift;
- # iterate over all the attributes in $self
+ my $dump = '';
+
my %attributes = %{ $self->meta->get_attribute_map };
- while ( my ( $name, $attribute ) = each %attributes ) {
+ for my $name ( sort keys %attributes ) {
+ my $attribute = $attributes{$name};
- # print the label if available
if ( $attribute->isa('MyApp::Meta::Attribute::Labeled')
&& $attribute->has_label ) {
- print $attribute->label;
+ $dump .= $attribute->label;
}
-
- # otherwise print the name
else {
- print $name;
+ $dump .= $name;
}
- # print the attribute's value
my $reader = $attribute->get_read_method;
- print ": " . $self->$reader . "\n";
+ $dump .= ": " . $self->$reader . "\n";
}
+
+ return $dump;
}
package main;
+
my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
- $app->dump;
=head1 SUMMARY
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 nede to create attributes that use that
+attribute. Second, we need to create attributes that use that
attribute metaclass.
=head1 RECIPE REVIEW
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 metaclas.
+referring to our new metaclass.
That was the whole attribute metaclass.
label => "The site's URL",
);
-This looks like a normal attribute declaraion, except for two things,
+This looks like a normal attribute declaration, except for two things,
the C<metaclass> and C<label> parameters. The C<metaclass> parameter
tells Moose we want to use a custom metaclass for this (one)
attribute. The C<label> parameter will be stored in the meta-attribute
sub dump {
my $self = shift;
- # iterate over all the attributes in $self
+ my $dump = '';
+
my %attributes = %{ $self->meta->get_attribute_map };
- while ( my ( $name, $attribute ) = each %attributes ) {
+ for my $name ( sort keys %attributes ) {
+ my $attribute = $attributes{$name};
- # print the label if available
if ( $attribute->isa('MyApp::Meta::Attribute::Labeled')
&& $attribute->has_label ) {
- print $attribute->label;
+ $dump .= $attribute->label;
}
This is a bit of defensive code. We cannot depend on every
We also check that the attribute has a label using the predicate we
defined. We could instead make the label C<required>. If we have a
-label, we print it, otherwise we print the attribute name:
+label, we use it, otherwise we use the attribute name:
- # otherwise print the name
else {
- print $name;
+ $dump .= $name;
}
- # print the attribute's value
my $reader = $attribute->get_read_method;
- print ": " . $self->$reader . "\n";
+ $dump .= ": " . $self->$reader . "\n";
}
+
+ return $dump;
}
The C<get_read_method> is part of the L<Moose::Meta::Attribute>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
+=begin testing
+
+my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
+is(
+ $app->dump, q{name: Google
+The site's URL: http://google.com
+}, 'got the expected dump value'
+);
+
+=end testing
+
=cut