6 use Test::More tests => 3;
13 ## attribute trait example
16 package MyApp::Meta::Attribute::Trait::Labeled;
22 predicate => 'has_label',
25 package Moose::Meta::Attribute::Custom::Trait::Labeled;
26 sub register_implementation { 'MyApp::Meta::Attribute::Trait::Labeled' }
28 package MyApp::Website;
32 traits => [qw/Labeled/],
35 label => "The site's URL",
48 # iterate over all the attributes in $self
49 my %attributes = %{ $self->meta->get_attribute_map };
50 foreach my $name (sort keys %attributes) {
52 my $attribute = $attributes{$name};
54 # print the label if available
55 if ($attribute->does('MyApp::Meta::Attribute::Trait::Labeled')
56 && $attribute->has_label) {
57 $dump_value .= $attribute->label;
59 # otherwise print the name
64 # print the attribute's value
65 my $reader = $attribute->get_read_method;
66 $dump_value .= ": " . $self->$reader . "\n";
74 my $app = MyApp::Website->new(url => "http://google.com", name => "Google");
75 is($app->dump, q{name: Google
76 The site's URL: http://google.com
77 }, '... got the expected dump value');
79 # using the trait directly in a regular metaclass
81 package MyApp::Meta::Attribute::Labeled;
83 extends 'Moose::Meta::Attribute';
84 with 'MyApp::Meta::Attribute::Trait::Labeled';
86 package Moose::Meta::Attribute::Custom::Labeled;
87 sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
89 package MyApp::Website2;
93 metaclass => 'Labeled',
96 label => "The site's URL",
109 # iterate over all the attributes in $self
110 my %attributes = %{ $self->meta->get_attribute_map };
111 foreach my $name (sort keys %attributes) {
113 my $attribute = $attributes{$name};
115 # print the label if available
116 if ($attribute->isa('MyApp::Meta::Attribute::Labeled')
117 && $attribute->has_label) {
118 $dump_value .= $attribute->label;
120 # otherwise print the name
122 $dump_value .= $name;
125 # print the attribute's value
126 my $reader = $attribute->get_read_method;
127 $dump_value .= ": " . $self->$reader . "\n";
135 my $app2 = MyApp::Website2->new(url => "http://google.com", name => "Google");
136 is($app2->dump, q{name: Google
137 The site's URL: http://google.com
138 }, '... got the expected dump value');