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
index e1edc60..8c519e2 100644 (file)
@@ -2,17 +2,45 @@ package Person;
 
 use Moose;
 
-with 'Printable', 'HasAccount', 'OutputsXML';
-
 has title => (
     is        => 'rw',
     predicate => 'has_title',
     clearer   => 'clear_title',
 );
 
-has first_name => ( is => 'rw' );
+has first_name => (
+    is       => 'rw',
+    required => 1,
+);
+
+has last_name => (
+    is       => 'rw',
+    required => 1,
+);
+
+sub BUILDARGS {
+    my $class = shift;
+
+    if ( @_ == 1 && 'ARRAY' eq ref $_[0] ) {
+        return {
+            first_name => $_[0]->[0],
+            last_name  => $_[0]->[1],
+        };
+    }
+    else {
+        return $class->SUPER::BUILDARGS(@_);
+    }
+}
+
+our @CALL;
 
-has last_name  => ( is => 'rw' );
+before full_name => sub {
+    push @CALL, 'calling full_name';
+};
+
+after full_name => sub {
+    push @CALL, 'called full_name';
+};
 
 sub full_name {
     my $self = shift;
@@ -24,15 +52,16 @@ sub full_name {
     return $title;
 }
 
-sub as_string { $_[0]->full_name }
-
-sub as_xml {
+around full_name => sub {
+    my $orig = shift;
     my $self = shift;
 
-    return
-        ( map { "<$_>" . ( $self->$_ || q{} ) . "</$_>" } qw( first_name last_name title ) ),
-        inner();
-}
+    return $self->$orig unless $self->last_name eq 'Wall';
+
+    return q{*} . $self->$orig . q{*};
+};
+
+sub as_string { $_[0]->full_name }
 
 no Moose;