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 9     9   52 use strict;
  9         30  
  9         202  
4 9     9   39 use warnings;
  9         21  
  9         215  
5              
6 9     9   45 use vars qw($VERSION);
  9         24  
  9         377  
7             $VERSION = '1.01';
8              
9 9     9   48 use Scalar::Util qw(blessed);
  9         18  
  9         2678  
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 50     50   95 my $sub = shift();
16 50         83 local $Params::Validate::Dependencies::DOC = $sub;
17 50         268 $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 48     48   225 my $sub = shift;
24 48         108 my $list = {@_}->{list};
25 48         163 (my $name = $sub->name()) =~ s/_/ /g;
26              
27             my @list = (
28 98         184 (map { (my $t = $_) =~ s/'/\\'/g; "'$t'" } grep { !ref($_) } @{$list}), # scalars first, quoted
  98         232  
  128         265  
  48         118  
29 48         114 (grep { ref($_) } @{$list}) # then code-refs
  128         303  
  48         86  
30             );
31            
32             return
33             $name.' ('.
34             (
35             $#list > 0
36 48 100       189 ? join(', ', map { _doc_element($_) } @list[0 .. $#list - 1]).
  80         171  
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 128     128   228 my $element = shift;
47 128 100       305 if(!ref($element)) { return $element }
  98 100       443  
48 28         89 elsif(blessed($element)) { return $element->_document(); }
49 2         16 else { return '[coderef does not support autodoc]' }
50             }
51              
52             1;