lib/Web/DataService/Set.pm

filter: Statementstmt 51.0% 49 / 96 Branchbran 26.0% 12 / 46 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 8 totaltotal 37.2% 70 / 188 CC 33 SCAR 32.6
CC
33
Cov
40%
document_set
56.0
valid_set
42.0
set_values
42.0
CRAP 26.0
« lib/Web/DataService/IDocument.pm ↑ Summary blib/lib/Web/DataService/IRequest.pm »
1 #
2 # Web::DataService::Set
3 #
4 # This module provides a role that is used by 'Web::DataService'. It implements
5 # routines for defining and documenting output formats.
6 #
7 # Author: Michael McClennen
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Web::DataService::Set;
12  
13 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 our (%SET_DEF) = (value => 'single',
20 maps_to => 'single',
21 disabled => 'single',
22 undocumented => 'single',
23 doc_string => 'single');
24  
25 # define_map ( name, specification... )
26 #
27 # Define a set of values, with optional value map and documentation. Such
28 # sets can be used to define and document acceptable parameter values,
29 # document data values, and many other uses.
30 #
31 # The names of sets must be unique within a single data service.
32  
33 sub define_set {
34  
35 1 ▶ my $self = shift;
Subroutine define_set: called CC=14 SCAR=28.6
Pod undocumented
36 1 my $name = shift;
37
38 # Make sure the name is unique.
39
40 1 ▶ croak "define_set: the first argument must be a valid name"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->valid_name($name) 0 1 1
41 unless $self->valid_name($name);
42
43 croak "define_set: '$name' was already defined at $self->{valueset}{$name}{defined_at}"
44 1 ▶ if ref $self->{valueset}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"valueset"}{$name} 0 1 1
45
46 # Create a new set object.
47
48 1 my ($package, $filename, $line) = caller;
49
50 1 my $vs = { name => $name,
51 defined_at => "line $line of $filename",
52 value => {},
53 value_list => [] };
54
55 1 bless $vs, 'Web::DataService::Set';
56
57 1 $self->{set}{$name} = $vs;
58
59 # Then process the records and documentation strings one by one. Throw an
60 # exception if we find an invalid record.
61
62 1 my $doc_node;
63 my @doc_lines;
64
65 1 foreach my $item (@_)
66 {
67 # A scalar is interpreted as a documentation string.
68
69 4 ▶ unless ( ref $item )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item) 2 2 4
70 {
71 2 ▶ $self->add_doc($vs, $item) if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 2 0 2
72 2 next;
73 }
74
75 # Any item that is not a record or a scalar is an error.
76
77 2 ▶ unless ( ref $item && reftype $item eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item and builtin::reftype($item) eq "HASH") 0 2 2
Condition33%1/3
ref $item and builtin::reftype($item) eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $item and builtin::reftype($item) eq "HASH"
ref $item builtin::reftype($item) eq "HASH"
Truth table33%1/3
A and B
A= ref $itemB= builtin::reftype($item) eq "HASH"
ABresult
0X0
100
111
78 {
79 0 croak "define_set: arguments must be records (hash refs) and documentation strings";
80 }
81
82 # Add the record to the documentation list.
83
84 2 $self->add_doc($vs, $item);
85
86 # Check for invalid attributes.
87
88 2 foreach my $k ( keys %$item )
89 {
90 croak "define_set: unknown attribute '$k'"
91 4 ▶ unless defined $SET_DEF{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Web::DataService::Set::SET_DEF{$k} 0 4 4
92 }
93
94 # Check that each reord contains an actual value, and that these
95 # values do not repeat.
96
97 2 my $value = $item->{value};
98
99 2 ▶ croak "define_set: you must specify a nonempty 'value' key in each record"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value ne "" 0 2 2
Condition33%1/3
defined $value and $value ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
100 unless defined $value && $value ne '';
101
102 croak "define_set: value '$value' cannot be defined twice"
103 2 ▶ if exists $vs->{value}{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $vs->{"value"}{$value} 0 2 2
104
105 # Add the value to the various lists it belongs to, and to the hash
106 # containing all defined values.
107
108 2 ▶ push @{$vs->{value_list}}, $value unless $item->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"disabled"} 2 0 2
109 2 $vs->{value}{$value} = $item;
110 }
111
112 # Finish the documentation for this object.
113
114 1 $self->process_doc($vs);
115
116 1 my $a = 1; # we can stop here when debugging
117 }
118  
119  
120 # set_defined ( name )
121 #
122 # Return true if the given argument is the name of a set that has been defined
123 # for the current data service, false otherweise.
124  
125 sub set_defined {
126
127 0 ▶ my ($self, $name) = @_;
Subroutine set_defined: not called CC=1 SCAR=6.9
Pod undocumented
128
129 0 return ref $self->{set}{$name} eq 'Web::DataService::Set';
130 }
131  
132  
133 # valid_set ( name )
134 #
135 # Return a reference to a validator routine (actualy a closure) which will
136 # accept the list of values defined for the specified set. If the given name
137 # does not correspond to any set, the returned routine will reject any value
138 # it is given.
139  
140 sub valid_set {
141  
142 0 ▶ my ($self, $set_name) = @_;
Subroutine valid_set: not called CC=6 SCAR=37.4
Pod undocumented
143
144 0 my $vs = $self->{set}{$set_name};
145
146 0 ▶ unless ( ref $vs eq 'Web::DataService::Set' )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $vs eq "Web::DataService::Set") 0 0 0
147 {
148 0 ▶ unless ( $Web::DataService::QUIET || $ENV{WDS_QUIET} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::DataService::QUIET or $ENV{"WDS_QUIET"}) 0 0 0
Condition0%0/3
$Web::DataService::QUIET or $ENV{"WDS_QUIET"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Web::DataService::QUIET or $ENV{"WDS_QUIET"}
$Web::DataService::QUIET $ENV{"WDS_QUIET"}
Truth table0%0/3
A or B
A= $Web::DataService::QUIETB= $ENV{"WDS_QUIET"}
ABresult
1X1
011
000
149 {
150 0 warn "WARNING: unknown set '$set_name'";
151 }
152 0 return \&bad_set_validator;
153 }
154
155 # If there is at least one enabled value for this set, return the
156 # appropriate closure.
157
158 0 ▶ if ( ref $vs->{value_list} eq 'ARRAY' && @{$vs->{value_list}} )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vs->{"value_list"} eq "ARRAY" and @{$vs->{"value_list"};}) 0 0 0
Condition0%0/3
ref $vs->{"value_list"} eq "ARRAY" and @{$vs->{"value_list"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $vs->{"value_list"} eq "ARRAY" and @{$vs->{"value_list"};}
ref $vs->{"value_list"} eq "ARRAY" @{$vs->{"value_list"};}
Truth table0%0/3
A and B
A= ref $vs->{"value_list"} eq "ARRAY"B= @{$vs->{"value_list"};}
ABresult
0X0
100
111
159 {
160 0 return HTTP::Validate::ENUM_VALUE( @{$vs->{value_list}} );
161 }
162
163 # Otherwise, return a reference to a routine which will always return an
164 # error.
165
166 0 return \&bad_set_validator;
167 }
168  
169  
170 sub bad_set_validator {
171  
172 0 ▶ return { error => "No valid values have been defined for {param}." };
Subroutine bad_set_validator: not called CC=1 SCAR=6.9
Pod undocumented
173 }
174  
175  
176 # document_set ( set_name )
177 #
178 # Return a string in Pod format documenting the values that were assigned to
179 # this set.
180  
181 sub document_set {
182  
183 0 ▶ my ($self, $set_name) = @_;
Subroutine document_set: not called CC=7 SCAR=40.3
Pod undocumented
184
185 # Look up a set object using the given name. If none could be found,
186 # return an explanatory message.
187
188 0 my $vs = $self->{set}{$set_name};
189
190 0 ▶ return "=over\n\n=item I<Could not find the specified set>\n\n=back"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vs eq "Web::DataService::Set" 0 0 0
191 unless ref $vs eq 'Web::DataService::Set';
192
193 0 my @values; @values = grep { ! $vs->{value}{$_}{undocumented} } @{$vs->{value_list}}
194 0 ▶ if ref $vs->{value_list} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $vs->{"value_list"} eq "ARRAY" 0 0 0
195
196 0 ▶ return "=over\n\n=item I<The specified set is empty>\n\n=back"
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 0 0
197 unless @values;
198
199 # Now return the documentation in Pod format.
200
201 0 my $doc = "=over\n\n";
202
203 0 foreach my $name ( @values )
204 {
205 0 my $rec = $vs->{value}{$name};
206
207 0 $doc .= "=item $rec->{value}\n\n";
208 0 ▶ $doc .= "$rec->{doc_string}\n\n" if defined $rec->{doc_string} && $rec->{doc_string} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $rec->{"doc_string"} and $rec->{"doc_string"} ne "" 0 0 0
Condition0%0/3
defined $rec->{"doc_string"} and $rec->{"doc_string"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rec->{"doc_string"} and $rec->{"doc_string"} ne ""
defined $rec->{"doc_string"} $rec->{"doc_string"} ne ""
Truth table0%0/3
A and B
A= defined $rec->{"doc_string"}B= $rec->{"doc_string"} ne ""
ABresult
0X0
100
111
209 }
210
211 0 $doc .= "=back";
212
213 0 return $doc;
214 }
215  
216  
217 # list_set_values ( set_name )
218 #
219 # Return a list of the documented values defined for the specified set.
220  
221 sub list_set_values {
222
223 1 ▶ my ($ds, $name) = @_;
Subroutine list_set_values: called CC=3 SCAR=11.2
Pod undocumented
224
225 1 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1 1
226
227 1 my $set = $ds->{set}{$name};
228
229 1 ▶ return unless ref $set eq 'Web::DataService::Set';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set eq "Web::DataService::Set" 0 1 1
230 1 return grep { ! $set->{value}{$_}{undocumented} } @{$set->{value_list}};
231 }
232  
233  
234 # set_values ( set_name )
235 #
236 # Return a list of records representing the values defined for the specified
237 # set.
238  
239 sub set_values {
240
241 0 ▶ my ($ds, $name) = @_;
Subroutine set_values: not called CC=6 SCAR=37.4
Pod undocumented
242
243 0 my $set = $ds->{set}{$name};
244
245 0 ▶ croak "set_values: set '$name' not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set eq "Web::DataService::Set" 0 0 0
246 unless ref $set eq 'Web::DataService::Set';
247
248 0 my @list;
249
250 0 foreach my $v ( @{$set->{value_list}} )
251 {
252 0 ▶ next if $set->{value}{$v}{undocumented};
Branch: 1 branch
BranchTrueFalseTotal
if $set->{"value"}{$v}{"undocumented"} 0 0 0
253
254 0 my $sr = $set->{value}{$v};
255 0 my $r = { value => $sr->{value} };
256 0 ▶ $r->{maps_to} = $sr->{maps_to} if defined $sr->{maps_to};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sr->{"maps_to"} 0 0 0
257 0 ▶ $r->{doc_string} = $sr->{doc_string} if defined $sr->{doc_string};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sr->{"doc_string"} 0 0 0
258
259 0 push @list, $r;
260 }
261
262 0 return @list;
263 }
264  
265  
266 # map_value ( set_name, value )
267 #
268 # If the given value is a member of the named set, then return the 'maps_to'
269 # value if any was defined. Return undef otherwise.
270  
271 sub map_value {
272
273 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
274
275 0 ▶ my ($ds, $name, $value) = @_;
Subroutine map_value: not called CC=2 SCAR=17.9
Pod undocumented
276
277 0 my $set = $ds->{set}{$name};
278
279 0 ▶ croak "set_values: set '$name' not found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set eq "Web::DataService::Set" 0 0 0
280 unless ref $set eq 'Web::DataService::Set';
281
282 0 return $set->{value}{$value}{maps_to};
283 }
284  
285 2 1;
« lib/Web/DataService/IDocument.pm ↑ Summary blib/lib/Web/DataService/IRequest.pm »