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
CommitLineData
66b226e5 1package Person;
2
3use BankAccount;
4use Moose;
5
8883f849 6with 'Printable';
66b226e5 7
8has account => (
9 is => 'rw',
10 isa => 'BankAccount',
11 default => sub { BankAccount->new },
12 handles => [ 'deposit', 'withdraw' ],
13);
14
15has title => (
16 is => 'rw',
17 predicate => 'has_title',
18 clearer => 'clear_title',
19);
20
21has first_name => ( is => 'rw' );
22
23has last_name => ( is => 'rw' );
24
25sub BUILD {
26 my $self = shift;
27
28 $self->account->owner($self);
29}
30
1d405ed6 31sub 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
66b226e5 45sub 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
55sub as_string { $_[0]->full_name }
56
66b226e5 57no Moose;
58
59__PACKAGE__->meta->make_immutable;
60
611;