package MyApp::Website;
use Moose;
- use MyApp::Meta::Attribute::Trait::Labeled;
has url => (
traits => [qw/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->does('MyApp::Meta::Attribute::Trait::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 BUT FIRST
attribute. Passing a C<label> parameter works just as it did with the
metaclass example.
- # print the label if available
- if ( $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
- && $attribute->has_label ) {
- print $attribute->label;
- }
+ if ( $attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
+ && $attribute->has_label ) {
+ $dump .= $attribute->label;
+ }
In the metaclass example, we used C<< $attribute->isa >>. With a role,
we instead ask if the meta-attribute object C<does> the required
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
-=cut
+=begin testing
+my $app2
+ = MyApp::Website->new( url => "http://google.com", name => "Google" );
+is(
+ $app2->dump, q{name: Google
+The site's URL: http://google.com
+}, '... got the expected dump value'
+);
+
+=end testing
+
+=cut