NAME
MooseX::Notification - An observer/notification for Moose
SYNOPSIS
{
package Counter;
use Moose;
use MooseX::Notification;
has count => ( is => 'rw', isa => 'Int', default => 0 );
sub inc {
my ($self) = @_;
$self->count( $self->count + 1 );
my $mn = MooseX::Notification->default;
$mn->notify( 'count', $self->count );
}
sub dec {
my ($self) = @_;
$self->count( $self->count - 1 );
my $mn = MooseX::Notification->default;
$mn->notify( { event => 'count', data => $self->count } );
}
no Moose;
package TrackCount;
use Moose;
has count => ( is => 'rw', isa => 'Int', default => 0 );
sub print {
my ($self) = @_;
print $self->count;
}
sub get_count {
my ( $self, $count ) = @_;
$self->count($count);
}
no Moose;
}
my $count = Counter->new;
my $mn = MooseX::Notification->default;
my $tc = TrackCount->new;
$mn->add(
{
observer => $tc,
event => 'count',
method => 'get_count',
}
);
for ( 1 .. 10 ) {
$count->inc;
}
for ( 1 .. 5 ) {
$count->dec;
}
$tc->print; # 5
or use IOC using Bread::Board
use Bread::Board;
my $c = container 'TestApp' => as {
service 'fname' => 'Larry';
service 'lname' => 'Wall';
service 'notification_center' => (
class => 'MooseX::Notification',
lifecycle => 'Singleton',
);
service 'person' => (
class => 'Person',
dependencies => {
notification => depends_on('notification_center'),
fname => depends_on('fname'),
lname => depends_on('lname'),
},
);
service 'upn' => (
class => 'UCPrintName',
dependencies => { notification => depends_on('notification_center') },
);
service 'pn' => (
class => 'PrintName',
dependencies => { notification => depends_on('notification_center'), },
);
};
my $pn = $c->fetch('pn')->get;
my $upn = $c->fetch('upn')->get;
my $person = $c->fetch('person')->get;
my $nc = $c->fetch('notification_center')->get;
$person->print_name;
$nc->remove( { observer => $upn } );
$person->print_name;
DESCRIPTION
An observer/notification based on the objective-c NSNotificationCenter Class
- new
-
The method creates a new instance of MooseX::Notification object
- default
-
This method creates a singleton of the MooseX::Notification object
- add
-
args keys: observer, event, method
observer: the object that will observer events
event: the name of the event that you are assigning the observer. Defaults to DEFAULT
method: the method you want called on the observer when the event is called. Defaults to update
- remove
-
args keys: observer, event
observer: the object that you want to remove
event: the name of the event that you are removing the observer. Defaults to DEFAULT
- notify
-
args keys : event, args
event: the event you want to trigger
args: data you want to pass into observers