merge trunk to pluggable errors
[gitmo/Moose.git] / t / 000_recipes / meta / 002_meta_attribute.t
CommitLineData
1edfdf1c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e606ae5f 6use Test::More tests => 1;
1edfdf1c 7use Test::Exception;
8
e606ae5f 9
1edfdf1c 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 };
1a28289d 49 foreach my $name (sort keys %attributes) {
50
51 my $attribute = $attributes{$name};
1edfdf1c 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
73my $app = MyApp::Website->new(url => "http://google.com", name => "Google");
1a28289d 74is($app->dump, q{name: Google
75The site's URL: http://google.com
1edfdf1c 76}, '... got the expected dump value');
77
78