carried BUILDARGS in Person.pm from exercise 1 forward through the later answer class...
[gitmo/moose-presentations.git] / moose-class / exercises / answers / 06-advanced-attributes / Person.pm
1 package Person;
2
3 use BankAccount;
4 use Moose;
5
6 with 'Printable';
7
8 has account => (
9     is      => 'rw',
10     isa     => 'BankAccount',
11     default => sub { BankAccount->new },
12     handles => [ 'deposit', 'withdraw' ],
13 );
14
15 has title => (
16     is        => 'rw',
17     predicate => 'has_title',
18     clearer   => 'clear_title',
19 );
20
21 has first_name => ( is => 'rw' );
22
23 has last_name  => ( is => 'rw' );
24
25 sub BUILD {
26     my $self = shift;
27
28     $self->account->owner($self);
29 }
30
31 sub BUILDARGS {
32     my $class = shift;
33
34     if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
35         return {
36             first_name => $_[0]->[0],
37             last_name  => $_[0]->[1],
38         };
39     }
40     else {
41         return $class->SUPER::BUILDARGS(@_);
42     }
43 }
44
45 sub full_name {
46     my $self = shift;
47
48     my $title = join q{ }, $self->first_name, $self->last_name;
49     $title .= q[ (] . $self->title . q[)]
50         if $self->has_title;
51
52     return $title;
53 }
54
55 sub as_string { $_[0]->full_name }
56
57 no Moose;
58
59 __PACKAGE__->meta->make_immutable;
60
61 1;