lib/AutoCode/Schema.pm

filter: Statementstmt 59.4% 69 / 116 Branchbran 37.5% 12 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 68.4% 13 / 19 Podpod 0.0% 0 / 9 totaltotal 52.4% 95 / 181 CC 28 SCAR 27.9
CC
28
Cov
54%
_initialize
33.3
dependence
30.0
fks
20.0
CRAP 16.3
« lib/AutoCode/SymbolTableUtils.pm ↑ Summary blib/lib/AutoCode/MarshalRoot.pm »
1 package AutoCode::Schema;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use AutoCode::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our @ISA=qw(AutoCode::Root);
6 5 our %PLURALS;
7 5 ▶use AutoCode::ModuleModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use AutoCode::AccessorMaker(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 '$'=>[qw(plurals modules package_prefix)],
10 '@'=>[qw(friendship)]
11 5 );
12 5 ▶use AutoCode::Friendship;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use AutoCode::Plurality;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶sub _initialize {
Subroutine _initialize: called CC=14 SCAR=35.0
17 5 my ($self, @args)=@_;
18 5 $self->SUPER::_initialize(@args);
19
20 5 my ($modules, $package_prefix, $plurals, $modules_type_grouped)=
21 $self->_rearrange(
22 [qw(MODULES PACKAGE_PREFIX PLURALS modules_type_grouped)], @args);
23
24 5 ▶ (ref($modules) eq 'HASH') and $self->modules($modules);
Branch: 1 branch
BranchTrueFalseTotal
ref $modules eq "HASH" and $self->modules($modules) 5 0 5
25 5 ▶ if(defined $modules_type_grouped){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $modules_type_grouped) 0 5 5
26 # print STDERR "FOUND modules_type_grouped\n";
27 0 ▶ $modules= {} unless defined $modules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $modules 0 0 0
28 0 ▶ $self->throw('modules_type_grouped must be a hash ref')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $modules_type_grouped eq "HASH" 0 0 0
29 unless ref($modules_type_grouped) eq 'HASH';
30 0 foreach my $module_name(keys %$modules_type_grouped){
31 $self->throw("one key '$module_name' in modules_type_grouped has been defined\nCURRENT KEYS:\t". join("\t", keys %$modules))
32 0 ▶ if exists $modules->{$module_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $modules->{$module_name} 0 0 0
33 0 my $module_type_grouped = $modules_type_grouped->{$module_name};
34 0 my %module;
35 0 foreach my $field_type (keys %$module_type_grouped){
36 0 foreach (@{$module_type_grouped->{$field_type}}){
37 0 $module{$_}= $field_type;
38 }
39 }
40 0 $modules->{$module_name}= \%module;
41 }
42 0 $self->modules($modules);
43 }
44
45 5 our $FRIENDSHIP_TYPE='~friends';
46 5 ▶ if(exists $modules->{$FRIENDSHIP_TYPE}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modules->{$AutoCode::Schema::FRIENDSHIP_TYPE}) 5 0 5
47 5 my $friends=$modules->{$FRIENDSHIP_TYPE};
48 5 delete $modules->{$FRIENDSHIP_TYPE};
49 5 foreach my $friend(keys %$friends){
50 5 my @peers=split /-/, $friend;
51 5 my $extras=$friends->{$friend};
52 5 $extras=~ s/;$//;
53 5 my @extras=split /;/, $extras;
54 5 my $friendship = AutoCode::Friendship->new(
55 -peer_string => $friend,
56 -peers => \@peers,
57 -extras => \@extras
58 );
59 5 $self->add_friendship($friendship);
60 }
61 }
62  
63 5 $self->package_prefix($package_prefix);
64 5 $self->plurals({});
65 5 ▶ if(defined $plurals){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plurals) 5 0 5
66 5 ▶ if(ref($plurals) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $plurals eq 'HASH') { } 5 0 5
67 # not directly assign to the package variable, avoiding overwrite
68 5 $self->plurals($plurals);
69 5 foreach (keys %$plurals){
70 5 AutoCode::Plurality->add_plural($_, $plurals->{$_});
71 }
72 }else{
73 0 $self->throw("plurals must be a hash reference");
74 }
75 } # else{ %PLURALS=();} wrongly to initialize the package variable.
76  
77 }
78  
79 # Only be invoked by ModuleModel.
80 #
81 14 ▶sub _get_module_definition {
Subroutine _get_module_definition: called CC=1 SCAR=0.0
82 14 my ($self, $type)=@_;
83 14 $self->_check_type($type);
84 14 return $self->modules->{$type};
85 }
86  
87 0 ▶sub get_all_types {
Subroutine get_all_types: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self=shift;
89 0 return grep !/^\W/, keys %{$self->modules};
90 }
91  
92 0 ▶sub get_friends {
Subroutine get_friends: not called CC=2 SCAR=17.9
Pod undocumented
93 0 my $friends=shift->modules->{'~friends'};
94 0 ▶ return (defined $friends)? @$friends : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $friends ? : 0 0 0
95 }
96  
97 0 ▶sub dependence {
Subroutine dependence: not called CC=5 SCAR=34.0
Pod undocumented
98 0 my $self=shift;
99 0 my %dependance=();
100 0 my %modules=%{$self->modules};
101 0 my @types = keys %modules;
102 0 foreach my $type(@types){
103 0 my $module = $self->get_module_model($type);
104 0 foreach my $tag ($module->get_all_value_attributes){
105 0 my ($context, $kind, $content, $required) =
106 $module->_classify_value_attribute($tag);
107 0 ▶ if($kind eq 'M'){
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "M") 0 0 0
108 0 ▶ $dependance{$type} = {} unless exists $dependance{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dependance{$type} 0 0 0
109 0 $dependance{$type}->{$content} = [$context, $tag];
110 }
111 }
112 }
113  
114 0 return %dependance;
115 }
116  
117 18 ▶sub find_friends {
Subroutine find_friends: called CC=4 SCAR=14.0
Pod undocumented
118 18 my ($self, $module)=@_;
119 18 ▶ $module = ref($module) || $module;
Condition33%1/3
ref $module || $module
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $module || $module
ref $module $module
Truth table33%1/3
A || B
A= ref $moduleB= $module
ABresult
1X1
011
000
120 18 my @friends; # to return
121 18 my @friendship=$self->get_friendships;
122 18 foreach my $friendship ($self->get_friendships){
123 18 ▶ if(grep /^$module$/, $friendship->get_peers){
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$module$/, $friendship->get_peers) 8 10 18
124 8 push @friends, grep !/^$module$/, $friendship->get_peers;
125 }
126 }
127 18 return @friends;
128 }
129  
130 0 ▶sub has_a {
Subroutine has_a: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my ($self, $type)=@_;
132 0 my %dependence = $self->dependence;
133 0 return ${$dependence{$type}};
134 }
135  
136 0 ▶sub fks {
Subroutine fks: not called CC=4 SCAR=30.0
Pod undocumented
137 0 my ($self)=@_;
138 0 my %has_a=$self->dependence;
139 0 my %fks;
140 0 foreach my $type(keys %has_a){
141 0 my %type=%{$has_a{$type}};
142 0 foreach(keys %type){
143 0 ▶ $fks{$_}={} unless exists $fks{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fks{$_} 0 0 0
144 0 $fks{$_}->{$type}=$type{$_};
145 }
146 }
147 0 return %fks;
148 }
149  
150 5 our %MODULE_MODELS;
151 36 ▶sub get_module_model {
Subroutine get_module_model: called CC=2 SCAR=6.9
Pod undocumented
152 36 my ($self, $type)=@_;
153 36 ▶ return $MODULE_MODELS{$type} if exists $MODULE_MODELS{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $AutoCode::Schema::MODULE_MODELS{$type} 22 14 36
154 14 my $model=AutoCode::ModuleModel->new(
155 -schema => $self,
156 -type => $type
157 );
158 14 $MODULE_MODELS{$type} = $model;
159 14 return $model;
160 }
161  
162 14 ▶sub _check_type {
Subroutine _check_type: called CC=2 SCAR=7.2
163 14 my ($self, $type)=@_;
164 $self->throw("[$type] does not exist in the schema")
165 14 ▶ unless exists $self->modules->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->modules->{$type} 0 14 14
166 }
167  
168 28 ▶sub get_plural {
Subroutine get_plural: called CC=2 SCAR=6.9
Pod undocumented
169 28 my ($self, $singular)=@_;
170 28 my $plurals=$self->plurals;
171 28 ▶ return (exists $plurals->{$singular})?$plurals->{$singular}:"${singular}s";
Branch: 1 branch
BranchTrueFalseTotal
exists $plurals->{$singular} ? : 14 14 28
172 }
173  
174 0 ▶sub ref_plural {
Subroutine ref_plural: not called CC=1 SCAR=6.9
Pod undocumented
175 0 my ($self, $singular)=@_;
176 0 return [$singular, $self->get_plural($singular)];
177 }
178  
179 5 1;
« lib/AutoCode/SymbolTableUtils.pm ↑ Summary blib/lib/AutoCode/MarshalRoot.pm »