lib/Web/DataService/Vocabulary.pm

filter: Statementstmt 36.7% 32 / 87 Branchbran 17.3% 9 / 52 Conditioncond 4.0% 1 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 4 totaltotal 24.7% 47 / 190 CC 40 SCAR 62.1
CC
40
Cov
26%
document_vocabs
812.0
define_vocab
20.2
valid_vocab
2.0
CRAP 500.1
« lib/Web/DataService/Plugin/JSON.pm ↑ Summary lib/Web/DataService/Document.pm »
1 #
2 # Web::DataService::Vocabulary.pm
3 #
4 # This module provides a role that is used by 'Web::DataService'. It implements
5 # routines for defining and documenting vocabularies.
6 #
7 # Author: Michael McClennen
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Web::DataService::Vocabulary;
12  
13 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our (%VOCAB_DEF) = (name => 'ignore',
18 title => 'single',
19 doc_node => 'single',
20 use_field_names => 'single',
21 undocumented => 'single',
22 disabled => 'single');
23  
24  
25 # define_vocab ( attrs... )
26 #
27 # Define one or more vocabularies for data service responses. These
28 # vocabularies provide field names for the responses.
29  
30 sub define_vocab {
31  
32 1 ▶ my $ds = shift;
Subroutine define_vocab: called CC=13 SCAR=30.0
Pod undocumented
33
34 1 my ($last_node);
35
36 # Now we go through the rest of the arguments. Hashrefs define new
37 # vocabularies, while strings add to the documentation of the vocabulary
38 # whose definition they follow.
39
40 1 foreach my $item (@_)
41 {
42 # A hashref defines a new vocabulary.
43
44 2 ▶ if ( ref $item eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 1 1 2
elsif (not ref $item) { } 1 0 1
45 {
46 # Make sure the attributes include 'name'.
47
48 1 my $name = $item->{name};
49
50 1 ▶ croak "define_vocab: you must include the attribute 'name'" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 1 1
51
52 # Make sure this vocabulary was not already defined by a previous call,
53 # and set the attributes as specified.
54
55 croak "define_vocab: '$name' was already defined" if defined $ds->{vocab}{$name}
56 1 ▶ and not $ds->{vocab}{$name}{_default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ds->{"vocab"}{$name} and not $ds->{"vocab"}{$name}{"_default"} 0 1 1
Condition33%1/3
defined $ds->{"vocab"}{$name} and not $ds->{"vocab"}{$name}{"_default"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $ds->{"vocab"}{$name} and not $ds->{"vocab"}{$name}{"_default"}
defined $ds->{"vocab"}{$name} not $ds->{"vocab"}{$name}{"_default"}
Truth table33%1/3
A and not(B)
A= defined $ds->{"vocab"}{$name}B= not $ds->{"vocab"}{$name}{"_default"}
ABresult
0X0
100
111
57
58 # Create a new record to represent this vocabulary.
59
60 1 my $record = bless { name => $name }, 'Web::DataService::Vocab';
61
62 # If this entry is for the 'null' vocabulary, then use the
63 # existing record. If this record is to be disabled,
64 # remove it from the vocabulary list.
65
66 1 ▶ if ( $name eq 'null' )
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "null") 0 1 1
67 {
68 0 $record = $ds->{vocab}{null};
69 0 ▶ shift @{$ds->{vocab_list}} if $item->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"disabled"} 0 0 0
70 }
71
72 # Now set the attributes for this vocabulary.
73
74 1 foreach my $k ( keys %$item )
75 {
76 2 ▶ croak "define_vocab: invalid attribute '$k'" unless $VOCAB_DEF{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $Web::DataService::Vocabulary::VOCAB_DEF{$k} 0 2 2
77
78 2 $record->{$k} = $item->{$k};
79 }
80
81 # Now install the new vocabulary. But don't add it to the list if
82 # the 'disabled' attribute is set.
83
84 1 $ds->{vocab}{$name} = $record;
85 1 ▶ push @{$ds->{vocab_list}}, $name unless $record->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $record->{"disabled"} 1 0 1
86 1 $last_node = $record;
87 }
88
89 # A scalar is taken to be a documentation string.
90
91 elsif ( not ref $item )
92 {
93 1 $ds->add_node_doc($last_node, $item);
94 }
95
96 else
97 {
98 0 croak "define_vocab: arguments must be hashrefs and strings";
99 }
100 }
101
102 1 ▶ croak "define_vocab: the arguments must include a hashref of attributes"
Branch: 1 branch
BranchTrueFalseTotal
unless $last_node 0 1 1
103 unless $last_node;
104 }
105  
106  
107 # list_vocabs ( )
108 #
109 # Return the list of names of all the vocabularies that have been defined for
110 # this data service.
111  
112 sub list_vocabs {
113  
114 1 ▶ my ($ds) = @_;
Subroutine list_vocabs: called CC=1 SCAR=0.0
Pod undocumented
115 1 return @{$ds->{vocab_list}};
116 }
117  
118  
119 # valid_vocab ( )
120 #
121 # Return a code reference (actually a reference to a closure) that can be used
122 # in a parameter rule to validate a vocaubulary-selecting parameter. All
123 # non-disabled vocabularies are included.
124  
125 sub valid_vocab {
126
127 0 ▶ my ($ds) = @_;
Subroutine valid_vocab: not called CC=1 SCAR=6.9
Pod undocumented
128
129 # The ENUM_VALUE subroutine is defined by HTTP::Validate.pm.
130
131 0 return HTTP::Validate::ENUM_VALUE(@{$ds->{vocab_list}});
132 }
133  
134  
135 # document_vocabs ( path, options )
136 #
137 # Return a string containing POD documentation of the response vocabularies
138 # that are allowed for the specified path. If the option 'all' is true, then
139 # document all of the vocabularies enabled for this data service regardless of
140 # whether they are actually allowed for that path.
141 #
142 # If the option 'extended' is true, then include the text description of each
143 # vocabulary.
144  
145 sub document_vocabs {
146  
147 0 ▶ my ($ds, $path, $options) = @_;
Subroutine document_vocabs: not called CC=28 SCAR=67.0
Pod undocumented
148
149 0 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
150 0 ▶ $path ||= '/';
Condition0%0/2
$path ||= "/"
l!l
00
MC/DC0%0/1
$path ||= "/"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
151
152 # Go through the list of defined vocabularies in order, filtering out
153 # those which are not allowed for this path. The reason for doing it this
154 # way is so that the vocabularies will always be listed in the order
155 # defined, instead of the arbitrary hash order.
156
157 0 my @vocabs;
158
159 0 ▶ if ( $path eq '/' )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 0 0 0
160 {
161 0 @vocabs = grep { ! $ds->{vocab}{$_}{undocumented} } @{$ds->{vocab_list}};
162 }
163
164 else
165 {
166 0 my $allowed = $ds->node_attr($path, 'allow_vocab');
167
168 0 ▶ return '' unless ref $allowed eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $allowed eq "HASH" 0 0 0
169
170 0 ▶ @vocabs = grep { $allowed->{$_} && ! $ds->{vocab}{$_}{undocumented} } @{$ds->{vocab_list}};
Branch: 1 branch
BranchTrueFalseTotal
$allowed->{$_} and not $ds->{"vocab"}{$_}{"undocumented"} 0 0 0
171 0 ▶ return '' unless @vocabs;
Branch: 1 branch
BranchTrueFalseTotal
unless @vocabs 0 0 0
172 }
173
174 # Figure out the default formats for each vocabulary.
175
176 0 my %default_for;
177
178 0 foreach my $format ( @{$ds->{format_list}} )
179 {
180 0 ▶ my $default_vocab = $ds->{format}{$format}{default_vocab} // $ds->{vocab_list}[0];
Condition0%0/3
$ds->{"format"}{$format}{"default_vocab"} // $ds->{"vocab_list"}[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$ds->{"format"}{$format}{"default_vocab"} // $ds->{"vocab_list"}[0]
$ds->{"format"}{$format}{"default_vocab"} $ds->{"vocab_list"}[0]
Truth table0%0/3
A // B
A= $ds->{"format"}{$format}{"default_vocab"}B= $ds->{"vocab_list"}[0]
ABresult
1X1
011
000
181 0 ▶ push @{$default_for{$default_vocab}}, "C<$format>" if $default_vocab;
Branch: 1 branch
BranchTrueFalseTotal
if $default_vocab 0 0 0
182 }
183
184 # Go through the list of defined vocabularies in order,
185
186 0 my @paths = grep { $ds->{vocab}{$_}{doc_node} } @vocabs;
187
188 0 ▶ my $ext_header = $options->{extended} || ! @paths ? " | Description" : '';
Branch: 1 branch
BranchTrueFalseTotal
$options->{'extended'} || !@paths ? : 0 0 0
Condition0%0/3
$options->{"extended"} or not @paths
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"extended"} or not @paths
$options->{"extended"} not @paths
Truth table0%0/3
A or not(B)
A= $options->{"extended"}B= not @paths
ABresult
1X1
011
000
189 0 ▶ my $doc_header = @paths ? " | Documentation" : '';
Branch: 1 branch
BranchTrueFalseTotal
@paths ? : 0 0 0
190
191 0 my $doc = "=for wds_table_header Vocabulary* | Name | Default for $doc_header $ext_header\n\n";
192 0 $doc .= "=over\n\n";
193
194 0 ▶ if ( $options->{valid} )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"valid"}) 0 0 0
195 {
196 0 $doc = "=for wds_table_no_header Value* | Description\n\n";
197 0 $doc .= "=over\n\n";
198 }
199
200 VOCABULARY:
201 0 foreach my $name (@vocabs)
202 {
203 0 my $frec = $ds->{vocab}{$name};
204 0 ▶ my $title = $frec->{title} || $frec->{name};
Condition0%0/3
$frec->{'title'} || $frec->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$frec->{'title'} || $frec->{'name'}
$frec->{'title'} $frec->{'name'}
Truth table0%0/3
A || B
A= $frec->{'title'}B= $frec->{'name'}
ABresult
1X1
011
000
205 0 ▶ my $def_list = $default_for{$name} ? join(', ', @{$default_for{$name}}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$default_for{$name} ? : 0 0 0
206 0 ▶ my $doc_link = $ds->node_link($frec->{doc_node}) if $frec->{doc_node};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"doc_node"} 0 0 0
207
208 0 ▶ next VOCABULARY if $frec->{undocumented};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"undocumented"} 0 0 0
209
210 0 ▶ if ( $options->{valid} )
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"valid"}) 0 0 0
211 {
212 0 $doc .= "=item C<$frec->{name}>\n\n";
213 0 ▶ $doc .= "$frec->{doc_string}\n\n" if $frec->{doc_string};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"doc_string"} 0 0 0
214 0 next;
215 }
216
217 0 $doc .= "=item $title | C<$frec->{name}> | $def_list";
218 0 ▶ $doc .= " | $doc_link" if $doc_link && @paths && $options->{extended};
Branch: 1 branch
BranchTrueFalseTotal
if $doc_link and @paths and $options->{"extended"} 0 0 0
Condition0%0/6
$doc_link and @paths and $options->{"extended"}
!ll&&!rl&&r
000
$doc_link and @paths
!ll&&!rl&&r
000
MC/DC0%0/3
$doc_link and @paths and $options->{"extended"}
$doc_link @paths $options->{"extended"}
Truth table0%0/4
A and B and C
A= $doc_linkB= @pathsC= $options->{"extended"}
ABCresult
0XX0
10X0
1100
1111
219 0 $doc .= "\n\n";
220
221 0 ▶ if ( $options->{extended} || ! @paths )
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'extended'} or not @paths) { } 0 0 0
elsif ($doc_link) { } 0 0 0
Condition0%0/3
$options->{"extended"} or not @paths
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"extended"} or not @paths
$options->{"extended"} not @paths
Truth table0%0/3
A or not(B)
A= $options->{"extended"}B= not @paths
ABresult
1X1
011
000
222 {
223 0 ▶ $doc .= "$frec->{doc_string}\n\n" if $frec->{doc_string};
Branch: 1 branch
BranchTrueFalseTotal
if $frec->{"doc_string"} 0 0 0
224 }
225
226 elsif ( $doc_link )
227 {
228 0 $doc .= "$doc_link\n\n";
229 }
230 }
231
232 0 $doc .= "=back";
233
234 0 return $doc;
235 }
236  
237  
238 2 1;
« lib/Web/DataService/Plugin/JSON.pm ↑ Summary lib/Web/DataService/Document.pm »