Moose now warns when you try to load it from the main package. Added a
[gitmo/Moose.git] / t / 000_recipes / meta / 002_meta_attribute.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 1;
7 use Test::Exception;
8
9
10
11 ## meta-attribute example
12 {
13
14     package MyApp::Meta::Attribute::Labeled;
15     use Moose;
16     extends 'Moose::Meta::Attribute';
17
18     has label => (
19         is  => 'rw',
20         isa => 'Str',
21         predicate => 'has_label',
22     );
23
24     package Moose::Meta::Attribute::Custom::Labeled;
25     sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
26
27     package MyApp::Website;
28     use Moose;
29
30     has url => (
31         metaclass => 'Labeled',
32         isa => 'Str',
33         is => 'rw',
34         label => "The site's URL",
35     );
36
37     has name => (
38         is => 'rw',
39         isa => 'Str',
40     );
41
42     sub dump {
43         my $self = shift;
44
45         my $dump_value = '';
46         
47         # iterate over all the attributes in $self
48         my %attributes = %{ $self->meta->get_attribute_map };
49         foreach my $name (sort keys %attributes) {
50     
51             my $attribute = $attributes{$name};
52             
53             # print the label if available
54             if ($attribute->isa('MyApp::Meta::Attribute::Labeled')
55                 && $attribute->has_label) {
56                     $dump_value .= $attribute->label;
57             }
58             # otherwise print the name
59             else {
60                 $dump_value .= $name;
61             }
62
63             # print the attribute's value
64             my $reader = $attribute->get_read_method;
65             $dump_value .= ": " . $self->$reader . "\n";
66         }
67         
68         return $dump_value;
69     }
70
71 }
72
73 my $app = MyApp::Website->new(url => "http://google.com", name => "Google");
74 is($app->dump, q{name: Google
75 The site's URL: http://google.com
76 }, '... got the expected dump value');
77
78