Commit | Line | Data |
85ec0d1f |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
88aaf2bd |
6 | use Test::More no_plan => 1; |
7 | |
85ec0d1f |
8 | BEGIN { |
9 | use_ok('MooseX::AttributeHelpers'); |
10 | } |
11 | |
12 | =pod |
13 | |
14 | ## convert this to a test ... |
15 | ## code by Robert Boone |
16 | |
17 | package Subject; |
18 | |
19 | use Moose::Role; |
20 | use MooseX::AttributeHelpers; |
21 | |
22 | has 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 | |
31 | sub notify { |
32 | my ($self) = @_; |
33 | foreach my $observer ( $self->observers() ) { |
34 | $observer->update($self); |
35 | } |
36 | } |
37 | |
38 | ############################################################################### |
39 | |
40 | package Observer; |
41 | |
42 | use Moose::Role; |
43 | |
44 | sub update { |
45 | die 'Forgot to implement' . "\n"; |
46 | } |
47 | |
48 | ############################################################################### |
49 | |
50 | package Counter; |
51 | |
52 | use Moose; |
53 | use MooseX::AttributeHelpers; |
54 | |
55 | with 'Subject'; |
56 | |
57 | has 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 | |
68 | after 'inc_counter','dec_counter' => sub { |
69 | my ($self) = @_; |
70 | $self->notify(); |
71 | }; |
72 | |
73 | ############################################################################### |
74 | |
75 | package Display; |
76 | |
77 | use Moose; |
78 | |
79 | with 'Observer'; |
80 | |
81 | sub update { |
82 | my ( $self, $subject ) = @_; |
83 | print $subject->count() . "\n"; |
84 | } |
85 | |
86 | ############################################################################### |
87 | |
88 | package main; |
89 | |
90 | my $count = Counter->new(); |
91 | $count->add_observer( Display->new() ); |
92 | |
93 | for ( 1 .. 5 ) { |
94 | $count->inc_counter(); |
95 | } |
96 | |
97 | for ( 1 .. 5 ) { |
98 | $count->dec_counter(); |
99 | } |