File Coverage

blib/lib/Data/Walk/Extracted/Dispatch.pm
Criterion Covered Total %
statement 23 27 85.1
branch 3 8 37.5
condition n/a
subroutine 7 7 100.0
pod n/a
total 33 42 78.5


line stmt bran cond sub pod time code
1             package Data::Walk::Extracted::Dispatch;
2             our $AUTHORITY = 'cpan:JANDREW';
3 5     5   481183 use version; our $VERSION = version->declare('v0.28.0');
  5         5  
  5         29  
4             ###InternalExtracteDDispatcH warn "You uncovered internal logging statements for Data::Walk::Extracted::Dispatch-$VERSION" if !$ENV{hide_warn_for_test};
5             ###InternalExtracteDDispatcH use Data::Dumper;
6 5     5   433 use 5.010;
  5         13  
7 5     5   15 use utf8;
  5         9  
  5         26  
8 5     5   438 use Moose::Role;
  5         2941  
  5         24  
9 5     5   16570 use Carp qw( confess );
  5         9  
  5         788  
10              
11             #########1 private methods 3#########4#########5#########6#########7#########8#########9
12              
13             sub _dispatch_method{
14 3831     3831   4129 my ( $self, $dispatch_ref, $call, @arg_list ) = @_;
15             ###InternalExtracteDDispatcH warn "Made it to _dispatch_method with call: $call" if !$ENV{hide_warn_for_test};
16             ###InternalExtracteDDispatcH warn "...for dispatch ref:" . Dumper( $dispatch_ref ) if !$ENV{hide_warn_for_test};
17             ###InternalExtracteDDispatcH warn "...with the passed arguments:" . Dumper( @arg_list ) if !$ENV{hide_warn_for_test};
18 3831 100       4468 if( exists $dispatch_ref->{$call} ){
    50          
19 2999         2160 my $action = $dispatch_ref->{$call};
20             ###InternalExtracteDDispatcH warn "found the action: $call" if !$ENV{hide_warn_for_test};
21 2999         4538 return $self->$action( @arg_list );
22             }elsif( exists $dispatch_ref->{DEFAULT} ){
23 832         587 my $action = $dispatch_ref->{DEFAULT};
24             ###InternalExtracteDDispatcH warn "running the DEFAULT action ..." if !$ENV{hide_warn_for_test};
25 832         1137 return $self->$action( @arg_list );
26             }else{
27             my $dispatch_name =
28             ( exists $dispatch_ref->{name} ) ?
29 0 0         $dispatch_ref->{name} : undef ;
30 0           my $string = "Failed to find the '$call' dispatch";
31 0 0         $string .= " in the $dispatch_name" if $dispatch_name;
32 0           confess $string;
33             }
34             }
35              
36             #########1 Phinish strong 3#########4#########5#########6#########7#########8#########9
37              
38 5     5   21 no Moose::Role;
  5         5  
  5         18  
39              
40             1;
41             # The preceding line will help the module return a true value
42              
43             #########1 main pod docs 3#########4#########5#########6#########7#########8#########9
44              
45             __END__
46              
47             =head1 NAME
48              
49             Data::Walk::Extracted::Dispatch - Dispatch table management
50              
51             =head1 SYNOPSIS
52              
53             package Data::Walk::Extracted;
54             use Moose;
55             with 'Data::Walk::Extracted::Dispatch';
56              
57             my $main_down_level_data ={
58             ###### Purpose: Used to build the generic elements of the next passed ref down
59             ###### Recieves: the upper ref value
60             ###### Returns: the lower ref value or undef
61             name => '- Extracted - main down level data',
62             DEFAULT => sub{ undef },
63             before_method => sub{ return $_[1] },
64             after_method => sub{ return $_[1] },
65             branch_ref => \&_main_down_level_branch_ref,
66             };
67              
68              
69             for my $key ( keys %$upper_ref ){
70             my $return = $self->_dispatch_method(
71             $main_down_level_data, $key, $upper_ref->{$key},
72             );
73             $lower_ref->{$key} = $return if defined $return;
74             }
75              
76             ### this example will not run on it's own it just demonstrates usage!
77              
78              
79              
80             =head1 DESCRIPTION
81              
82             This role only serves the purpose of standardizing the handling of dispatch tables. It
83             will first attempt to call the passed dispatch call. If it cannot find it then it will
84             attempt a 'DEFAULT' call after which it will 'confess' to failure.
85              
86             =head1 Methods
87              
88             =head2 _dispatch_method( $dispatch_ref, $call, @arg_list ) - internal
89              
90             =over
91              
92             B<Definition:> To make a class extensible, the majority of the decision points
93             can be managed by (hash) dispatch tables. In order to have the dispatch behavior
94             common across all methods this role can be attached to the class to provided for
95             common dispatching. If the hash key requested is not available then the dispatch
96             method will attempt to call 'DEFAULT'. If both fail the method will 'confess'.
97              
98             B<Accepts:> This method expects to be called by $self. It first receives the
99             dispatch table (hash) as a data reference. Next, the target hash key is accepted as
100             $call. Finally, any arguments needed by the dispatch table are passed through in
101             @arg_list. if the dispatch table has a name => key the value will be used in any
102             confessed error message.
103              
104             B<Returns:> defined by the dispatch (hash) table
105              
106             =back
107              
108             =head1 TODO
109              
110             =over
111              
112             B<1.> Nothing yet!
113              
114             =back
115              
116             =head1 SUPPORT
117              
118             =over
119              
120             L<github Data-Walk-Extracted/issues|https://github.com/jandrew/Data-Walk-Extracted/issues>
121              
122             =back
123              
124             =head1 AUTHOR
125              
126             =over
127              
128             =item Jed Lund
129              
130             =item jandrew@cpan.org
131              
132             =back
133              
134             =head1 COPYRIGHT
135              
136             This program is free software; you can redistribute
137             it and/or modify it under the same terms as Perl itself.
138              
139             The full text of the license can be found in the
140             LICENSE file included with this module.
141              
142             This software is copyrighted (c) 2013, 2016 by Jed Lund.
143              
144             =head1 Dependencies
145              
146             =over
147              
148             L<version>
149              
150             L<utf8>
151              
152             L<Carp> - confess
153              
154             L<Moose::Role>
155              
156             =back
157              
158             =head1 SEE ALSO
159              
160             =over
161              
162             L<Log::Shiras::Unhide> - Can use to unhide '###InternalExtracteDDispatcH' tags
163              
164             L<Log::Shiras::TapWarn> - to manage the output of exposed '###InternalExtracteDDispatcH' lines
165              
166             L<Data::Walk::Extracted>
167              
168             =back
169              
170             =cut
171              
172             #########1 Main POD ends 3#########4#########5#########6#########7#########8#########9