lib/AutoCode/AccessorMaker.pm

filter: Statementstmt 94.6% 264 / 279 Branchbran 70.0% 63 / 90 Conditioncond 47.0% 8 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 93.6% 44 / 47 Podpod 0.0% 0 / 5 totaltotal 84.8% 381 / 449 CC 62 SCAR 16.2
CC
62
Cov
87%
import
12.6
__ANON__
12.0
make_initialize
12.0
CRAP 5.1
« lib/AutoCode/ModuleModel.pm ↑ Summary lib/AutoCode/AttributeType.pm »
1 ###
2 # POD documents at the end of the file
3 ###
4 package AutoCode::AccessorMaker;
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 our $VERSION='0.01';
7 7 ▶use AutoCode::Root0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 our @ISA=qw(AutoCode::Root0);
9 # use AutoCode::Initializer;
10 7 ▶use AutoCode::SymbolTableUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 our %AUTO_ACCESSORS;
12  
13 7 our $VALID_ACCESSOR_NAME='[_a-z][_a-z0-9]+';
14 7 $VALID_ACCESSOR_NAME='[_a-zA-Z][_a-zA-Z0-9]+';
15  
16 34 ▶sub import {
Subroutine import: called CC=12 SCAR=25.3
17 34 my ($class, @args)=@_;
18 34 my $self = $class->new;
19 34 ▶ my $caller = ref(caller) || caller;
Condition33%1/3
ref caller || caller
l!l&&r!l&&!r
0340
MC/DC0%0/2
ref caller || caller
ref caller caller
Truth table33%1/3
A || B
A= ref callerB= caller
ABresult
1X1
011
000
20 34 my %args=@args;
21
22 34 ▶ if(exists $args{'$'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"\$"}) 27 7 34
23 27 foreach ($class->_scalar_to_array($args{'$'})){
24 79 $self->make_scalar_accessor($_, $caller);
25 }
26 }
27  
28 34 ▶ if(exists $args{'@'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"\@"}) 17 17 34
29 17 foreach($class->_scalar_to_array($args{'@'})){
30 52 $self->make_array_accessor($_, $caller);
31 }
32 }
33  
34 34 ▶ if(exists $args{'%'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"%"}) 1 33 34
35 1 foreach($class->_scalar_to_array($args{'%'})){
36 1 $self->make_hash_accessor($_, $caller);
37 }
38 }
39  
40 34 ▶ if(exists $args{_initialize} or exists $args{_digest_args}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"_initialize"} or exists $args{"_digest_args"}) 6 28 34
Condition66%2/3
exists $args{"_initialize"} or exists $args{"_digest_args"}
l!l&&r!l&&!r
6028
MC/DC50%1/2
exists $args{"_initialize"} or exists $args{"_digest_args"}
exists $args{"_initialize"} exists $args{"_digest_args"}
Truth table66%2/3
A or B
A= exists $args{"_initialize"}B= exists $args{"_digest_args"}
ABresult
1X1
011
000
41 6 my $initializer=$self;
42
43 6 ▶ if(exists $args{_initialize}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"_initialize"}) 6 0 6
44 6 $initializer->_make_initialize_by_hash(\%args, $caller);
45 }
46 6 ▶ if(exists $args{_digest_args}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"_digest_args"}) 0 6 6
47 0 $initializer->_make_digest_args_by_hash(\%args, $caller);
48 }
49 }
50 }
51  
52 45 ▶sub _scalar_to_array {
Subroutine _scalar_to_array: called CC=3 SCAR=11.2
53 45 my ($class, $scalar)=@_;
54 45 my $ref=ref($scalar);
55 45 ▶ if($ref eq'ARRAY'){
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 44 1 45
elsif ($ref eq '') { } 1 0 1
56 44 return @$scalar;
57 }elsif($ref eq ''){
58 1 return ($scalar);
59 }else{
60 0 $class->throw("ref [$ref] is neither nothing nor ARRAY");
61 }
62 }
63  
64 # This method is only invoked by make_scalar_accessor and make_array_accessor
65 # While subroutine defined the argument of those two method abovementioned.
66 # This most hacky part is caller(2); that mean the first immedicate package
67 # after this Module.
68 193 ▶sub __accessor_to_glob {
Subroutine __accessor_to_glob: called CC=6 SCAR=18.2
69 193 my ($self, $accessor, $pkg)=@_;
70 193 ▶ defined $accessor or $self->throw("method_name needed as 2nd arg");
Branch: 1 branch
BranchTrueFalseTotal
defined $accessor or $self->throw("method_name needed as 2nd arg") 193 0 193
71 193 ▶ my $singular = (ref($accessor) eq 'ARRAY')? $accessor->[0]: $accessor;
Branch: 1 branch
BranchTrueFalseTotal
ref $accessor eq 'ARRAY' ? : 24 169 193
72 # According to the specification of AutoCode, upper letter are not allowed
73 # in the names of methods which are automatically generated by this system.
74 193 ▶ $self->throw("'$singular' method name must match /^$VALID_ACCESSOR_NAME\$/")
Branch: 1 branch
BranchTrueFalseTotal
unless $singular =~ /^$AutoCode::AccessorMaker::VALID_ACCESSOR_NAME$/ 0 193 193
75 unless $singular =~ /^$VALID_ACCESSOR_NAME$/;
76 193 if(0){ # For debug
77 print "$_\t". (caller($_))[0]."\n" foreach(0..3);
78 $self->throw("");
79 }
80 193 ▶ $pkg ||= (caller(2))[0]; # This line will definitely assign a value.
Condition33%1/3
$pkg ||= (caller 2)[0]
l!l&&r!l&&!r
19300
MC/DC0%0/2
$pkg ||= (caller 2)[0]
$pkg (caller 2)[0]
Truth table33%1/3
A ||= B
A= $pkgB= (caller 2)[0]
ABresult
1X1
011
000
81  
82 # This typeglob is meaningful for both scalar and array accessors.
83 # For scalar, it means the same as the real typeglob;
84 # for array, there is no such method with exact method, but a symbol for
85 # these three methods.
86 193 my $typeglob="$pkg\::$singular";
87 193 ▶ unless(grep {$_ eq $typeglob} keys %AUTO_ACCESSORS){
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $typeglob;} keys %AutoCode::AccessorMaker::AUTO_ACCESSORS) 177 16 193
88 # push @{$self->{AUTO_ACCESSORS_SLOT}}, $typeglob;
89 177 $AUTO_ACCESSORS{$typeglob}=1;
90 }
91 193 my $slot="$pkg\::_auto_accessors::$singular";
92 193 return ($accessor, $pkg, $typeglob, $slot);
93
94 }
95  
96 118 ▶sub make_scalar_accessor {
Subroutine make_scalar_accessor: called CC=3 SCAR=11.0
Pod undocumented
97 118 my $self=shift;
98 118 my ($accessor, $pkg, $typeglob, $slot) = $self->__accessor_to_glob(@_);
99
100 118 $typeglob="$pkg\::$accessor";
101 118 $slot="$pkg\::$accessor\_\$";
102 118 $self->debug("making a scalar accessor [$typeglob]");
103  
104 118 ▶ return if(AutoCode::SymbolTableUtils::CODE_exists_in_ST($typeglob));
Branch: 1 branch
BranchTrueFalseTotal
if AutoCode::SymbolTableUtils::CODE_exists_in_ST($typeglob) 10 108 118
105 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 108 ▶ return if defined &$typeglob;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$typeglob 0 108 108
107 446 ▶ *$typeglob =sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
108 446 my $self=shift;
109 446 ▶ $self->{$slot}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 107 339 446
110 446 return $self->{$slot};
111 108 };
112 }
113  
114 150 ▶sub _deref_plural {
Subroutine _deref_plural: called CC=2 SCAR=6.9
115 150 my ($self, $accessor)=@_;
116 150 ▶ (ref($accessor) eq 'ARRAY')? @$accessor: ($accessor, "${accessor}s");
Branch: 1 branch
BranchTrueFalseTotal
ref $accessor eq 'ARRAY' ? : 66 84 150
117 }
118  
119 66 ▶sub make_array_accessor {
Subroutine make_array_accessor: called CC=3 SCAR=11.0
Pod undocumented
120 66 my $self=shift;
121 66 my ($accessor, $pkg, $typeglob, $slot)=$self->__accessor_to_glob(@_);
122 66 my ($singular, $plural) = $self->_deref_plural($accessor);
123 # $typeglob is useless here. So the 3 new method globs are composed here
124 66 my $add_method="$pkg\::add_$singular";
125 66 my $get_method="$pkg\::get_$plural";
126 66 my $remove_method="$pkg\::remove_$plural";
127 66 foreach $typeglob($add_method, $get_method, $remove_method){
128 190 ▶ return if(AutoCode::SymbolTableUtils::CODE_exists_in_ST($typeglob));
Branch: 1 branch
BranchTrueFalseTotal
if AutoCode::SymbolTableUtils::CODE_exists_in_ST($typeglob) 4 186 190
129 }
130 62 $slot="$pkg\::$singular\_\%";
131 62 $self->_make_array_add($add_method, $slot);
132 62 $self->_make_array_get($get_method, $slot);
133 62 $self->_make_array_remove($remove_method, $slot, $get_method);
134 }
135  
136 62 ▶sub _make_array_add {
Subroutine _make_array_add: called CC=1 SCAR=0.0
137 62 my ($self, $glob, $slot)=@_;
138 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 120 ▶ *$glob=sub{
Subroutine __ANON__: called CC=4 SCAR=14.1
140 120 ▶ my $self=shift; return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 120 120
141
142 120 foreach my $value(@_){
143 # Avoid duplicates
144 120 ▶ next if grep /^$value$/, @{$self->{$slot}};
Branch: 1 branch
BranchTrueFalseTotal
if grep /^$value$/, @{$$self{$slot};} 0 120 120
145 120 push @{$self->{$slot}}, $value;
146 }
147 62 };
148 }
149  
150 62 ▶sub _make_array_get {
Subroutine _make_array_get: called CC=1 SCAR=0.0
151 62 my ($self, $glob, $slot)=@_;
152 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 201 ▶ *$glob=sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
154 201 my $self=shift;
155 201 ▶ return @{$self->{$slot}} if exists $self->{$slot};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$slot} 150 51 201
156 51 return ();
157 62 };
158 }
159  
160 62 ▶sub _make_array_remove {
Subroutine _make_array_remove: called CC=1 SCAR=0.0
161 62 my ($self, $glob, $slot, $get_method)=@_;
162 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 13 ▶ *$glob=sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
164 13 my $self=shift;
165 13 my @olds=&{$get_method}($self);
166 13 $self->{$slot}=[];
167 13 return @olds;
168 62 };
169 }
170  
171 9 ▶sub make_hash_accessor {
Subroutine make_hash_accessor: called CC=3 SCAR=11.0
Pod undocumented
172 9 my $self=shift;
173 9 my ($accessor, $pkg, $typeglob, $slot)=$self->__accessor_to_glob(@_);
174 9 my ($singular, $plural) =$self->_deref_plural($accessor);
175
176 9 $slot="$pkg\::$singular\_\%";
177 9 my $add_method="$pkg\::add_$singular";
178 9 my $get_method="$pkg\::get_$plural";
179 9 my $remove_method="$pkg\::remove_$plural";
180 {
181 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
182 9 foreach $typeglob($add_method, $get_method, $remove_method){
183 23 ▶ return if defined &$typeglob;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$typeglob 2 21 23
184 }
185 }
186  
187 7 $self->_make_hash_add($add_method, $slot);
188 7 $self->_make_hash_get($get_method, $slot);
189 7 $self->_make_hash_remove($remove_method, $slot, $get_method);
190 }
191  
192 7 ▶sub _make_hash_add {
Subroutine _make_hash_add: called CC=1 SCAR=0.0
193 7 my ($self, $glob, $slot)=@_;
194 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
195 3 ▶ *$glob=sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
196 3 ▶ my $self=shift; return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
197 3 ▶ $self->{$slot}={} unless exists $self->{$slot};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$slot} 1 2 3
198 3 my $key=shift;
199 3 $self->{$slot}->{$key}=shift;
200 3 1;
201 7 };
202 }
203  
204 7 ▶sub _make_hash_get {
Subroutine _make_hash_get: called CC=1 SCAR=0.0
205 7 my ($self, $glob, $slot)=@_;
206 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
207 4 ▶ *$glob=sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
208 4 my $self=shift;
209 4 ▶ if(exists $self->{$slot} && defined $self->{$slot}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$slot} and defined $self->{$slot}) { } 3 1 4
Condition66%2/3
exists $self->{$slot} and defined $self->{$slot}
!ll&&!rl&&r
103
MC/DC50%1/2
exists $self->{$slot} and defined $self->{$slot}
exists $self->{$slot} defined $self->{$slot}
Truth table66%2/3
A and B
A= exists $self->{$slot}B= defined $self->{$slot}
ABresult
0X0
100
111
210 3 return %{$self->{$slot}};
211 }else{
212 1 $self->{$slot}={};
213 1 return ();
214 }
215 7 };
216 }
217  
218 7 ▶sub _make_hash_remove {
Subroutine _make_hash_remove: called CC=1 SCAR=2.1
219 7 my ($self, $glob, $slot)=@_;
220 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 0 ▶ *$glob=sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
222 0 my $self=shift;
223 0 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
224 0 ▶ delete $self->{$slot}->{shift} while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { delete $self->{$slot}{"shift"} } 0 0 0
225 }else{
226 0 $self->{$slot}={};
227 }
228 7 };
229 }
230  
231 0 ▶sub make_initialize {
Subroutine make_initialize: not called CC=3 SCAR=24.8
Pod undocumented
232 0 my $self=shift;
233 0 my %args=@_;
234 0 ▶ my $scalar_accessors=$args{'$'} if exists $args{'$'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"\$"} 0 0 0
235 0 ▶ my $array_accessors=$args{'@'} if exists $args{'@'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"\@"} 0 0 0
236 0 AutoCode::Initialzer->make_initialize_by_hash(\@_);
237 }
238  
239 ### From Initializer
240  
241 24 ▶sub _compose_source {
Subroutine _compose_source: called CC=6 SCAR=18.1
242 24 my ($self, $hash, $pkg)=@_;
243 # $self->throw('pkg is required') unless defined $pkg;
244 24 my @scalar_attrs;
245 24 ▶ if(exists $hash->{'$'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"\$"}) 24 0 24
246 24 push @scalar_attrs, @{$hash->{'$'}};
247 }
248 24 my @array_attrs;
249 24 ▶ if(exists $hash->{'@'}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"\@"}) 24 0 24
250 24 push @array_attrs, @{$hash->{'@'}};
251 }
252
253 24 my $source = 'sub { my($dummy, @args)=@_;'."\n";
254 # The line below is for debug. It will run only when the made module is working
255 # $source .= "print 'I am in _init of '. ref(\$dummy) . '_____';";
256
257 # $source .= "\$dummy->SUPER::_initialize(\@args);\n";
258 24 ▶ if(@scalar_attrs || @array_attrs){
Branch: 1 branch
BranchTrueFalseTotal
if (@scalar_attrs or @array_attrs) 24 0 24
Condition33%1/3
@scalar_attrs or @array_attrs
l!l&&r!l&&!r
2400
MC/DC0%0/2
@scalar_attrs or @array_attrs
@scalar_attrs @array_attrs
Truth table33%1/3
A or B
A= @scalar_attrsB= @array_attrs
ABresult
1X1
011
000
259 24 $source .= 'my ('. join ',', map{"\$$_"} @scalar_attrs;
260 24 ▶ $source .= ', ' unless @scalar_attrs == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @scalar_attrs == 0 24 0 24
261 $source .= join ',', map{
262 24 '$'. ($self->_deref_plural($_))[1]}@array_attrs;
263 # "\$$_"} @array_attrs_plural;
264 24 $source .= ')='."\n".'$dummy->_rearrange([qw(';
265 24 $source .= join ' ', @scalar_attrs;
266 $source .= ' '. join ' ', map{
267 24 ($self->_deref_plural($_))[1]}@array_attrs;
268 # @array_attrs_plural;
269 24 $source .= ')], @args);'."\n";
270 24 map {$source .=
271 "defined \$$_ and \$dummy->$_(\$$_);\n"} @scalar_attrs;
272
273 # if the array ref is defined, assign the dereferenced into array,
274 # otherwise initialize the array by invoking remove_$plural
275 map {
276 24 my ($singular, $plural)= $self->_deref_plural($_);
277 # ($_, $schema->get_plural($_));
278 25 $source .= <<END_ACCESSORS;
279 if(ref(\$$plural) eq'ARRAY'){
280 \$dummy->add_$singular(\$_) foreach (\@{\$$plural});
281 }else{
282 \$dummy->remove_$plural;
283 }
284 END_ACCESSORS
285 }@array_attrs;
286 }
287 # The following 3 lines are to replace 'the not-working SUPER with eval'
288 # It spends almost a whole afternoon of the second day of 2004.
289 24 $source .= "no strict 'refs';\n";
290 24 $source .= 'my $super=AutoCode::Root::_find_super("'. $pkg .'", "_initialize");'."\n";
291 24 $source .= '&{$super. "::_initialize"}($dummy, @args);'."\n";
292
293 # $source .= "\$dummy->SUPER::_initialize(\@args);\n";
294 # $source .= "print '______' \. *{\$dummy->SUPER::_initialize} \. \"\\n\"";
295 24 $source .= '};'."\n";
296 24 $self->debug("$source");
297 24 return $source;
298 }
299  
300 24 ▶sub _make_initialize_by_hash {
Subroutine _make_initialize_by_hash: called CC=3 SCAR=11.0
301 24 my ($self, $hash, $pkg, $method)=@_;
302 24 my $source = $self->_compose_source($hash, $pkg);
303 24 ▶ $method ||= '_initialize';
Condition50%1/2
$method ||= "_initialize"
l!l
024
MC/DC0%0/1
$method ||= "_initialize"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
304 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
305 0 ▶ *{"$pkg\::$method"} = eval $source;
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
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=6 SCAR=18.0
Subroutine __ANON__: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=9 SCAR=22.0
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 11 branches
BranchTrueFalseTotal
defined $peer_string and $dummy->peer_string($peer_string) 6 0 6
if (ref $peers eq 'ARRAY') { } 6 0 6
if (ref $extras eq 'ARRAY') { } 6 0 6
defined $adaptor and $dummy->adaptor($adaptor) 0 5 5
defined $birthday and $dummy->birthday($birthday) 1 4 5
defined $nric and $dummy->nric($nric) 1 4 5
defined $last_name and $dummy->last_name($last_name) 3 2 5
defined $first_name and $dummy->first_name($first_name) 1 4 5
if (ref $aliases eq 'ARRAY') { } 0 5 5
if (ref $emails eq 'ARRAY') { } 1 4 5
defined $home_address and $dummy->home_address($home_address) 4 1 5
306 24 ▶ $self->throw( "Error when eval'ing _initialize\n$@") if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 24 24
307 }
308
309 0 ▶sub _make_digest_args_by_hash {
Subroutine _make_digest_args_by_hash: not called CC=1 SCAR=6.9
310 0 my ($self, $hash, $pkg)=@_;
311 0 $self->_make_initialize_by_hash($hash, $pkg, '_digest_args');
312 }
313  
314 18 ▶sub make_initialize_by_model {
Subroutine make_initialize_by_model: called CC=2 SCAR=6.9
Pod undocumented
315 18 my ($class, $model, $pkg)=@_;
316 18 my $schema=$model->schema;
317 18 my @scalar_attrs = $model->get_scalar_attributes;
318 18 my @array_attrs = $model->get_array_attributes;
319 18 my @array_attrs_plural= map {$schema->get_plural($_)} @array_attrs;
320 18 my %args=('$'=>\@scalar_attrs);
321 18 my @array_ones;
322 18 ▶ for(my $i=0; $i<@array_attrs; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @array_attrs) 14 18 32
323 14 push @array_ones, [$array_attrs[$i], $array_attrs_plural[$i]];
324 }
325 18 my %args=('$'=>\@scalar_attrs, '@'=> \@array_ones);
326 18 $class->_make_initialize_by_hash(\%args, $pkg);
327 }
328  
329 7 1;
330 __END__
« lib/AutoCode/ModuleModel.pm ↑ Summary lib/AutoCode/AttributeType.pm »