File Coverage

lib/Log/Shiras/Report/MetaMessage.pm
Criterion Covered Total %
statement 49 49 100.0
branch 15 20 75.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 75 80 93.7


line stmt bran cond sub pod time code
1             package Log::Shiras::Report::MetaMessage;
2             our $AUTHORITY = 'cpan:JANDREW';
3 1     1   364 use version; our $VERSION = version->declare("v0.48.0");
  1         1  
  1         4  
4 1     1   58 use strict;
  1         1  
  1         14  
5 1     1   2 use warnings;
  1         1  
  1         17  
6 1     1   12 use 5.010;
  1         2  
7 1     1   2 use utf8;
  1         1  
  1         4  
8             #~ use lib '../../../';
9             #~ use Log::Shiras::Unhide qw( :InternalReporTMetaMessagE );
10             ###InternalReporTMetaMessagE warn "You uncovered internal logging statements for Log::Shiras::Report::MetaMessage-$VERSION" if !$ENV{hide_warn};
11             ###InternalReporTMetaMessagE use Log::Shiras::Switchboard;
12             ###InternalReporTMetaMessagE my $switchboard = Log::Shiras::Switchboard->instance;
13 1     1   20 use Moose::Role;
  1         1  
  1         5  
14             requires '_my_test_for_around_add_line';
15 1     1   3058 use MooseX::Types::Moose qw( ArrayRef HashRef CodeRef );
  1         2  
  1         6  
16 1     1   3114 use Carp 'confess';
  1         1  
  1         331  
17              
18             #########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
19              
20             has pre_sub =>(
21             isa => CodeRef,
22             clearer => 'clear_pre_sub',
23             predicate => 'has_pre_sub',
24             reader => 'get_pre_sub',
25             writer => 'set_pre_sub',
26             );
27            
28             has hashpend =>(
29             isa => HashRef,
30             traits =>['Hash'],
31             clearer => 'clear_hashpend',
32             predicate => 'has_hashpend',
33             reader => 'get_all_hashpend',
34             handles =>{
35             add_to_hashpend => 'set',
36             remove_from_hashpend => 'delete',
37             },
38             );
39              
40             has prepend =>(
41             isa => ArrayRef,
42             traits =>['Array'],
43             clearer => 'clear_prepend',
44             predicate => 'has_prepend',
45             reader => 'get_all_prepend',
46             handles =>{
47             add_to_prepend => 'push',
48             },
49             );
50            
51             has postpend =>(
52             isa => ArrayRef,
53             traits =>['Array'],
54             clearer => 'clear_postpend',
55             predicate => 'has_postpend',
56             reader => 'get_all_postpend',
57             handles =>{
58             add_to_postpend => 'push',
59             },
60             );
61              
62             has post_sub =>(
63             isa => CodeRef,
64             clearer => 'clear_post_sub',
65             predicate => 'has_post_sub',
66             reader => 'get_post_sub',
67             writer => 'set_post_sub',
68             );
69              
70             #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
71              
72             sub manage_message{
73              
74 4     4 1 5 my ( $self, $message_ref ) = @_;
75             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 2,
76             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
77             ###InternalReporTMetaMessagE message =>[ 'Updating the message ref:', $message_ref ], } );
78            
79             # Handle pre_sub
80 4 100       76 if( $self->has_pre_sub ){
81 1         19 my $subref = $self->get_pre_sub;
82             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
83             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
84             ###InternalReporTMetaMessagE message =>[ 'Handling the pre_sub now' ], } );
85 1         4 $subref->( $message_ref );
86             }
87             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
88             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
89             ###InternalReporTMetaMessagE message =>[ 'Update message:', $message_ref->{message} ], } );
90            
91             # Handle hashpend
92 4 100       90 if( $self->has_hashpend ){
93 2         38 my $hashpend_ref = $self->get_all_hashpend;
94             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
95             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
96             ###InternalReporTMetaMessagE message =>[ 'Handling the hashpend values now:', $hashpend_ref ], } );
97 2 50       6 confess "The first element of the value (array ref) for the message key was not a hash ref" if !is_HashRef( $message_ref->{message}->[0] );
98 2         253 for my $element ( keys %$hashpend_ref ){
99             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 0,
100             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
101             ###InternalReporTMetaMessagE message =>[ "Handling the hashpend key -$element- with value: $hashpend_ref->{$element}" ], } );
102             $message_ref->{message}->[0]->{$element} =
103             is_CodeRef( $hashpend_ref->{$element} ) ? $hashpend_ref->{$element}->( $message_ref ) :
104             exists $message_ref->{$hashpend_ref->{$element}} ? $message_ref->{$hashpend_ref->{$element}} :
105 2 0       6 $hashpend_ref->{$element} ;
    50          
106             }
107             }
108             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
109             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
110             ###InternalReporTMetaMessagE message =>[ 'Update message:', $message_ref->{message} ], } );
111            
112             # Handle prepend
113 4 100       318 if( $self->has_prepend ){
114 2         35 my $prepend_ref = $self->get_all_prepend;
115             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
116             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
117             ###InternalReporTMetaMessagE message =>[ 'Handling the prepend values now:', $prepend_ref ], } );
118 2         4 for my $element ( reverse @$prepend_ref ){
119             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 0,
120             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
121             ###InternalReporTMetaMessagE message =>[ "Handling the prepend value: $element" ], } );
122 4         11 unshift @{$message_ref->{message}}, (
123 4 50       4 exists $message_ref->{$element} ? $message_ref->{$element} :
124             $element );
125             }
126             }
127             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
128             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
129             ###InternalReporTMetaMessagE message =>[ 'Update message:', $message_ref->{message} ], } );
130            
131             # Handle postpend
132 4 100       73 if( $self->has_postpend ){
133 2         38 my $postpend_ref = $self->get_all_postpend;
134             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
135             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
136             ###InternalReporTMetaMessagE message =>[ 'Handling the postpend values now:', $postpend_ref ], } );
137 2         4 for my $element ( @$postpend_ref ){
138             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 0,
139             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
140             ###InternalReporTMetaMessagE message =>[ "Handling the postepend value: $element" ], } );
141 4         12 push @{$message_ref->{message}}, (
142 4 100       2 exists $message_ref->{$element} ? $message_ref->{$element} :
143             $element );
144             }
145             }
146            
147             # Handle post_sub
148 4 100       77 if( $self->has_post_sub ){
149 1         44 my $subref = $self->get_post_sub;
150             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
151             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
152             ###InternalReporTMetaMessagE message =>[ 'Handling the post_sub now' ], } );
153 1         4 $subref->( $message_ref );
154             }
155             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 1,
156             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
157             ###InternalReporTMetaMessagE message =>[ 'Update message:', $message_ref->{message} ], } );
158            
159             ###InternalReporTMetaMessagE $switchboard->master_talk( { report => 'log_file', level => 3,
160             ###InternalReporTMetaMessagE name_space => 'Log::Shiras::Report::MetaMessage::manage_message',
161             ###InternalReporTMetaMessagE message =>[ "Updated full message:", $message_ref ], } );
162 4         20 return $message_ref;
163             }
164              
165             #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
166              
167              
168              
169             #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
170              
171              
172              
173             #########1 Phinish 3#########4#########5#########6#########7#########8#########9
174              
175 1     1   4 no Moose::Role;
  1         1  
  1         4  
176              
177             1;
178             # The preceding line will help the module return a true value
179              
180             #########1 main pod docs 3#########4#########5#########6#########7#########8#########9
181              
182             __END__
183              
184             =head1 NAME
185              
186             Log::Shiras::Report::MetaMessage - Add data to messages for reports
187              
188             =head1 SYNOPSIS
189              
190             use MooseX::ShortCut::BuildInstance qw( build_class );
191             use Log::Shiras::Report;
192             use Log::Shiras::Report::MetaMessage;
193             use Data::Dumper;
194             my $message_class = build_class(
195             package => 'Test',
196             add_roles_in_sequence => [
197             'Log::Shiras::Report',
198             'Log::Shiras::Report::MetaMessage',
199             ],
200             add_methods =>{
201             add_line => sub{
202             my( $self, $message ) = @_;
203             print Dumper( $message->{message} );
204             return 1;
205             },
206             }
207             );
208             my $message_instance = $message_class->new(
209             prepend =>[qw( lets go )],
210             postpend =>[qw( store package )],
211             );
212             $message_instance->add_line({ message =>[qw( to the )], package => 'here', });
213            
214             #######################################################################################
215             # Synopsis output to this point
216             # 01: $VAR1 = [
217             # 02: 'lets',
218             # 03: 'go',
219             # 04: 'to',
220             # 05: 'the',
221             # 06: 'store',
222             # 07: 'here'
223             # 08: ];
224             #######################################################################################
225            
226             $message_instance->set_post_sub(
227             sub{
228             my $message = $_[0];
229             my $new_ref;
230             for my $element ( @{$message->{message}} ){
231             push @$new_ref, uc( $element );
232             }
233             $message->{message} = $new_ref;
234             }
235             );
236             $message_instance->add_line({ message =>[qw( from the )], package => 'here', });
237            
238             #######################################################################################
239             # Synopsis output addition to this point
240             # 01: $VAR1 = [
241             # 02: 'LETS',
242             # 03: 'GO',
243             # 04: 'FROM',
244             # 05: 'THE',
245             # 06: 'STORE',
246             # 07: 'HERE'
247             # 08: ];
248             #######################################################################################
249            
250             $message_instance = $message_class->new(
251             hashpend => {
252             locate_jenny => sub{
253             my $message = $_[0];
254             my $answer;
255             for my $person ( keys %{$message->{message}->[0]} ){
256             if( $person eq 'Jenny' ){
257             $answer = "$person lives in: $message->{message}->[0]->{$person}" ;
258             last;
259             }
260             }
261             return $answer;
262             }
263             },
264             );
265             $message_instance->add_line({ message =>[{
266             Frank => 'San Fransisco',
267             Donna => 'Carbondale',
268             Jenny => 'Portland' }], });
269            
270             #######################################################################################
271             # Synopsis output addition to this point
272             # 01: $VAR1 = [
273             # 02: {
274             # 03: 'locate_jenny' => 'Jenny lives in: Portland',
275             # 04: 'Donna' => 'Carbondale',
276             # 05: 'Jenny' => 'Portland',
277             # 06: 'Frank' => 'San Fransisco'
278             # 07: }
279             # 08: ];
280             #######################################################################################
281            
282             $message_instance->set_pre_sub(
283             sub{
284             my $message = $_[0];
285             my $lookup = {
286             'San Fransisco' => 'CA',
287             'Carbondale' => 'IL',
288             'Portland' => 'OR',
289             };
290             for my $element ( keys %{$message->{message}->[0]} ){
291             $message->{message}->[0]->{$element} .=
292             ', ' . $lookup->{$message->{message}->[0]->{$element}};
293             }
294             }
295             );
296             $message_instance->add_line({ message =>[{
297             Frank => 'San Fransisco',
298             Donna => 'Carbondale',
299             Jenny => 'Portland' }], });
300            
301             #######################################################################################
302             # Synopsis output addition to this point
303             # 01: $VAR1 = [
304             # 02: {
305             # 03: 'locate_jenny' => 'Jenny lives in: Portland, OR',
306             # 04: 'Donna' => 'Carbondale, IL',
307             # 05: 'Jenny' => 'Portland, OR',
308             # 06: 'Frank' => 'San Fransisco, CA'
309             # 07: }
310             # 08: ];
311             #######################################################################################
312            
313             =head1 DESCRIPTION
314              
315             This is Moose role that can be used by L<Log::Shiras::Report> to massage the message prior
316             to 'add_line' being implemented in the report. It uses the hook built in the to Report
317             role for the method 'manage_message'.
318              
319             There are five ways to affect the passed message ref. Each way is set up as an L<attribute
320             |/Attributes> of the class. Details of how each is implemented is explained in the
321             Attributes section.
322              
323             =head2 Warning
324              
325             'hashpend' and 'prepend' - 'postpend' can conflict since 'hashpend' acts on the first
326             message element as if it were a hashref and the next two act as if the message is a list.
327             A good rule of thumb is to not use both sets together unless you really know what is going
328             on.
329              
330             =head2 Attributes
331              
332             Data passed to ->new when creating an instance. For modification of these attributes
333             after the instance is created see the attribute methods.
334              
335             =head3 pre_sub
336              
337             =over
338              
339             B<Definition:> This is a place to store a perl closure that will be passed the full
340             $message_ref including meta data. The results of the closure are not used so any
341             desired change should be done to the $message_ref itself since it is persistent. The
342             action takes place before all the other attributes are implemented so the changes will
343             NOT be available to process. See the example in the SYNOPSIS.
344              
345             B<Default:> None
346              
347             B<Required:> No
348              
349             B<Range:> it must pass the is_CodeRef test
350              
351             B<attribute methods>
352              
353             =over
354              
355             B<clear_pre_sub>
356              
357             =over
358              
359             B<Description> removes the stored attribute value
360              
361             =back
362              
363             B<has_pre_sub>
364              
365             =over
366              
367             B<Description> predicate for the attribute
368              
369             =back
370              
371             B<get_pre_sub>
372              
373             =over
374              
375             B<Description> returns the attribute value
376              
377             =back
378              
379             B<set_pre_sub( $closure )>
380              
381             =over
382              
383             B<Description> sets the attribute value
384              
385             =back
386              
387             =back
388              
389             =back
390              
391             =head3 hashpend
392              
393             =over
394              
395             B<Definition:> This will update the position %{$message_ref->{message}->[0]}. If
396             that position is not a hash ref then. It will kill the process with L<Carp> -
397             confess. After it passes that test it will perform the following assuming the
398             attribute is retrieved as $hashpend_ref and the entire message is passed as
399             $message_ref;
400              
401             for my $element ( keys %$hashpend_ref ){
402             $message_ref->{message}->[0]->{$element} =
403             is_CodeRef( $hashpend_ref->{$element} ) ?
404             $hashpend_ref->{$element}->( $message_ref ) :
405             exists $message_ref->{$hashpend_ref->{$element}} ?
406             $message_ref->{$hashpend_ref->{$element}} :
407             $hashpend_ref->{$element} ;
408             }
409            
410             This means that if the value of the $element is a closure then it will use the results
411             of that and add that to the message sub-hashref. Otherwise it will attempt to pull
412             the equivalent key from the $message meta-data and add it to the message sub-hashref or
413             if all else fails just load the key value pair as it stands to the message sub-hashref.
414              
415             B<Default:> None
416              
417             B<Required:> No
418              
419             B<Range:> it must be a hashref
420              
421             B<attribute methods>
422              
423             =over
424              
425             B<clear_hashpend>
426              
427             =over
428              
429             B<Description> removes the stored attribute value
430              
431             =back
432              
433             B<has_hashpend>
434              
435             =over
436              
437             B<Description> predicate for the attribute
438              
439             =back
440              
441             B<get_all_hashpend>
442              
443             =over
444              
445             B<Description> returns the attribute value
446              
447             =back
448              
449             B<add_to_hashpend( $key => $value|$closure )>
450              
451             =over
452              
453             B<Description> this adds to the attribute and can accept more than one $key => $value pair
454              
455             =back
456              
457             B<remove_from_hashpend( $key )>
458              
459             =over
460              
461             B<Description> removes the $key => $value pair associated with the passed $key from the
462             hashpend. This can accept more than one key at a time.
463              
464             =back
465              
466             =back
467              
468             =back
469              
470             =head3 prepend
471              
472             =over
473              
474             B<Definition:> This will push elements to the beginning of the list
475             @{$message_ref->{message}}. The elements are pushed in the reverse order that they are
476             stored in this attribute meaning that they will wind up in the stored order in the message
477             ref. The action assumes that
478             the attribute is retrieved as $prepend_ref and the entire message is passed as
479             $message_ref;
480              
481             for my $element ( reverse @$prepend_ref ){
482             unshift @{$message_ref->{message}}, (
483             exists $message_ref->{$element} ? $message_ref->{$element} :
484             $element );
485             }
486            
487             Unlike the hashpend attribute it will not handle CodeRefs.
488              
489             B<Default:> None
490              
491             B<Required:> No
492              
493             B<Range:> it must be an arrayref
494              
495             B<attribute methods>
496              
497             =over
498              
499             B<clear_prepend>
500              
501             =over
502              
503             B<Description> removes the stored attribute value
504              
505             =back
506              
507             B<has_prepend>
508              
509             =over
510              
511             B<Description> predicate for the attribute
512              
513             =back
514              
515             B<get_all_prepend>
516              
517             =over
518              
519             B<Description> returns the attribute value
520              
521             =back
522              
523             B<add_to_prepend( $element )>
524              
525             =over
526              
527             B<Description> this adds to the end of the attribute and can accept more than one $element
528              
529             =back
530              
531             =back
532              
533             =back
534              
535             =head3 postpend
536              
537             =over
538              
539             B<Definition:> This will push elements to the end of the list @{$message_ref->{message}}.
540             The elements are pushed in the order that they are stored in this attribute. The action
541             below assumes that the attribute is retrieved as $postpend_ref and the entire message is
542             passed as $message_ref;
543              
544             for my $element ( reverse @$postpend_ref ){
545             push @{$message_ref->{message}}, (
546             exists $message_ref->{$element} ? $message_ref->{$element} :
547             $element );
548             }
549            
550             Unlike the hashpend attribute it will not handle CodeRefs.
551              
552             B<Default:> None
553              
554             B<Required:> No
555              
556             B<Range:> it must be an arrayref
557              
558             B<attribute methods>
559              
560             =over
561              
562             B<clear_postpend>
563              
564             =over
565              
566             B<Description> removes the stored attribute value
567              
568             =back
569              
570             B<has_postpend>
571              
572             =over
573              
574             B<Description> predicate for the attribute
575              
576             =back
577              
578             B<get_all_postpend>
579              
580             =over
581              
582             B<Description> returns the attribute value
583              
584             =back
585              
586             B<add_to_postpend( $element )>
587              
588             =over
589              
590             B<Description> this adds to the end of the attribute and can accept more than one $element
591              
592             =back
593              
594             =back
595              
596             =back
597              
598             =head3 post_sub
599              
600             =over
601              
602             B<Definition:> This is a place to store a perl closure that will be passed the full
603             $message_ref including meta data. The results of the closure are not used so any
604             desired change should be done to the $message_ref itself since it is persistent. The
605             action takes place after all the other attributes are implemented so the changes will
606             be available to process. See the example in the SYNOPSIS.
607              
608             B<Default:> None
609              
610             B<Required:> No
611              
612             B<Range:> it must pass the is_CodeRef test
613              
614             B<attribute methods>
615              
616             =over
617              
618             B<clear_post_sub>
619              
620             =over
621              
622             B<Description> removes the stored attribute value
623              
624             =back
625              
626             B<has_post_sub>
627              
628             =over
629              
630             B<Description> predicate for the attribute
631              
632             =back
633              
634             B<get_post_sub>
635              
636             =over
637              
638             B<Description> returns the attribute value
639              
640             =back
641              
642             B<set_post_sub( $closure )>
643              
644             =over
645              
646             B<Description> sets the attribute value
647              
648             =back
649              
650             =back
651              
652             =back
653              
654             =head2 Methods
655              
656             =head3 manage_message( $message_ref )
657              
658             =over
659              
660             B<Definition:> This is a possible method called by L<Log::Shiras::Report> with the
661             intent of implementing the L<attributes|/Attributes> on each message passed to a
662             L<Log::Shiras::Switchboard/reports>. Actions taken on that message vary from attribute
663             to attribute and the specifics are explained in each. The attributes are implemented in
664             this order.
665              
666             pre_sub -> hashpend -> prepend -> postpend -> post_sub
667            
668              
669             B<Returns:> the (updated) $message_ref
670              
671             =back
672              
673             =head1 GLOBAL VARIABLES
674              
675             =over
676              
677             =item B<$ENV{hide_warn}>
678              
679             The module will warn when debug lines are 'Unhide'n. In the case where the you
680             don't want these notifications set this environmental variable to true.
681              
682             =back
683              
684             =head1 SUPPORT
685              
686             =over
687              
688             L<Log-Shiras/issues|https://github.com/jandrew/Log-Shiras/issues>
689              
690             =back
691              
692             =head1 TODO
693              
694             =over
695              
696             B<1.> Nothing L<currently|/SUPPORT>
697              
698             =back
699              
700             =head1 AUTHOR
701              
702             =over
703              
704             =item Jed Lund
705              
706             =item jandrew@cpan.org
707              
708             =back
709              
710             =head1 COPYRIGHT
711              
712             This program is free software; you can redistribute
713             it and/or modify it under the same terms as Perl itself.
714              
715             The full text of the license can be found in the
716             LICENSE file included with this module.
717              
718             =head1 DEPENDENCIES
719              
720             =over
721              
722             L<perl 5.010|perl/5.10.0>
723              
724             L<utf8>
725              
726             L<version>
727              
728             L<Moose::Role>
729              
730             L<MooseX::Types::Moose>
731              
732             L<Carp> - confess
733              
734             =back
735              
736             =cut
737              
738             #########1#########2 main pod documentation end 5#########6#########7#########8#########9