Class-Observable

 view release on metacpan or  search on metacpan

lib/Class/Observable.pm  view on Meta::CPAN

     if ( $@ ) {
         My::Exception->throw( "Error saving: $@" );
     }
     $self->notify_observers( 'edit', old_values => \%old_values );
  }
 
  # Define an observer
 
  package My::Observer;
 
  sub update {
     my ( $class, $object, $action ) = @_;
     unless ( $action ) {
         warn "Cannot operation on [", $object->id, "] without action";
         return;
     }
     $class->_on_save( $object )   if ( $action eq 'save' );
     $class->_on_update( $object ) if ( $action eq 'update' );
  }
 
  # Register the observer class with all instances of the observable

lib/Class/Observable.pm  view on Meta::CPAN

      my ( $self ) = @_;
      $self->_wet_toothbrush();
  }
 
  sub wash_face { return }
 
  # Create a class-based observer
 
  package My::ParentRules;
 
  sub update {
      my ( $item, $action ) = @_;
      if ( $action eq 'prepare_for_bed' ) {
          $item->brush_teeth;
          $item->wash_face;
      }
  }
 
  My::Parent->add_observer( __PACKAGE__ );
 
  $parent->prepare_for_bed # brush, floss, gargle, and wash face

lib/Class/Observable.pm  view on Meta::CPAN

     my ( $item, $action, $params ) = @_;
     return unless ( $action eq 'update' );
     # ...
 }
 $observable->add_observer( \&respond );

B<Class observer>:

 package My::ObserverC;
 
 sub update {
     my ( $class, $item, $action, $params ) = @_;
     return unless ( $action eq 'update' );
     # ...
 }

B<Object observer>:

 package My::ObserverO;
 
 sub new {
     my ( $class, $type ) = @_;
     return bless ( { type => $type }, $class );
 }
 
 sub update {
     my ( $self, $item, $action, $params ) = @_;
     return unless ( $action eq $self->{type} );
     # ...
 }

=head2 Observable Objects and DESTROY

This class has a C<DESTROY> method which B<must> run
when an instance of an observable class goes out of scope
in order to clean up the observers added to that instance.

t/lib/DeeJay.pm  view on Meta::CPAN

    #print "Let's get this party started!\n";
    $self->{current_song} = 0;
    $self->{playlist}[0]->play;
}

sub end_party {
    my ( $self ) = @_;
    #print "Party's over, time to go home\n";
}

sub update {
    my ( $self, $song, $action ) = @_;
    #print "Caught update [$action] from [$song->{band}]\n";
    $self->{update}++;
    return unless ( $action eq 'stop_play' );
    $self->{update_stop}++;
    $self->{current_song}++;
    if ( $self->{current_song} == $self->{num_songs} ) {
        return $self->end_party;
    }
    $self->{playlist}[ $self->{current_song} ]->play;

t/lib/DeeJay.pm  view on Meta::CPAN


# This DJ only responds to his/her own songs

sub new {
    my ( $class, $my_name ) = @_;
    return bless( { name        => $my_name,
                    update      => 0,
                    update_self => 0 }, $class );
}

sub update {
    my ( $self, $song ) = @_;
    $self->{update}++;
    #print "I am [$self->{name}] song is [$song->{band}]\n";
    $self->{update_self}++ if ( $song->{band} eq $self->{name} );
}

sub num_updates      { return $_[0]->{update} }
sub num_updates_self { return $_[0]->{update_self} }

package DeeJay::Helper;

sub new { return bless( {}, $_[0] ) }

sub update {
    my ( $self, $song ) = @_;
    $self->{update}++;
}

sub num_updates { return $_[0]->{update} }

1;



( run in 0.249 second using v1.01-cache-2.11-cpan-95122f20152 )