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   74 use strict;
  11         23  
  11         317  
4 11     11   80 use warnings;
  11         22  
  11         307  
5              
6 11     11   67 use vars qw($VERSION);
  11         24  
  11         567  
7             $VERSION = '1.41';
8              
9 11     11   77 use Scalar::Util qw(blessed);
  11         29  
  11         4912  
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 83     83   153 my $sub = shift();
16 83         158 local $Params::Validate::Dependencies::DOC = $sub;
17 83         399 $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 79     79   318 my $sub = shift;
24 79         201 my $list = {@_}->{list};
25 79         243 (my $name = $sub->name()) =~ s/_/ /g;
26              
27             my @list = (
28 164         330 (map { (my $t = $_) =~ s/'/\\'/g; "'$t'" } grep { !ref($_) } @{$list}), # scalars first, quoted
  164         386  
  210         474  
  79         157  
29 79         194 (grep { ref($_) } @{$list}) # then code-refs
  210         462  
  79         149  
30             );
31            
32             return
33             $name.' ('.
34             (
35             $#list > 0
36 79 100       336 ? join(', ', map { _doc_element($_) } @list[0 .. $#list - 1]).
  131         288  
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 210     210   400 my $element = shift;
47 210 100       538 if(!ref($element)) { return $element }
  164 100       883  
48 44         157 elsif(blessed($element)) { return $element->_document(); }
49 2         23 else { return '[coderef does not support autodoc]' }
50             }
51              
52             1;