6 use Test::More tests => 2;
11 ## attribute trait example
14 package MyApp::Meta::Attribute::Trait::Labeled;
20 predicate => 'has_label',
23 package Moose::Meta::Attribute::Custom::Trait::Labeled;
24 sub register_implementation { 'MyApp::Meta::Attribute::Trait::Labeled' }
26 package MyApp::Website;
30 traits => [qw/Labeled/],
33 label => "The site's URL",
46 # iterate over all the attributes in $self
47 my %attributes = %{ $self->meta->get_attribute_map };
48 foreach my $name (sort keys %attributes) {
50 my $attribute = $attributes{$name};
52 # print the label if available
53 if ($attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
54 && $attribute->has_label) {
55 $dump_value .= $attribute->label;
57 # otherwise print the name
62 # print the attribute's value
63 my $reader = $attribute->get_read_method;
64 $dump_value .= ": " . $self->$reader . "\n";
72 my $app = MyApp::Website->new(url => "http://google.com", name => "Google");
73 is($app->dump, q{name: Google
74 The site's URL: http://google.com
75 }, '... got the expected dump value');
77 # using the trait directly in a regular metaclass
79 package MyApp::Meta::Attribute::Labeled;
81 extends 'Moose::Meta::Attribute';
82 with 'MyApp::Meta::Attribute::Trait::Labeled';
84 package Moose::Meta::Attribute::Custom::Labeled;
85 sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
87 package MyApp::Website2;
91 metaclass => 'Labeled',
94 label => "The site's URL",
107 # iterate over all the attributes in $self
108 my %attributes = %{ $self->meta->get_attribute_map };
109 foreach my $name (sort keys %attributes) {
111 my $attribute = $attributes{$name};
113 # print the label if available
114 if ($attribute->isa('MyApp::Meta::Attribute::Labeled')
115 && $attribute->has_label) {
116 $dump_value .= $attribute->label;
118 # otherwise print the name
120 $dump_value .= $name;
123 # print the attribute's value
124 my $reader = $attribute->get_read_method;
125 $dump_value .= ": " . $self->$reader . "\n";
133 my $app2 = MyApp::Website2->new(url => "http://google.com", name => "Google");
134 is($app2->dump, q{name: Google
135 The site's URL: http://google.com
136 }, '... got the expected dump value');