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   77 use strict;
  11         28  
  11         417  
4 11     11   59 use warnings;
  11         21  
  11         785  
5              
6 11     11   69 use vars qw($VERSION);
  11         22  
  11         738  
7             $VERSION = '2.00';
8              
9 11     11   75 use Scalar::Util qw(blessed);
  11         24  
  11         6125  
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 69     69   116 my $sub = shift();
16 69         117 local $Params::Validate::Dependencies::DOC = $sub;
17 69         230 $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 66     66   162 my $sub = shift;
24 66         184 my $list = {@_}->{list};
25 66         208 (my $name = $sub->name()) =~ s/_/ /g;
26              
27             my @list = (
28 140         274 (map { (my $t = $_) =~ s/'/\\'/g; "'$t'" } grep { !ref($_) } @{$list}), # scalars first, quoted
  140         341  
  177         347  
  66         170  
29 66         145 (grep { ref($_) } @{$list}) # then code-refs
  177         463  
  66         116  
30             );
31            
32             return
33             $name.' ('.
34             (
35             $#list > 0
36 66 100       318 ? join(', ', map { _doc_element($_) } @list[0 .. $#list - 1]).
  111         211  
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 177     177   296 my $element = shift;
47 177 100       371 if(!ref($element)) { return $element }
  140 100       858  
48 36         106 elsif(blessed($element)) { return $element->_document(); }
49 1         14 else { return '[coderef does not support autodoc]' }
50             }
51              
52             1;