blib/lib/MooseX/Documenter.pm

filter: Statementstmt 98.5% 140 / 142 Branchbran 100% 56 / 56 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 20 / 20 Podpod 72.7% 8 / 11 totaltotal 97.8% 230 / 235 CC 40 SCAR 13.6
CC
40
Cov
99%
inherited_attributes
7.0
roles
6.0
local_methods
6.0
CRAP 3.9
↑ Summary
1 package MooseX::Documenter;
2  
3 37 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 require PPI::Document;
8 16 require Exporter;
9 37 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 $VERSION = '0.01';
12  
13 42 ▶sub setmooselib {
Subroutine setmooselib: called CC=1 SCAR=0.0
Pod documented
14 63 my $self = shift;
15 42 my $path = shift;
16  
17 42 $self->{moose_lib} = $path;
18 }
19  
20 #requires library path to source of moose objects, and name of moose object to get documentation for..
21 # example /src/moose/lib TNT::Form
22 68 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
23 68 my $classname = shift;
24 68 my $lib = shift;
25 68 my $object = shift;
26  
27 68 my $source = $lib . '/' . join( '/', split( '::', $object ) ) . '.pm';
28  
29 # load the PPI object
30 68 ▶ -e $lib || die "Directory can not be opened: $lib: $!";
Branch: 1 branch
BranchTrueFalseTotal
-e $lib or die "Directory can not be opened: ${lib}: $!" 67 1 68
31 67 my $document = PPI::Document->new($source);
32  
33 # load the moose class object
34 67 ▶ eval "use lib '$lib';use $object;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
35 67 ▶ die "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 66 67
36 66 my $class_meta = $object->meta;
37  
38 66 bless {
39 ppi_object => $document,
40 meta_object => $class_meta,
41 user_lib => $lib,
42 moose_lib => '/opt/local/perl/lib/site_perl/5.10.0',
43 object_name => $object,
44 inherited_attributes => {},
45 inherited_methods => {},
46 role_method_exclusions => {},
47 parents => [],
48 functions_run => {}
49 },
50 $classname;
51 }
52  
53 #return undef if no attributes or hashref of attributes
54 42 ▶sub local_attributes {
Subroutine local_attributes: called CC=4 SCAR=13.9
Pod documented
55 42 my $self = shift;
56  
57 #must run roles function object before this one
58 42 ▶ $self->roles unless $self->{functions_run}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"roles"} 23 19 42
59  
60 42 my $meta_object = $self->{'meta_object'};
61  
62 42 for my $attr ( $meta_object->get_attribute_list() ) {
63 31 my $attribute = $meta_object->get_attribute($attr);
64 31 $self->{local_attributes}->{$attr} = get_attribute_details($attribute);
65 }
66  
67 42 $self->{functions_run}->{local_attributes} = 1;
68 42 ▶ return undef if scalar( keys %{ $self->{local_attributes} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"local_attributes"};} == 0 24 18 42
69 18 return $self->{local_attributes};
70 }
71  
72 12 ▶sub inherited_attributes {
Subroutine inherited_attributes: called CC=7 SCAR=19.5
Pod documented
73 12 my $self = shift;
74  
75 12 ▶ $self->roles unless $self->{functions_run}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"roles"} 1 11 12
76 12 ▶ $self->class_parents unless $self->{functions_run}->{class_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"class_parents"} 9 3 12
77  
78 12 foreach my $parent ( @{ $self->{parents} } ) {
79 15 my $parent_doc;
80 15 ▶ if( $parent eq 'Moose::Object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent eq 'Moose::Object') { } 10 5 15
81 10 $parent_doc = MooseX::Documenter->new( $self->{moose_lib}, $parent );
82 }
83 else {
84 5 $parent_doc = MooseX::Documenter->new( $self->{user_lib}, $parent );
85 }
86  
87 15 $parent_doc->setmooselib( $self->{moose_lib} );
88  
89 15 my $parent_attributes = $parent_doc->local_attributes;
90  
91 15 ▶ $self->{inherited_attributes}->{$parent} = $parent_attributes
Branch: 1 branch
BranchTrueFalseTotal
if $parent_attributes 5 10 15
92 if $parent_attributes;
93 }
94  
95 return undef
96 12 ▶ if scalar( keys %{ $self->{inherited_attributes} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"inherited_attributes"};} == 0 7 5 12
97 5 return $self->{inherited_attributes};
98 }
99  
100 # returns undef or hash ref of local methods.
101 27 ▶sub local_methods {
Subroutine local_methods: called CC=6 SCAR=17.9
Pod documented
102 27 my $self = shift;
103  
104 #must run roles function object before this one
105 27 ▶ $self->roles unless $self->{functions_run}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"roles"} 15 12 27
106 27 ▶ $self->local_attributes unless $self->{functions_run}->{local_attributes};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"local_attributes"} 15 12 27
107  
108 27 my $meta_object = $self->{'meta_object'};
109  
110 27 for my $method ( $meta_object->get_method_list() ) {
111 next
112 156 ▶ if $self->{local_attributes}->{ $method
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"local_attributes"}{$method} 21 135 156
113 }; # hide methods that are just attribute accessors if are the same name
114 135 ▶ next if $self->{role_method_exclusions}->{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"role_method_exclusions"}{$method} 12 123 135
115 123 $self->{local_methods}->{$method} = $self->get_method_source($method);
116 }
117  
118 27 $self->{functions_run}->{local_methods} = 1;
119 27 return $self->{local_methods};
120 }
121  
122 12 ▶sub inherited_methods {
Subroutine inherited_methods: called CC=6 SCAR=17.9
Pod documented
123 12 my $self = shift;
124  
125 12 ▶ $self->roles unless $self->{functions_run}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"roles"} 1 11 12
126 12 ▶ $self->class_parents unless $self->{functions_run}->{class_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"functions_run"}{"class_parents"} 1 11 12
127  
128 12 foreach my $parent ( @{ $self->{parents} } ) {
129 15 my $parent_doc;
130 15 ▶ if( $parent eq 'Moose::Object' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent eq 'Moose::Object') { } 10 5 15
131 10 $parent_doc = MooseX::Documenter->new( $self->{moose_lib}, $parent );
132 }
133 else {
134 5 $parent_doc = MooseX::Documenter->new( $self->{user_lib}, $parent );
135 }
136  
137 15 $parent_doc->setmooselib( $self->{moose_lib} );
138  
139 15 $self->{inherited_methods}->{$parent} = $parent_doc->local_methods;
140 }
141  
142 return undef
143 12 ▶ if scalar( keys %{ $self->{inherited_methods} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$$self{"inherited_methods"};} == 0 2 10 12
144 10 return $self->{inherited_methods};
145 }
146  
147 #return undef or array ref of parents.
148 21 ▶sub class_parents {
Subroutine class_parents: called CC=5 SCAR=16.1
Pod documented
149 21 my $self = shift;
150  
151 21 ▶ return $self->{parents} if $self->{functions_run}->{class_parents};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"functions_run"}{"class_parents"} 9 12 21
152 12 $self->{functions_run}->{class_parents} = 1;
153  
154 12 ▶ return undef if $self->is_role;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_role 1 11 12
155  
156 11 my @classes = $self->{meta_object}->linearized_isa;
157 11 my $class_size = @classes;
158  
159 11 ▶ for( my $i = 1 ; $i < $class_size ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $class_size) 15 11 26
160 15 push( @{ $self->{parents} }, $classes[$i] );
161 }
162  
163 11 ▶ return undef if scalar( @{ $self->{parents} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"parents"};} == 0 1 10 11
164 10 return $self->{parents};
165 }
166  
167 # returns array_ref of role name(s) or undef
168 52 ▶sub roles {
Subroutine roles: called CC=6 SCAR=18.0
Pod documented
169 52 my $self = shift;
170 52 my $meta_object = $self->{meta_object};
171  
172 52 my @return = ();
173  
174 52 $self->{functions_run}->{roles} = 1;
175  
176 52 ▶ return undef if $self->is_role;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_role 2 50 52
177  
178 50 foreach my $role ( @{ $meta_object->roles } ) {
179 12 my $rolename = $role->name();
180 12 push( @return, $rolename );
181  
182 #make document class for the role to populate into here.
183 12 my $role_doc = MooseX::Documenter->new( $self->{user_lib}, $rolename );
184  
185 12 for my $attr ( $role->get_attribute_list() ) {
186 0 my $attribute = $meta_object->get_attribute($attr);
187 0 $self->{inherited_attributes}->{$rolename} =
188 get_attribute_details($attribute);
189 }
190  
191 12 for my $method ( $role->get_method_list() ) {
192 24 $self->{role_method_exclusions}->{$method} = 1;
193 24 $self->{inherited_methods}->{$rolename}->{$method} =
194 $role_doc->get_method_source($method);
195 }
196 }
197  
198 50 ▶ return undef if scalar(@return) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @return == 0 38 12 50
199 12 return \@return;
200 }
201  
202 # if is a role or not, 1=yes, 0 = no
203 64 ▶sub is_role {
Subroutine is_role: called CC=2 SCAR=6.9
Pod undocumented
204 64 my $self = shift;
205  
206 64 ▶ return 1 if $self->{meta_object}->isa("Moose::Meta::Role");
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"meta_object"}->isa("Moose::Meta::Role") 3 61 64
207 61 return 0;
208 }
209  
210 31 ▶sub get_attribute_details {
Subroutine get_attribute_details: called CC=5 SCAR=16.1
Pod undocumented
211 31 my $attribute = shift;
212  
213 31 my $details;
214 31 ▶ $details->{is} = $attribute->{is} || '';
Condition100%2/2
$attribute->{'is'} || ''
l!l
292
MC/DC100%1/1
$attribute->{'is'} || ''
$attribute->{'is'}
Truth table100%2/2
A
A= $attribute->{'is'}
Aresult
11
00
215 31 ▶ $details->{isa} = $attribute->{isa} || '';
Condition100%2/2
$attribute->{'isa'} || ''
l!l
292
MC/DC100%1/1
$attribute->{'isa'} || ''
$attribute->{'isa'}
Truth table100%2/2
A
A= $attribute->{'isa'}
Aresult
11
00
216  
217 31 my @modifiers;
218 31 foreach
219 my $mod (qw/ required lazy lazy_build coerce weak_ref trigger handles /) {
220 217 ▶ push @modifiers, $mod if $attribute->{$mod};
Branch: 1 branch
BranchTrueFalseTotal
if $attribute->{$mod} 31 186 217
221 }
222  
223 31 $details->{modifiers} = join ',', @modifiers;
224  
225 31 return $details;
226 }
227  
228 # returns undef or the source code of method.
229 147 ▶sub get_method_source {
Subroutine get_method_source: called CC=4 SCAR=13.9
Pod undocumented
230 147 my $self = shift;
231 147 my $method = shift;
232  
233 # Find all the named subroutines
234 my $sub_nodes =
235 $self->{ppi_object}
236 147 ▶ ->find( sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa("PPI::Statement::Sub") and $_[1]->name 838 92915 93753
237  
238 147 ▶ return undef unless $sub_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_nodes 2 145 147
239 145 my @nodes = @{$sub_nodes};
240  
241 145 foreach my $node (@nodes) {
242 552 ▶ if( $node->name eq $method ) { return $node->content; }
Branch: 1 branch
BranchTrueFalseTotal
if ($node->name eq $method) 103 449 552
243 }
244  
245 42 return undef;
246 }
247  
248 16 1;
249 __END__
↑ Summary