File Coverage

blib/lib/Params/Validate/Dependencies/Documenter.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Params::Validate::Dependencies::Documenter;
2              
3 11     11   64 use strict;
  11         19  
  11         252  
4 11     11   47 use warnings;
  11         20  
  11         241  
5              
6 11     11   47 use vars qw($VERSION);
  11         21  
  11         431  
7             $VERSION = '1.01';
8              
9 11     11   59 use Scalar::Util qw(blessed);
  11         33  
  11         3765  
10              
11             # sets a magic flag in P::V::D that the code-refs use to tell
12             # whether they should document themselves or validate, then
13             # calls the code-ref
14             sub _document {
15 80     80   138 my $sub = shift();
16 80         125 local $Params::Validate::Dependencies::DOC = $sub;
17 80         339 $sub->({});
18             }
19              
20             # gets passed the list of options for this validator, and spits
21             # out doco, recursing as necessary
22             sub _doc_me {
23 77     77   258 my $sub = shift;
24 77         197 my $list = {@_}->{list};
25 77         246 (my $name = $sub->name()) =~ s/_/ /g;
26              
27             my @list = (
28 157         278 (map { (my $t = $_) =~ s/'/\\'/g; "'$t'" } grep { !ref($_) } @{$list}), # scalars first, quoted
  157         345  
  202         401  
  77         151  
29 77         203 (grep { ref($_) } @{$list}) # then code-refs
  202         381  
  77         132  
30             );
31            
32             return
33             $name.' ('.
34             (
35             $#list > 0
36 77 100       300 ? join(', ', map { _doc_element($_) } @list[0 .. $#list - 1]).
  125         248  
37             " ".$sub->join_with().' '._doc_element($list[-1])
38             : _doc_element($list[0])
39             ).
40             ')';
41             }
42              
43             # passed an option, returning it if it's scalar, otherwise
44             # calling its ->_document() method
45             sub _doc_element {
46 202     202   358 my $element = shift;
47 202 100       439 if(!ref($element)) { return $element }
  157 100       822  
48 43         113 elsif(blessed($element)) { return $element->_document(); }
49 2         20 else { return '[coderef does not support autodoc]' }
50             }
51              
52             1;