Basset
view release on metacpan or search on metacpan
lib/Basset/Object.pm view on Meta::CPAN
=item wipe_errors
Wipes out the current error message and error code.
=cut
=pod
=begin btest(wipe_errors)
$test->is(scalar __PACKAGE__->error("test error", "error code"), undef, "Class set error and errcode");
$test->is(__PACKAGE__->error(), "test error", "Class accesses error");
$test->is(__PACKAGE__->errcode(), "error code", "Class accesses errcode");
$test->ok(scalar __PACKAGE__->wipe_errors(), "Class wiped errors");
$test->is(scalar __PACKAGE__->error(), undef, "Class error wiped out");
$test->is(scalar __PACKAGE__->errcode(), undef, "Class errcode wiped out");
my $confClass = __PACKAGE__->pkg_for_type('conf');
$test->ok($confClass, "Got conf");
my $cfg = $confClass->conf;
$test->ok($cfg, "Got configuration");
$test->ok($cfg->{"Basset::Object"}->{'exceptions'} = 1, "enables exceptions");
eval {
__PACKAGE__->error("test exception", "test exception code");
};
$test->ok($@, "Caught exception");
$test->like($@, qr/test exception code/, "Exception matches");
$test->like(__PACKAGE__->last_exception, qr/test exception/, "Exception is present");
$test->ok(scalar __PACKAGE__->wipe_errors(), "Class wiped errors");
$test->is(__PACKAGE__->last_exception, undef, "last exception wiped out");
$test->is($cfg->{"Basset::Object"}->{'exceptions'} = 0, 0,"disables exceptions");
=end btest(wipe_errors)
=cut
sub wipe_errors {
my $self = shift;
$self->error(undef);
$self->errcode(undef);
$self->last_exception(undef) if $self->can('exceptions');
return 1;
};
=pod
=item notify
Used for non-fatal messages, usually an error message that shouldn't cause things to abort. Expects at least one argument,
the notification being posted. Additional arguments will be passed through to any handlers.
sub lockThing {
my $self = shift;
my $thing = shift;
if ($thing->locked) {
$self->notify("info", "Cannot lock - thing is already locked");
} else {
$thing->lock();
};
return 1;
}
In this example, we have a method called "lockThing" that locks a thing (whatever that means). But it only locks the thing
if it is not already locked. If it is locked, it sends an informational message that the thing is already locked. But that's not
fatal - we still end up with a locked thing, so we're happy no matter what. No need to kick back an error.
notify is a wrapper around the notification center.
$obj->notify('foo') == Basset::NotificationCenter->postNotification('object' => $obj, 'notification' => 'foo');
=cut
=pod
=begin btest(notify)
my $test1notes = undef;
my $test2notes = undef;
sub test1notifier {
my $self = shift;
my $note = shift;
$test1notes = $note->{'args'}->[0];
};
sub test2notifier {
my $self = shift;
my $note = shift;
$test2notes = $note->{'args'}->[0];
};
my $center = __PACKAGE__->pkg_for_type('notificationcenter');
$test->ok($center, "Got notification center class");
$test->ok(
scalar
$center->addObserver(
'observer' => '__PACKAGE__',
'notification' => 'test1',
'object' => 'all',
'method' => 'test1notifier'
), "Added observer for test1 notifications"
);
$test->ok(
scalar
$center->addObserver(
'observer' => '__PACKAGE__',
'notification' => 'test2',
'object' => 'all',
'method' => 'test2notifier'
), "Added observer for test2 notifications"
);
my $o = __PACKAGE__->new();
$test->ok($o, "Object created");
$test->ok(scalar __PACKAGE__->notify('test1', "Test 1 note 1"), "Class posted notification");
$test->is($test1notes, "Test 1 note 1", "Received note");
$test->is($test2notes, undef, "No note for test 2");
$test->ok(scalar __PACKAGE__->notify('test2', "Test 2 note 2"), "Class posted notification");
$test->is($test2notes, "Test 2 note 2", "Received note");
$test->is($test1notes, "Test 1 note 1", "Test 1 note unchanged");
( run in 0.904 second using v1.01-cache-2.11-cpan-bbe5e583499 )