Redo method modifiers exercise to use before/after/around instead of augment/inner
[gitmo/moose-presentations.git] / moose-class / exercises / answers / 04-method-modifiers / Person.pm
1 package Person;
2
3 use Moose;
4
5 has title => (
6     is        => 'rw',
7     predicate => 'has_title',
8     clearer   => 'clear_title',
9 );
10
11 has first_name => (
12     is       => 'rw',
13     required => 1,
14 );
15
16 has last_name => (
17     is       => 'rw',
18     required => 1,
19 );
20
21 sub BUILDARGS {
22     my $class = shift;
23
24     if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
25         return {
26             first_name => $_[0]->[0],
27             last_name  => $_[0]->[1],
28         };
29     }
30     else {
31         return $class->SUPER::BUILDARGS(@_);
32     }
33 }
34
35 our @CALL;
36
37 before full_name => sub {
38     push @CALL, 'calling full_name';
39 };
40
41 after full_name => sub {
42     push @CALL, 'called full_name';
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 around full_name => sub {
56     my $orig = shift;
57     my $self = shift;
58
59     return $self->$orig unless $self->last_name eq 'Wall';
60
61     return q{*} . $self->$orig . q{*};
62 };
63
64 sub as_string { $_[0]->full_name }
65
66 no Moose;
67
68 __PACKAGE__->meta->make_immutable;
69
70 1;