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