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