File Coverage

blib/lib/Method/Signatures/WithDocumentation.pm
Criterion Covered Total %
statement 55 55 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 24 24 100.0
pod 0 9 0.0
total 81 93 87.1


line stmt bran cond sub pod time code
1 1     1   10 use strictures 2;
  1         5  
  1         28  
2              
3             package Method::Signatures::WithDocumentation;
4              
5             # ABSTRACT: use Method::Signatures with Sub::Documentation together
6              
7 1     1   824 use Attribute::Handlers;
  1         4835  
  1         5  
8 1     1   527 use Sub::Documentation 'add_documentation';
  1         575  
  1         58  
9 1     1   6 use Moose;
  1         5  
  1         7  
10             extends 'Method::Signatures';
11              
12             our $VERSION = '0.001'; # VERSION
13              
14             around parse_proto => sub {
15             my ($orig, $self, @args) = @_;
16             my $code = $self->$orig(@args);
17             my $signature = $self->{signature};
18             my @parameters = @{ $signature->parameters };
19             my @short_sig;
20             foreach my $parameter (@parameters) {
21             if ($parameter->is_yadayada) {
22             push @short_sig => '...';
23             next;
24             }
25             my @doc = ($parameter->type, $parameter->variable);
26             push @doc => 'named' if $parameter->is_named;
27             push @doc => 'aliased' if $parameter->is_ref_alias;
28             push @doc => 'required' if $parameter->is_required;
29             push @doc => 'optional' if not $parameter->is_required;
30             push @doc => 'defaults to C<<< '.$parameter->default.' >>>' if $parameter->default;
31             push @doc => 'but only when C<<< '.$parameter->default_when.' >>>' if $parameter->default_when;
32              
33             my $short_sig = $parameter->type . ' ';
34             $short_sig .= "\\" if $parameter->is_ref_alias;
35             $short_sig .= ':' if $parameter->is_named;
36             $short_sig .= $parameter->variable;
37             $short_sig = "[ $short_sig ]" unless $parameter->is_required;
38              
39             add_documentation(
40             package => $self->{into},
41             glob_type => 'CODE',
42             name => $self->{function_name},
43             type => 'param_signature',
44             documentation => \@doc,
45             );
46             push @short_sig => $short_sig;
47             }
48             add_documentation(
49             package => $self->{into},
50             glob_type => 'CODE',
51             name => $self->{function_name},
52             type => 'type',
53             documentation => $self->{name},
54             );
55             add_documentation(
56             package => $self->{into},
57             glob_type => 'CODE',
58             name => $self->{function_name},
59             type => 'signature',
60             documentation => join(', ', @short_sig),
61             );
62             return $code;
63             };
64              
65             sub _add_attr_doc {
66 29     29   40 my ($type, $package, $symbol, $referent, $data) = @_[0..3,5];
67 29 50 33     55 $data = $data->[0] if ref($data) eq 'ARRAY' && @$data == 1;
68 29         57 add_documentation(
69             package => $package,
70             glob_type => ref($referent),
71 29         24 name => *{$symbol}{NAME},
72             type => $type,
73             documentation => $data,
74             );
75             }
76              
77              
78 1     1   7669 no warnings 'redefine'; ## no critic
  1         5  
  1         71  
79              
80              
81             sub UNIVERSAL::Purpose : ATTR(CODE,BEGIN,RAWDATA) {
82 3     3 0 1444 _add_attr_doc(purpose => @_);
83 1     1   4 }
  1         2  
  1         7  
84              
85              
86             sub UNIVERSAL::Pod : ATTR(CODE,BEGIN,RAWDATA) {
87 4     4 0 180 _add_attr_doc(pod => @_);
88 1     1   414 }
  1         2  
  1         3  
89              
90              
91             sub UNIVERSAL::Param : ATTR(CODE,BEGIN,RAWDATA) {
92 6     6 0 272 _add_attr_doc(param => @_);
93 1     1   288 }
  1         2  
  1         3  
94              
95              
96             sub UNIVERSAL::Author : ATTR(CODE,BEGIN,RAWDATA) {
97 4     4 0 178 _add_attr_doc(author => @_);
98 1     1   331 }
  1         1  
  1         3  
99              
100              
101             sub UNIVERSAL::Returns : ATTR(CODE,BEGIN,RAWDATA) {
102 2     2 0 93 _add_attr_doc(returns => @_);
103 1     1   368 }
  1         2  
  1         4  
104              
105              
106             sub UNIVERSAL::Throws : ATTR(CODE,BEGIN,RAWDATA) {
107 4     4 0 180 _add_attr_doc(throws => @_);
108 1     1   385 }
  1         1  
  1         10  
109              
110              
111             sub UNIVERSAL::Example : ATTR(CODE,BEGIN,RAWDATA) {
112 2     2 0 120 _add_attr_doc(example => @_);
113 1     1   352 }
  1         1  
  1         3  
114              
115              
116             sub UNIVERSAL::Since : ATTR(CODE,BEGIN,RAWDATA) {
117 2     2 0 92 _add_attr_doc(since => @_);
118 1     1   337 }
  1         2  
  1         7  
119              
120              
121             sub UNIVERSAL::Deprecated : ATTR(CODE,BEGIN,RAWDATA) {
122 2     2 0 93 _add_attr_doc(deprecated => @_);
123 1     1   347 }
  1         1  
  1         4  
124              
125             1;
126              
127             __END__
128              
129             =pod
130              
131             =head1 NAME
132              
133             Method::Signatures::WithDocumentation - use Method::Signatures with Sub::Documentation together
134              
135             =head1 VERSION
136              
137             version 0.001
138              
139             =head1 SYNOPSIS
140              
141             use Method::Signatures::WithDocumentation;
142            
143             method foo (Str $text) :Purpose(Does something with text) {
144             ...
145             }
146              
147             =head1 DESCRIPTION
148              
149             This module extends L<Method::Signatures> to grab out parameter definitions. It behaves also similiar to L<Sub::Documentation::Attributes>, but with an important fix to let it work together with L<Pod::Weaver::Section::AutoDoc> (which is also part of this package).
150              
151             =head1 SUBROUTINE ATTRIBUTES
152              
153             Each of the attributes (except C<Deprecated>) requires a non-interpolated string. B<Please note that all parantheses must be balanced>.
154              
155             =head2 Purpose
156              
157             A brief description what the function/method does.
158              
159             =head2 Pod
160              
161             Free-text deeper description of whats going on.
162              
163             =head2 Param
164              
165             A description of a function/method param, suggested by the following format:
166              
167             method xxx ($foo, $bar) :Param($foo: This is foo) :Param($bar: This is bar) { ... }
168              
169             Just the variable name (without modifiers like C<\>, C<:>, C<?> or C<!>) followed by a colon and the description.
170              
171             =head2 Author
172              
173             Name of the author of the method/function, if it differs from the module author and the name should be explicity printed in the documentation.
174              
175             Maybe used more than once, one for each author.
176              
177             =head2 Returns
178              
179             A free text what will be returned.
180              
181             =head2 Throws
182              
183             A free text what will be thrown in case of whatever.
184              
185             Maybe used more than once, one for each case.
186              
187             =head2 Example
188              
189             A verbatim text, like a synopsis at the beginning of each module documentation.
190              
191             =head2 Since
192              
193             An identifier since when the method/function is available. For example a date or a version number.
194              
195             =head2 Deprecated
196              
197             This attributes marks the method/function as deprecated. The reason is optional.
198              
199             =head1 SEE ALSO
200              
201             =over 4
202              
203             =item * L<Pod::Weaver::Section::AutoDoc>
204              
205             =back
206              
207             =head1 BUGS
208              
209             Please report any bugs or feature requests on the bugtracker website
210             https://github.com/zurborg/libmethod-signatures-withdocumentation-perl/issu
211             es
212              
213             When submitting a bug or request, please include a test-file or a
214             patch to an existing test-file that illustrates the bug or desired
215             feature.
216              
217             =head1 AUTHOR
218              
219             David Zurborg <zurborg@cpan.org>
220              
221             =head1 COPYRIGHT AND LICENSE
222              
223             This software is Copyright (c) 2015 by David Zurborg.
224              
225             This is free software, licensed under:
226              
227             The ISC License
228              
229             =cut