);
has history => (
+ traits => ['Array'],
is => 'ro',
isa => 'ArrayRef[Int]',
default => sub { [] },
+ handles => { add_history => 'push' },
);
sub deposit {
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;
#
# 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.
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' );