From: Stevan Little Date: Thu, 12 Apr 2007 18:59:57 +0000 (+0000) Subject: adding some crap in X-Git-Tag: 0.18_01~90 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=85ec0d1f018fd6f21bf4e2095c546b7b1a4af07d;p=gitmo%2FMooseX-AttributeHelpers.git adding some crap in --- diff --git a/t/100_collection_with_roles.t b/t/100_collection_with_roles.t new file mode 100644 index 0000000..8964854 --- /dev/null +++ b/t/100_collection_with_roles.t @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +BEGIN { + use_ok('MooseX::AttributeHelpers'); +} + +=pod + +## convert this to a test ... +## code by Robert Boone + +package Subject; + +use Moose::Role; +use MooseX::AttributeHelpers; + +has observers => ( + metaclass => 'Collection::Array', + is => 'ro', + isa => 'ArrayRef', + auto_deref => 1, + default => sub { [] }, + provides => { 'push' => 'add_observer', } +); + +sub notify { + my ($self) = @_; + foreach my $observer ( $self->observers() ) { + $observer->update($self); + } +} + +############################################################################### + +package Observer; + +use Moose::Role; + +sub update { + die 'Forgot to implement' . "\n"; +} + +############################################################################### + +package Counter; + +use Moose; +use MooseX::AttributeHelpers; + +with 'Subject'; + +has count => ( + metaclass => 'Counter', + is => 'ro', + isa => 'Int', + default => 0, + provides => { + inc => 'inc_counter', + dec => 'dec_counter', + } +); + +after 'inc_counter','dec_counter' => sub { + my ($self) = @_; + $self->notify(); +}; + +############################################################################### + +package Display; + +use Moose; + +with 'Observer'; + +sub update { + my ( $self, $subject ) = @_; + print $subject->count() . "\n"; +} + +############################################################################### + +package main; + +my $count = Counter->new(); +$count->add_observer( Display->new() ); + +for ( 1 .. 5 ) { + $count->inc_counter(); +} + +for ( 1 .. 5 ) { + $count->dec_counter(); +} \ No newline at end of file