blib/lib/Rose/HTML/Object/Messages.pm

filter: Statementstmt 99.1% 242 / 244 Branchbran 87.5% 28 / 32 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 98.5% 66 / 67 Podpod 80.0% 8 / 10 totaltotal 94.4% 357 / 378 CC 24 SCAR 11.1
CC
24
Cov
96%
add_messages
10.3
add_message
9.4
message_names
3.0
CRAP 3.0
« blib/lib/Rose/HTML/Form/Field/TextArea.pm ↑ Summary blib/lib/Rose/HTML/Image.pm »
1 package Rose::HTML::Object::Messages;
2  
3 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 47 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 47 ▶use base 'Rose::HTML::Object::Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 47 our $VERSION = '0.618';
10  
11 47 our $Debug = 0;
12  
13 use Rose::Class::MakeMethods::Generic
14 47 ▶(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 47 inheritable_scalar =>
16 [
17 '_message_names',
18 'message_id_to_name_map',
19 'message_name_to_id_map',
20 ],
21 47 );
22  
23 BEGIN
24 47 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
25 47 __PACKAGE__->_message_names([]);
26 47 __PACKAGE__->message_id_to_name_map({});
27 47 __PACKAGE__->message_name_to_id_map({});
28 }
29  
30 sub init_export_tags
31 139 ▶{
Subroutine init_export_tags: called CC=1 SCAR=0.0
Pod undocumented
32 139 my($class) = shift;
33  
34 139 my $list = $class->message_names;
35  
36 $class->export_tags
37 (
38 all => $list,
39 6283 field => [ grep { /^FIELD_/ } @$list ],
40 6283 form => [ grep { /^FORM_/ } @$list ],
41 6283 date => [ grep { /^DATE_|_(?:YEAR|MONTH|DAY)$/ } @$list ],
42 6283 time => [ grep { /^TIME_|_(?:HOUR|MINUTE|SECOND)$/ } @$list ],
43 6283 email => [ grep { /^EMAIL_/ } @$list ],
44 6283 phone => [ grep { /^PHONE_/ } @$list ],
45 6283 number => [ grep { /^NUM_/ } @$list ],
46 6283 set => [ grep { /^SET_/ } @$list ],
47 139 string => [ grep { /^STRING_/ } @$list ],
48 );
49 }
50  
51 sub import
52 332 ▶{
Subroutine import: called CC=2 SCAR=6.9
53 332 my($class) = shift;
54  
55 332 $class->use_private_messages;
56 332 $class->init_export_tags;
57  
58 332 ▶ if($Rose::HTML::Object::Exporter::Target_Class)
Branch: 1 branch
BranchTrueFalseTotal
if ($Rose::HTML::Object::Exporter::Target_Class) { } 3 329 332
59 {
60 3 $class->SUPER::import(@_);
61 }
62 else
63 {
64 329 local $Rose::HTML::Object::Exporter::Target_Class = (caller)[0];
65 329 $class->SUPER::import(@_);
66 }
67 }
68  
69 47 our %Private;
70  
71 sub use_private_messages
72 4204 ▶{
Subroutine use_private_messages: called CC=2 SCAR=6.9
Pod undocumented
73 4204 my($class) = shift;
74  
75 4204 ▶ unless($Private{$class})
Branch: 1 branch
BranchTrueFalseTotal
unless ($Rose::HTML::Object::Messages::Private{$class}) 107 4097 4204
76 {
77 107 $Private{$class} = 1;
78  
79 # Make private copies of inherited data structures
80 # (shallow copy is sufficient)
81 107 $class->message_names([ $class->message_names ]);
82 107 $class->message_id_to_name_map({ %{$class->message_id_to_name_map} });
83 107 $class->message_name_to_id_map({ %{$class->message_name_to_id_map} });
84 }
85 }
86  
87 69 ▶sub message_id_exists { defined $_[0]->message_id_to_name_map->{$_[1]} }
Subroutine message_id_exists: called CC=1 SCAR=0.0
Pod documented
88 4 ▶sub message_name_exists { defined $_[0]->message_name_to_id_map->{$_[1]} }
Subroutine message_name_exists: called CC=1 SCAR=0.0
Pod documented
89  
90 sub message_names
91 593 ▶{
Subroutine message_names: called CC=3 SCAR=11.0
Pod documented
92 593 my($class) = shift;
93  
94 593 ▶ $class->_message_names(@_) if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 152 441 593
95  
96 593 ▶ wantarray ? @{$class->_message_names} :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 109 484 593
97 $class->_message_names;
98 }
99  
100 sub get_message_id
101 113 ▶{
Subroutine get_message_id: called CC=2 SCAR=6.9
Pod documented
102 113 my($class, $symbol) = @_;
103 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 113 my $const = "${class}::$symbol";
105 113 ▶ return &$const if(defined &$const);
Branch: 1 branch
BranchTrueFalseTotal
if defined &$const 111 2 113
106 2 return undef;
107 }
108  
109 sub message_ids
110 6 ▶{
Subroutine message_ids: called CC=2 SCAR=6.9
Pod documented
111 6 my($class) = shift;
112 6 my $map = $class->message_id_to_name_map;
113  
114 return wantarray ?
115 604 (sort { $a <=> $b } keys %$map) :
116 6 ▶ [ sort { $a <=> $b } keys %$map ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 2 6
117 }
118  
119 sub get_message_name
120 471 ▶{
Subroutine get_message_name: called CC=1 SCAR=0.0
Pod documented
121 47 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 471 return $_[0]->message_id_to_name_map->{$_[1]};
123 }
124  
125 sub add_message
126 3763 ▶{
Subroutine add_message: called CC=9 SCAR=22.4
Pod documented
127 3763 my($class, $name, $id) = @_;
128  
129 3763 $class->use_private_messages;
130  
131 3763 ▶ unless($class->imported($name))
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->imported($name)) 3282 481 3763
132 {
133 3282 ▶ if(exists $class->message_name_to_id_map->{$name} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $class->message_name_to_id_map->{$name} and $class->message_name_to_id_map->{$name} != $id) 0 3282 3282
Condition33%1/3
exists $class->message_name_to_id_map->{$name} and $class->message_name_to_id_map->{$name} != $id
!ll&&!rl&&r
328200
MC/DC0%0/2
exists $class->message_name_to_id_map->{$name} and $class->message_name_to_id_map->{$name} != $id
exists $class->message_name_to_id_map->{$name} $class->message_name_to_id_map->{$name} != $id
Truth table33%1/3
A and B
A= exists $class->message_name_to_id_map->{$name}B= $class->message_name_to_id_map->{$name} != $id
ABresult
0X0
100
111
134 $class->message_name_to_id_map->{$name} != $id)
135 {
136 croak "Could not add message '$name' - a message with that name already exists ",
137 0 '(', $class->message_name_to_id_map->{$name}, ')';
138 }
139  
140 3282 ▶ if(exists $class->message_id_to_name_map->{$id} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $class->message_id_to_name_map->{$id} and $class->message_id_to_name_map->{$id} ne $name) 2 3280 3282
Condition66%2/3
exists $class->message_id_to_name_map->{$id} and $class->message_id_to_name_map->{$id} ne $name
!ll&&!rl&&r
328002
MC/DC50%1/2
exists $class->message_id_to_name_map->{$id} and $class->message_id_to_name_map->{$id} ne $name
exists $class->message_id_to_name_map->{$id} $class->message_id_to_name_map->{$id} ne $name
Truth table66%2/3
A and B
A= exists $class->message_id_to_name_map->{$id}B= $class->message_id_to_name_map->{$id} ne $name
ABresult
0X0
100
111
141 $class->message_id_to_name_map->{$id} ne $name)
142 {
143 croak "Could not add message '$name' - a message with the id $id already exists ",
144 2 '(', $class->message_id_to_name_map->{$id}, ')';
145 }
146 }
147  
148 MAKE_CONSTANT:
149 {
150 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 3761 my $const = "${class}::$name";
152 3761 ▶ unless($class->can($name) || defined &$const)
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can($name) or defined &$const) 19 3742 3761
Condition66%2/3
$class->can($name) or defined &$const
l!l&&r!l&&!r
3742019
MC/DC50%1/2
$class->can($name) or defined &$const
$class->can($name) defined &$const
Truth table66%2/3
A or B
A= $class->can($name)B= defined &$const
ABresult
1X1
011
000
153 {
154 0 ▶ *{"${class}::$name"} = sub() { $id };
Subroutine __ANON__: not called CC=1 SCAR=0.4
155  
156 #my $error;
157 #
158 #TRY:
159 #{
160 # local $@;
161 # eval "package $class; use constant $name => $id;";
162 # $error = $@;
163 #}
164 #
165 #croak "Could not create constant '$name' in $class - $error" if($error);
166 }
167 }
168  
169 3761 ▶ unless(exists $class->message_name_to_id_map->{$name})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $class->message_name_to_id_map->{$name}) 3280 481 3761
170 {
171 3280 push(@{$class->_message_names}, $name);
172 }
173  
174 3761 $class->message_id_to_name_map->{$id} = $name;
175 3761 $class->message_name_to_id_map->{$name} = $id;
176  
177 3761 return;
178 }
179  
180 sub add_messages
181 109 ▶{
Subroutine add_messages: called CC=10 SCAR=23.3
Pod documented
182 109 my($class) = shift;
183  
184 109 $class->use_private_messages;
185  
186 47 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 109 ▶ if(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 107 109
189 {
190 2 foreach my $name (@_)
191 {
192 4 $class->add_message($name, "${class}::$name"->());
193 }
194 }
195 else
196 {
197 107 foreach my $name (keys %{"${class}::"})
198 {
199 6090 my $fq_name = "${class}::$name";
200  
201 6090 ▶ next unless(defined *{$fq_name}{'CODE'} && $name =~ /^[A-Z0-9_]+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$fq_name;}{"CODE"} and $name =~ /^[A-Z0-9_]+$/ 2352 3738 6090
Condition100%3/3
defined *{$fq_name;}{"CODE"} and $name =~ /^[A-Z0-9_]+$/
!ll&&!rl&&r
101313393738
MC/DC100%2/2
defined *{$fq_name;}{"CODE"} and $name =~ /^[A-Z0-9_]+$/
defined *{$fq_name;}{"CODE"} $name =~ /^[A-Z0-9_]+$/
Truth table100%3/3
A and B
A= defined *{$fq_name;}{"CODE"}B= $name =~ /^[A-Z0-9_]+$/
ABresult
0X0
100
111
202  
203 3738 my $code = $class->can($name);
204  
205 # Skip it if it's not a constant
206 3738 ▶ next unless(defined prototype($code) && !length(prototype($code)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined prototype $code and not length prototype $code 0 3738 3738
Condition33%1/3
defined prototype $code and not length prototype $code
!ll&&!rl&&r
003738
MC/DC0%0/2
defined prototype $code and not length prototype $code
defined prototype $code not length prototype $code
Truth table33%1/3
A and not(B)
A= defined prototype $codeB= not length prototype $code
ABresult
0X0
100
111
207  
208 # Should not need this check?
209 3738 ▶ next if($name =~ /^(BEGIN|DESTROY|AUTOLOAD|TIE.*)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(BEGIN|DESTROY|AUTOLOAD|TIE.*)$/ 0 3738 3738
210  
211 3738 ▶ $Debug && warn "$class ADD $name = ", $code->(), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$Rose::HTML::Object::Messages::Debug and warn "$class ADD $name = ", &$code(), "\n" 0 3738 3738
212 3738 $class->add_message($name, $code->());
213 }
214 }
215 }
216  
217 #
218 # Messages
219 #
220  
221 47 ▶use constant CUSTOM_MESSAGE => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
222  
223 # Fields and labels
224 47 ▶use constant FIELD_LABEL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 47 ▶use constant FIELD_DESCRIPTION => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
226 47 ▶use constant FIELD_REQUIRED_GENERIC => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 47 ▶use constant FIELD_REQUIRED_LABELLED => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
228 47 ▶use constant FIELD_REQUIRED_SUBFIELD => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 47 ▶use constant FIELD_REQUIRED_SUBFIELDS => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 47 ▶use constant FIELD_PARTIAL_VALUE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 47 ▶use constant FIELD_INVALID_GENERIC => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 47 ▶use constant FIELD_INVALID_LABELLED => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 47 ▶use constant FIELD_LABEL_YEAR => 10_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 47 ▶use constant FIELD_LABEL_MONTH => 10_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 47 ▶use constant FIELD_LABEL_DAY => 10_002;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 47 ▶use constant FIELD_LABEL_HOUR => 10_003;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 47 ▶use constant FIELD_LABEL_MINUTE => 10_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 47 ▶use constant FIELD_LABEL_SECOND => 10_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 47 ▶use constant FIELD_ERROR_LABEL_YEAR => 11_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 47 ▶use constant FIELD_ERROR_LABEL_MONTH => 11_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 47 ▶use constant FIELD_ERROR_LABEL_DAY => 11_002;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 47 ▶use constant FIELD_ERROR_LABEL_HOUR => 11_003;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 47 ▶use constant FIELD_ERROR_LABEL_MINUTE => 11_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
246 47 ▶use constant FIELD_ERROR_LABEL_SECOND => 11_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
247  
248 47 ▶use constant FIELD_ERROR_LABEL_MINIMUM_DATE => 11_006;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 47 ▶use constant FIELD_ERROR_LABEL_MAXIMUM_DATE => 11_007;
Subroutine BEGIN: called CC=1 SCAR=0.0
250  
251 # Forms
252 47 ▶use constant FORM_HAS_ERRORS => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
253  
254 # Numerical messages
255 47 ▶use constant NUM_INVALID_INTEGER => 1300;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 47 ▶use constant NUM_INVALID_INTEGER_POSITIVE => 1301;
Subroutine BEGIN: called CC=1 SCAR=0.0
257 47 ▶use constant NUM_NOT_POSITIVE_INTEGER => 1302;
Subroutine BEGIN: called CC=1 SCAR=0.0
258 47 ▶use constant NUM_BELOW_MIN => 1303;
Subroutine BEGIN: called CC=1 SCAR=0.0
259 47 ▶use constant NUM_ABOVE_MAX => 1304;
Subroutine BEGIN: called CC=1 SCAR=0.0
260 47 ▶use constant NUM_INVALID_NUMBER => 1305;
Subroutine BEGIN: called CC=1 SCAR=0.0
261 47 ▶use constant NUM_INVALID_NUMBER_POSITIVE => 1306;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 47 ▶use constant NUM_NOT_POSITIVE_NUMBER => 1307;
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 # String messages
265 47 ▶use constant STRING_OVERFLOW => 1400;
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 # Date messages
268 47 ▶use constant DATE_INVALID => 1500;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 47 ▶use constant DATE_MIN_GREATER_THAN_MAX => 1501;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 # Time messages
272 47 ▶use constant TIME_INVALID => 1550;
Subroutine BEGIN: called CC=1 SCAR=0.0
273 47 ▶use constant TIME_INVALID_HOUR => 1551;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 47 ▶use constant TIME_INVALID_MINUTE => 1552;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 47 ▶use constant TIME_INVALID_SECONDS => 1553;
Subroutine BEGIN: called CC=1 SCAR=0.0
276 47 ▶use constant TIME_INVALID_AMPM => 1554;
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 # Email messages
279 47 ▶use constant EMAIL_INVALID => 1600;
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 # Phone messages
282 47 ▶use constant PHONE_INVALID => 1650;
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 # Set messages
285 47 ▶use constant SET_INVALID_QUOTED_STRING => 1700;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 47 ▶use constant SET_PARSE_ERROR => 1701;
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 47 ▶BEGIN { __PACKAGE__->add_messages }
Subroutine BEGIN: called CC=1 SCAR=0.0
289  
290 47 1;
291  
292 __END__
« blib/lib/Rose/HTML/Form/Field/TextArea.pm ↑ Summary blib/lib/Rose/HTML/Image.pm »