Robert Boone

NAME

Notification::Center - An observer/notification for perl

SYNOPSIS

    {

        package Counter;

        use Moose;
        use Notification::Center;

        has count => ( is => 'rw', isa => 'Int', default => 0 );

        sub inc {
            my ($self) = @_;
            $self->count( $self->count + 1 );
            my $nc = Notification::Center->default;
            $nc->notify( { event => 'count', args => $self->count } );
        }

        sub dec {
            my ($self) = @_;
            $self->count( $self->count - 1 );
            my $nc = Notification::Center->default;
            $nc->notify( { event => 'count', args => $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 = Notification::Center->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     => 'Notification::Center',
            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 center based on the objective-c NSNotificationCenter Class

new

The method creates a new instance of Notification::Center object

default

This method creates a singleton of the Notification::Center 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

GIT Repository

http://github.com/rlb3/notification-center/tree/master