File Coverage

blib/lib/MooseX/Observer/Role/Observer.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package MooseX::Observer::Role::Observer;
2             {
3             $MooseX::Observer::Role::Observer::VERSION = '0.010';
4             }
5             # ABSTRACT: Tags a Class as being an Observer
6 1     1   2109 use Moose::Role;
  0            
  0            
7             requires 'update';
8             1;
9              
10              
11             __END__
12             =pod
13              
14             =head1 NAME
15              
16             MooseX::Observer::Role::Observer - Tags a Class as being an Observer
17              
18             =head1 VERSION
19              
20             version 0.010
21              
22             =head1 SYNOPSIS
23              
24             ############################################################################
25             package Counter;
26              
27             use Moose;
28              
29             has count => (
30             traits => ['Counter'],
31             is => 'rw',
32             isa => 'Int',
33             default => 0,
34             handles => {
35             inc_counter => 'inc',
36             dec_counter => 'dec',
37             },
38             );
39              
40             # apply the observable-role and
41             # provide methodnames, after which the observers are notified of changes
42             with 'MooseX::Observer::Role::Observable' => { notify_after => [qw~
43             count
44             inc_counter
45             dec_counter
46             reset_counter
47             ~] };
48              
49             sub reset_counter { shift->count(0) }
50              
51             sub _utility_method { ... }
52              
53             ############################################################################
54             package Display;
55              
56             use Moose;
57              
58             # apply the oberserver-role, tagging the class as observer and ...
59             with 'MooseX::Observer::Role::Observer';
60              
61             # ... require an update-method to be implemented
62             # this is called after the observed subject calls an observed method
63             sub update {
64             my ( $self, $subject, $args, $eventname ) = @_;
65             print $subject->count;
66             }
67              
68             ############################################################################
69             package main;
70              
71             my $counter = Counter->new();
72             # add an observer of type "Display" to our observable counter
73             $counter->add_observer( Display->new() );
74              
75             # increments the counter to 1, afterwards its observers are notified of changes
76             # Display is notified of a change, its update-method is called
77             $counter->inc_counter; # Display prints 1
78             $counter->dec_counter; # Display prints 0
79              
80             =head1 DESCRIPTION
81              
82             This is a simple role, that you have to apply to your oberservers. It simply
83             requires you to implement a method called update. This method is called
84             everytime the observed object changes.
85              
86             =head1 METHODS
87              
88             =head2 update($subject, $args, $eventname)
89              
90             This method has to be implemented by the class, that consumes this role. The
91             following arguments are passed.
92              
93             =head3 1. the subject
94              
95             The object being observed.
96              
97             =head3 2. an arrayref containg arguments
98              
99             The arguments of method, that was the reason for the observed class' change.
100              
101             =head3 3. an eventname
102              
103             The name of the method, that was the reason for the observed class' change.
104              
105             =cut
106              
107             =head1 INSTALLATION
108              
109             See perlmodinstall for information and options on installing Perl modules.
110              
111             =head1 SEE ALSO
112              
113             Please see those modules/websites for more information related to this module.
114              
115             =over 4
116              
117             =item *
118              
119             L<MooseX::Observer|MooseX::Observer>
120              
121             =back
122              
123             =head1 AUTHOR
124              
125             Thomas Müller <tmueller@cpan.org>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is copyright (c) 2011 by Thomas Müller.
130              
131             This is free software; you can redistribute it and/or modify it under
132             the same terms as the Perl 5 programming language system itself.
133              
134             =cut
135