Message::Stack - Deal with a "stack" of messages
version 0.20
my $stack = Message::Stack->new;
$stack->add(Message::Stack::Message->new(
msgid => 'something_happened',
level => 'error',
scope => 'login_formm',
subject => 'username',
text => 'Something happened!'
));
# Or... for those that want to type less
$stack->add({
msgid => 'something_else_happened',
level => 'error',
scope => 'login_form',
subject => 'password',
text => 'Something else happened!'
});
...
my $errors = $stack->for_level($error);
# Or
my $login_form_errors = $stack->for_scope('login_form');
$login_form_errors->for_subject('username');
print "Username has ".$login_form_errors->count." errors.\n";
Message::Stack provides a mechanism for storing messages until they can be consumed. A stack is used to retain order of occurrence. Each message may have a id, level, scope, subject and text. Consult the documentation for Message::Stack::Message for an explanation of these attributes.
This is not a logging mechanism. The original use was to store various errors
or messages that occur during processing for later display in a web
application. The messages are added via add
.
msgid used to be id. It was renamed to be a bit more description. All the
methods that existed for id still exist and the id attribute is now aliased
to msgid. In other words if you create an object using id
then the msgid
methods and the id
methods will work, and vice versa.
This module uses MooseX::Storage::Deferred to facilitate easy serialization. Consult the documentation for MooseX::Storage::Deferred options, but the gist is:
my $json = $stack->freeze({ format => 'JSON' }); ... my $stack = Message::Stack->thaw($json, { format => 'JSON' });
Returns the full arrayref of messages for this stack.
Returns the number of messages in the stack.
Returns the first message (if there is one, else undef)
Get the message at the supplied index.
Returns true if there are messages in the stack, else false
Returns the last message (if there is one, else undef)
Clear all messages, resetting this stack.
Adds the supplied message to the stack. $message
may be either a
Message::Stack::Message object or a hashref with similar keys.
Returns a new Message::Stack containing only the message objects with the supplied msgid. If there are no messages for that level then the stack returned will have no messages.
Returns a new Message::Stack containing only the message objects with the supplied level. If there are no messages for that level then the stack returned will have no messages.
Returns a new Message::Stack containing only the message objects with the supplied scope. If there are no messages for that scope then the stack returned will have no messages.
Returns a new Message::Stack containing only the message objects with the supplied subject. If there are no messages for that subject then the stack returned will have no messages.
Returns true if there are messages with the supplied msgid.
Returns true if there are messages with the supplied level.
Returns true if there are messages with the supplied scope.
Returns true if there are messages with the supplied subject.
Returns a Message::Stack containing messages that return true when passed to the coderef argument.
$stack->search( sub { $_[0]->id eq 'someid' } )
Clears the stack of all messages of scope $scope.
Clears the stack of all messages of level $level.
Clears the stack of all messages of msgid $msgid.
Clears the stack of all messages of subject $subject.
Jay Shirley
Stevan Little
Justin Hunter
Jon Wright
Mike Eldridge
Andrew Nelson
Cory G Watson [email protected]
This software is copyright (c) 2011 by Cory G Watson.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.