Jim Thomason > Basset > Basset::NotificationCenter

Download:
Basset-1.04.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 1.02   Source  

NAME ^

Basset::NotificationCenter - used to notify other objects of interesting things

AUTHOR ^

Jim Thomason, jim@jimandkoka.com

DESCRIPTION ^

This concept is stolen lock stock and barrel from Objective-C (Apple's cocoa frameworks, specifically). Basically, the notification center is a high level object that sits off to the side. Objects can register with it to pay attention to interesting things that other objects do, and they can then act upon the interesting things.

For example. Let's keep track of all times we see a weasel. First, we'll set up a logger (see Basset::Logger) to write to a log file.

 my $logger = Basset::Logger->new(
        'handle' => '/tmp/weasels.log'
 );

Now, we register it as an observer

 Basset::NotificationCenter->addObserver(
        'observer'              => $logger,
        'notification'  => 'weasels',
        'object'                => 'all',
        'method'                => 'log'
 );

And we're done! Now we've registered our observer that will watch for "weasels" notifications posted by all objects, and when it seems them, it will call its log method.

So when a notification is posted:

 Basset::NotificationCenter->postNotification(
        'object'                => $henhouse,
        'notification'  => 'weasels',
        'args'                  => ["Weasels in the hen house!"]
 );

That will look for all observers registered to watch for 'weasels' notifications (our logger, in this case) and call their methods. Again, for our example, internally the notification center fires off:

 $logger->log("Weasels in the hen house!");

Which logs the line of data to our /tmp/weasels.log file.

You will need to put a types entry into your conf file for

 notificationcenter=Basset::NotificationCenter

(or whatever center you're using)

ATTRIBUTES ^

$test->ok(1, "testing is implied");

observation

This is useful for debugging purposes. Set the notification center as an observer, and the observation will contain the most recently postend notification.

 Basset::NotificationCenter->addObserver(
        'observer'              => Basset::NotificationCenter->new,
        'notification'  => "YOUR NOTIFICATION,
        'object'                => 'all',
        'method'                => 'observation'
 );

my $center = __PACKAGE__->new(); $test->ok($center, 'got default center');

$test->is($center->addObserver('method' => 'observation', 'observer' => $center, 'notification' => 'foo'), 1, 'Added center-as-observer for foo from all');

$test->is($center->postNotification('notification' => 'foo', 'object' => $center), 1, "Center posted foo notification"); my $note = $center->observation; $test->is($note->{'object'}, $center, 'Notification object is center'); $test->is($note->{'notification'}, 'foo', 'Notification is foo');

$test->is($center->removeAllObservers(), 1, 'cleaned up and removed observers');

loggers

loggers should be specified in the conf file. Similar spec to the 'types' entry.

 loggers %= error=/tmp/error.log
 loggers %= warnings=/tmp/warnings.log
 loggers %= info=/tmp/info.log

etc. Those conf file entries create loggers watching all objects for error, warnings, and info notifications, and the log files to which they write.

my $o = __PACKAGE__->new(); $test->ok($o, 'got object'); $test->is(ref($o->observers), 'HASH', 'observers is hashref');

METHODS ^

new

Basset::NotificationCenter is a singleton. Calling the constructor will return the single instance that can exist. All other methods may be called as either an object or a class method.

$test->is(__PACKAGE__->center, __PACKAGE__->new, 'new returns center singleton');

postNotification

 Basset::NotificationCenter->postNotification(
        'notification'  => 'weasels',
        'object'                => $henhouse,
        'args'                  => ["Weasels in the hen house!"]
 );

postNotification (say it with me, now) posts a notification. It expects 2-3 arguments.

 object                 - required. The object posting the notification. May be a class.
 notification   - required. A string containing the notification being posted.
 args                   - optional. Additional arguments in an arrayref to pass through to any observers.

The observer receives a hashref containing the args passed into postNotification.

package Basset::Test::Testing::Basset::NotificationCenter::postNotification; our @ISA = qw(Basset::Object);

Basset::Test::Testing::Basset::NotificationCenter::postNotification->add_attr('observation');

package __PACKAGE__;

my $center = Basset::NotificationCenter->new;

my $o = Basset::Test::Testing::Basset::NotificationCenter::postNotification->new(); $test->ok($o, "got object");

$test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'foo'), 1, 'Added observer for foo from all'); $test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'bar', 'object' => $o), 1, 'Added observer for bar from self');

my $args = [qw(a b c)]; $test->ok($args, "Got args");

$test->is($center->postNotification('notification' => 'foo', 'object' => $center, 'args' => $args), 1, "Center posted foo notification"); my $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center'); $test->is($note->{'notification'}, 'foo', 'Notification is foo'); $test->is($note->{'args'}, $args, 'args are correct');

$test->is(__PACKAGE__->postNotification('notification' => 'foo', 'object' => $center, 'args' => $args), 1, "Center posted foo notification through package"); $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center'); $test->is($note->{'notification'}, 'foo', 'Notification is foo'); $test->is($note->{'args'}, $args, 'args are correct');

$test->is($center->postNotification('notification' => 'bar', 'object' => $center, 'args' => $args), 1, "Center posted bar notification"); $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center (object ignores bar from center)'); $test->is($note->{'notification'}, 'foo', 'Notification is foo (object ignores bar from center)'); $test->is($note->{'args'}, $args, 'args are correct (object ignores bar from center)');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o, 'args' => $args), 1, "o posted bar notification"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is($note->{'args'}, $args, 'args are correct');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o), 1, "o posted bar notification w/no args"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is(scalar(@{$note->{'args'}}), 0, 'args are empty arrayref');

$test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'cam', 'object' => $o), 1, 'Added observer for cam from self'); $test->is($center->postNotification('notification' => 'cam', 'object' => $o), 1, "o posted cam notification w/no args"); $test->is(__PACKAGE__->postNotification('notification' => 'cam', 'object' => $o), 1, "o posted cam notification w/no args via class");

$test->is($center->removeAllObservers(), 1, 'cleaned up and removed observers');

addObserver

 Basset::NotificationCenter->addObserver(
        'observer'              => $logger
        'notification'  => 'weasels',
        'object'                => 'all',
        'method'                => 'log'
 );

addObserver (say it with me, now) adds an observer. It expects 3-4 arguments.

 observer               - required. The object observing the notification. May be a class.
 notification   - required. A string containing the notification to watch for.
 method                 - required. The method to call when the notification is observed.
 object                 - optional. If specified, then the observer will only watch for notifications posted by that object (or class).
                                        otherwise, watches for all notifications of that type.

package Basset::Test::Testing::Basset::NotificationCenter::addObserver; our @ISA = qw(Basset::Object);

Basset::Test::Testing::Basset::NotificationCenter::addObserver->add_attr('observation');

package __PACKAGE__;

my $o = Basset::Test::Testing::Basset::NotificationCenter::addObserver->new(); $test->ok($o, "got object");

my $center = __PACKAGE__->new(); $test->ok($center, 'got default center');

$test->is(scalar($center->addObserver), undef, 'Could not add observer w/o method'); $test->is($center->errcode, 'BN-01', 'proper error code'); $test->is(scalar(__PACKAGE__->addObserver), undef, 'Could not add observer w/o method through package'); $test->is($center->errcode, 'BN-01', 'proper error code'); $test->is(scalar($center->addObserver('method' => 'observation')), undef, 'Could not add observer w/o observer'); $test->is($center->errcode, 'BN-02', 'proper error code'); $test->is(scalar($center->addObserver('method' => 'observation', 'observer' => $o)), undef, 'Could not add observer w/o notification'); $test->is($center->errcode, 'BN-03', 'proper error code'); $test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'foo'), 1, 'Added observer for foo from all'); $test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'bar', 'object' => $o), 1, 'Added observer for bar from self');

my $args = [qw(a b c)]; $test->ok($args, "Got args");

$test->is($center->postNotification('notification' => 'foo', 'object' => $center, 'args' => $args), 1, "Center posted foo notification"); my $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center'); $test->is($note->{'notification'}, 'foo', 'Notification is foo'); $test->is($note->{'args'}, $args, 'args are correct');

$test->is($center->postNotification('notification' => 'bar', 'object' => $center, 'args' => $args), 1, "Center posted bar notification"); $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center (object ignores bar from center)'); $test->is($note->{'notification'}, 'foo', 'Notification is foo (object ignores bar from center)'); $test->is($note->{'args'}, $args, 'args are correct (object ignores bar from center)');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o, 'args' => $args), 1, "o posted bar notification"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is($note->{'args'}, $args, 'args are correct');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o), 1, "o posted bar notification w/no args"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is(scalar(@{$note->{'args'}}), 0, 'args are empty arrayref');

$test->is($center->removeAllObservers(), 1, 'cleaned up and removed observers');

removeObserver

 Basset::NotificationCenter->removeObserver(
        'observer'              => $logger
        'notification'  => 'weasels',
 );

removeObserver (say it with me, now) removes an observer. It expects 2 arguments.

 observer               - required. The object observing the notification. May be a class.
 notification   - required. A string containing the notification to watch for.

Behave yourself and properly manage your memory. Remove observers when you're no longer using them. This is especially important in a mod_perl environment.

package Basset::Test::Testing::Basset::NotificationCenter::removeObserver; our @ISA = qw(Basset::Object);

Basset::Test::Testing::Basset::NotificationCenter::removeObserver->add_attr('observation');

package __PACKAGE__;

my $o = Basset::Test::Testing::Basset::NotificationCenter::removeObserver->new(); $test->ok($o, "got object");

my $center = __PACKAGE__->new(); $test->ok($center, 'got default center');

$test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'foo'), 1, 'Added observer for foo from all'); $test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'bar', 'object' => $o), 1, 'Added observer for bar from self');

$test->is(scalar($center->removeObserver), undef, 'Could not remove observer w/o observer'); $test->is($center->errcode, 'BN-05', 'proper error code'); $test->is(scalar(__PACKAGE__->removeObserver), undef, 'Could not remove observer w/o observer through package'); $test->is($center->errcode, 'BN-05', 'proper error code'); $test->is(scalar($center->removeObserver('observer' => $o)), undef, 'Could not remove observer w/o notification'); $test->is($center->errcode, 'BN-06', 'proper error code');

$test->is($center->removeObserver('observer' => $o, 'notification' => 'foo'), 1, 'removed foo notification'); $test->is(scalar($o->observation(undef)), undef, 'wiped out any previous notifications'); $test->is($center->postNotification('notification' => 'foo', 'object' => $o), 1, "o posted foo notification"); my $note = $o->observation; $test->is($note, undef, "No notification received");

$test->is($center->postNotification('notification' => 'bar', 'object' => $o), 1, "o posted bar notification w/no args"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is(scalar(@{$note->{'args'}}), 0, 'args are empty arrayref');

$test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'foo'), 1, 'Re-Added observer for foo from all');

$test->is($center->removeObserver('observer' => $o, 'notification' => 'bar'), 1, 'removed bar notification for all');

$test->is(scalar($o->observation(undef)), undef, 'wiped out any previous notifications');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o), 1, "o posted bar notification"); $note = $o->observation; $test->is($note->{'object'}, $o, 'Notification object is o'); $test->is($note->{'notification'}, 'bar', 'Notification is bar'); $test->is(scalar(@{$note->{'args'}}), 0, 'args are empty arrayref');

$test->is($center->removeObserver('observer' => $o, 'notification' => 'bar', 'object' => $o), 1, 'removed bar notification for $o');

$test->is(scalar($o->observation(undef)), undef, 'wiped out any previous notifications');

$test->is($center->postNotification('notification' => 'bar', 'object' => $o), 1, "o posted bar notification"); $note = $o->observation;

$test->is($note, undef, "No notification received");

$test->is($center->postNotification('notification' => 'foo', 'object' => $center), 1, "Center posted foo notification"); $note = $o->observation; $test->is($note->{'object'}, $center, 'Notification object is center'); $test->is($note->{'notification'}, 'foo', 'Notification is foo');

$test->is($center->removeAllObservers(), 1, 'cleaned up and removed observers');

removeAllObservers

Sometimes, though, it's easier to just nuke all the existing observers. The end of execution in a mod_perl process, for instance. You don't need to care what observers are still around or what they're doing. You just want them to go away. So you can remove them all.

 Basset::NotificationCenter->removeAllObservers();

package Basset::Test::Testing::Basset::NotificationCenter::removeAllObservers; our @ISA = qw(Basset::Object);

Basset::Test::Testing::Basset::NotificationCenter::removeAllObservers->add_attr('observation');

package __PACKAGE__;

my $o = Basset::Test::Testing::Basset::NotificationCenter::removeAllObservers->new(); $test->ok($o, "got object");

my $center = __PACKAGE__->new(); $test->ok($center, 'got default center');

$test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'foo'), 1, 'Added observer for foo from all'); $test->is($center->addObserver('method' => 'observation', 'observer' => $o, 'notification' => 'bar', 'object' => $o), 1, 'Added observer for bar from self');

my $observers = $center->observers;

$test->is(scalar(keys %{$center->observers}), 2, 'two observers'); $test->is($center->removeAllObservers, 1, 'removed all observers'); $test->is(scalar(keys %{$center->observers}), 0, 'no more observers');

$center->observers($observers);