From: Dave Rolsky Date: Sat, 6 Feb 2010 03:23:22 +0000 (-0600) Subject: Fix up trigger instructions and example implementation. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ed84c5c65e43867d252f46d13d91df162e6abf6d;p=gitmo%2Fmoose-presentations.git Fix up trigger instructions and example implementation. Also ask that people use a native delegation for recording history. --- diff --git a/moose-class/exercises/answers/06-advanced-attributes/BankAccount.pm b/moose-class/exercises/answers/06-advanced-attributes/BankAccount.pm index 90c78e2..039fb52 100644 --- a/moose-class/exercises/answers/06-advanced-attributes/BankAccount.pm +++ b/moose-class/exercises/answers/06-advanced-attributes/BankAccount.pm @@ -17,9 +17,11 @@ has owner => ( ); has history => ( + traits => ['Array'], is => 'ro', isa => 'ArrayRef[Int]', default => sub { [] }, + handles => { add_history => 'push' }, ); sub deposit { @@ -42,9 +44,12 @@ sub withdraw { sub _record_balance { my $self = shift; shift; + + return unless @_; + my $old_value = shift; - push @{ $self->history }, $old_value; + $self->add_history($old_value); } no Moose; diff --git a/moose-class/exercises/t/06-advanced-attributes.t b/moose-class/exercises/t/06-advanced-attributes.t index 23ec690..65f11c9 100644 --- a/moose-class/exercises/t/06-advanced-attributes.t +++ b/moose-class/exercises/t/06-advanced-attributes.t @@ -10,14 +10,14 @@ # # Copy the deposit and withdraw methods from the HasAccount role. # -# Finally, add a read-only history attribute. This will be an ArrayRef -# of Int's. This should default to an empty array reference. +# Finally, add a read-only history attribute. This will be an ArrayRef of +# Int's. This should default to an empty array reference. Use Native +# delegation to create a method to push values onto this attribute. # # Use a trigger to record the _old value_ of the balance each time it -# changes. -# -# Use a BUILD method in BankAccount to record the original balance in -# the history. +# changes. This means your trigger should not do anything if it is not passed +# an old value (this case happens when the attribute is set for the first +# time). # # We will now delete the HasAccount role entirely. Instead, add an # "account" attribute to Person directly. diff --git a/moose-class/exercises/t/lib/MooseClass/Tests.pm b/moose-class/exercises/t/lib/MooseClass/Tests.pm index 4169bd8..0015192 100644 --- a/moose-class/exercises/t/lib/MooseClass/Tests.pm +++ b/moose-class/exercises/t/lib/MooseClass/Tests.pm @@ -232,6 +232,14 @@ sub tests06 { ok( $ba_meta->has_attribute('balance'), 'BankAccount class has a balance attribute' ); + my $history_attr = $ba_meta->get_attribute('history'); + + ok( + $history_attr->meta() + ->does_role('Moose::Meta::Attribute::Native::Trait::Array'), + 'BankAccount history attribute uses native delegation to an array ref' + ); + ok( $ba_meta->get_attribute('balance')->has_trigger, 'BankAccount balance attribute has a trigger' );