adding some crap in
[gitmo/MooseX-AttributeHelpers.git] / t / 100_collection_with_roles.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 BEGIN {
7     use_ok('MooseX::AttributeHelpers');
8 }
9
10 =pod
11
12 ## convert this to a test ... 
13 ## code by Robert Boone
14
15 package Subject;
16
17 use Moose::Role;
18 use MooseX::AttributeHelpers;
19
20 has observers => (
21     metaclass  => 'Collection::Array',
22     is         => 'ro',
23     isa        => 'ArrayRef',
24     auto_deref => 1,
25     default    => sub { [] },
26     provides   => { 'push' => 'add_observer', }
27 );
28
29 sub notify {
30     my ($self) = @_;
31     foreach my $observer ( $self->observers() ) {
32         $observer->update($self);
33     }
34 }
35
36 ###############################################################################
37
38 package Observer;
39
40 use Moose::Role;
41
42 sub update {
43     die 'Forgot to implement' . "\n";
44 }
45
46 ###############################################################################
47
48 package Counter;
49
50 use Moose;
51 use MooseX::AttributeHelpers;
52
53 with 'Subject';
54
55 has count => (
56     metaclass => 'Counter',
57     is        => 'ro',
58     isa       => 'Int',
59     default   => 0,
60     provides  => {
61         inc => 'inc_counter',
62         dec => 'dec_counter',
63     }
64 );
65
66 after 'inc_counter','dec_counter' => sub {
67     my ($self) = @_;
68     $self->notify();
69 };
70
71 ###############################################################################
72
73 package Display;
74
75 use Moose;
76
77 with 'Observer';
78
79 sub update {
80     my ( $self, $subject ) = @_;
81     print $subject->count() . "\n";
82 }
83
84 ###############################################################################
85
86 package main;
87
88 my $count = Counter->new();
89 $count->add_observer( Display->new() );
90
91 for ( 1 .. 5 ) {
92     $count->inc_counter();
93 }
94
95 for ( 1 .. 5 ) {
96     $count->dec_counter();
97 }