lib/DBIx/EAV/EntityType.pm

filter: Statementstmt 90.1% 128 / 142 Branchbran 78.4% 69 / 88 Conditioncond 66.6% 6 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 90.9% 20 / 22 Podpod 6.2% 1 / 16 totaltotal 79.5% 225 / 283 SCAR 40.5
CC
49
Cov
85%
register_relationship
13.6
attributes
6.0
has_attribute
6.0
CRAP 57.2
« lib/DBIx/EAV/Cursor.pm ↑ Summary lib/DBIx/EAV.pm »
1 package DBIx::EAV::EntityType;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7  
8 10 has 'core', is => 'ro', required => 1;
9 10 has 'id', is => 'ro', required => 1;
10 10 has 'name', is => 'ro', required => 1;
11 10 has 'parent', is => 'ro', predicate => 1;
12 10 has '_static_attributes', is => 'ro', init_arg => undef, lazy => 1, builder => 1;
13 4 ▶has '_attributes', is => 'ro', init_arg => 'attributes', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 45 ▶has '_relationships', is => 'ro', init_arg => undef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16  
17 sub _build__static_attributes {
18 20 ▶ my $self = shift;
Subroutine _build__static_attributes: called CC=1 SCAR=0.0
19 +{
20 67 map { $_ => {name => $_, is_static => 1} }
21 20 @{$self->core->table('entities')->columns}
22 }
23 }
24  
25 sub load {
26 4 ▶ my ($class, $row) = @_;
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
27 4 ▶ die "load() is a class method" if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 4 4
28  
29 4 my $self = $class->new($row);
30  
31 # load attributes
32 4 my $sth = $self->core->table('attributes')->select({ entity_type_id => $self->id });
33  
34 4 ▶ while (my $attr = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $sth->fetchrow_hashref) 12 4 16
35 12 $self->_attributes->{$attr->{name}} = $attr;
36 }
37  
38 # load relationships
39 4 $sth = $self->core->table('relationships')->select({ left_entity_type_id => $self->id });
40  
41 4 ▶ while (my $rel = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rel = $sth->fetchrow_hashref) 4 4 8
42 4 $self->_install_relationship($rel);
43 }
44  
45 4 $self;
46 }
47  
48 sub parents {
49 6 ▶ my ($self) = @_;
Subroutine parents: called CC=3 SCAR=11.0
Pod undocumented
50 6 ▶ return () unless $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent 0 6 6
51 6 my @parents;
52 6 my $parent = $self->parent;
53 6 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 9 6 15
54 9 push @parents, $parent;
55 9 $parent = $parent->parent;
56 }
57  
58 6 @parents;
59 }
60  
61 sub is_type($) {
62 51 ▶ my ($self, $type) = @_;
Subroutine is_type: called CC=4 SCAR=13.9
Pod undocumented
63 51 ▶ return 1 if $self->name eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq $type 46 5 51
64 5 foreach my $parent ($self->parents) {
65 6 ▶ return 1 if $parent->name eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->name eq $type 5 1 6
66 }
67 0 0;
68 }
69  
70  
71  
72  
73 sub has_attribute {
74 161 ▶ my ($self, $name) = @_;
Subroutine has_attribute: called CC=6 SCAR=17.9
Pod undocumented
75 161 ▶ return 1 if exists $self->_attributes->{$name} || exists $self->_static_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_attributes->{$name} or exists $self->_static_attributes->{$name} 130 31 161
Condition66%2/3
exists $self->_attributes->{$name} or exists $self->_static_attributes->{$name}
l!l&&r!l&&!r
130031
MC/DC50%1/2
exists $self->_attributes->{$name} or exists $self->_static_attributes->{$name}
exists $self->_attributes->{$name} exists $self->_static_attributes->{$name}
Truth table66%2/3
A or B
A= exists $self->_attributes->{$name}B= exists $self->_static_attributes->{$name}
ABresult
1X1
011
000
76 31 ▶ return 0 unless $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent 19 12 31
77  
78 12 my $parent = $self->parent;
79 12 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 16 1 17
80 16 ▶ return 1 if $parent->has_own_attribute($name);
Branch: 1 branch
BranchTrueFalseTotal
if $parent->has_own_attribute($name) 11 5 16
81 5 $parent = $parent->parent;
82 }
83  
84 1 0;
85 }
86  
87 sub has_static_attribute {
88 408 ▶ my ($self, $name) = @_;
Subroutine has_static_attribute: called CC=1 SCAR=0.0
Pod undocumented
89 408 exists $self->_static_attributes->{$name};
90 }
91  
92 sub has_own_attribute {
93 19 ▶ my ($self, $name) = @_;
Subroutine has_own_attribute: called CC=2 SCAR=6.9
Pod undocumented
94 19 ▶ exists $self->_attributes->{$name} || exists $self->_static_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->_attributes->{$name} or exists $self->_static_attributes->{$name} 5 14 19
95 }
96  
97 sub has_inherited_attribute {
98 5 ▶ my ($self, $name) = @_;
Subroutine has_inherited_attribute: called CC=4 SCAR=15.9
Pod undocumented
99 5 ▶ return 0 unless $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent 0 5 5
100 5 my $parent = $self->parent;
101 5 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 5 0 5
102 5 ▶ return 1 if exists $parent->_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parent->_attributes->{$name} 5 0 5
103 0 $parent = $parent->parent;
104 }
105 0 0;
106 }
107  
108 sub attribute {
109 782 ▶ my ($self, $name) = @_;
Subroutine attribute: called CC=5 SCAR=16.2
Pod undocumented
110  
111 # our attr
112 return $self->_attributes->{$name}
113 782 ▶ if exists $self->_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_attributes->{$name} 662 120 782
114  
115 return $self->_static_attributes->{$name}
116 120 ▶ if exists $self->_static_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_static_attributes->{$name} 7 113 120
117  
118 # parent attr
119 113 my $parent = $self->parent;
120 113 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 168 0 168
121 return $parent->_attributes->{$name}
122 168 ▶ if exists $parent->_attributes->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parent->_attributes->{$name} 113 55 168
123 55 $parent = $parent->parent;
124 }
125  
126 # unknown attribute
127 0 die sprintf("Entity '%s' does not have attribute '%s'.", $self->name, $name);
128 }
129  
130 sub attributes {
131 118 ▶ my ($self, %options) = @_;
Subroutine attributes: called CC=6 SCAR=18.0
Pod documented
132 118 my @items;
133  
134 # static
135 1 push @items, values %{$self->_static_attributes}
136 118 ▶ unless $options{no_static};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_static"} 1 117 118
137  
138 # own
139 118 push @items, values %{$self->_attributes}
140 118 ▶ unless $options{no_own};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_own"} 118 0 118
141  
142 # inherited
143 118 ▶ unless ($options{no_inherited}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_inherited"}) 118 0 118
144  
145 118 my $parent = $self->parent;
146 118 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 23 118 141
147 23 push @items, values %{$parent->_attributes};
148 23 $parent = $parent->parent;
149 }
150 }
151  
152 118 ▶ return $options{names} ? map { $_->{name} } @items : @items;
Branch: 1 branch
BranchTrueFalseTotal
$options{'names'} ? : 113 5 118
153 }
154  
155  
156  
157  
158 sub has_own_relationship {
159 88 ▶ my ($self, $name) = @_;
Subroutine has_own_relationship: called CC=1 SCAR=0.0
Pod undocumented
160 88 exists $self->_relationships->{$name};
161 }
162  
163 sub has_relationship {
164 579 ▶ my ($self, $name) = @_;
Subroutine has_relationship: called CC=5 SCAR=16.1
Pod undocumented
165 579 ▶ return 1 if exists $self->_relationships->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_relationships->{$name} 85 494 579
166 494 ▶ return 0 unless $self->has_parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_parent 435 59 494
167  
168 59 my $parent = $self->parent;
169 59 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 88 51 139
170 88 ▶ return 1 if $parent->has_own_relationship($name);
Branch: 1 branch
BranchTrueFalseTotal
if $parent->has_own_relationship($name) 8 80 88
171 80 $parent = $parent->parent;
172 }
173  
174 51 0;
175 }
176  
177 sub relationship {
178 157 ▶ my ($self, $name) = @_;
Subroutine relationship: called CC=4 SCAR=15.0
Pod undocumented
179  
180 # our
181 return $self->_relationships->{$name}
182 157 ▶ if exists $self->_relationships->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_relationships->{$name} 142 15 157
183  
184 # parent
185 15 my $parent = $self->parent;
186 15 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 15 0 15
187 return $parent->_relationships->{$name}
188 15 ▶ if exists $parent->_relationships->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parent->_relationships->{$name} 15 0 15
189 0 $parent = $parent->parent;
190 }
191  
192 # unknown
193 0 die sprintf("Entity '%s' does not have relationship '%s'.", $self->name, $name);
194 }
195  
196 sub relationships {
197 24 ▶ my ($self, %options) = @_;
Subroutine relationships: called CC=4 SCAR=14.1
Pod undocumented
198  
199 # ours
200 24 my @items = values %{$self->_relationships};
201  
202 # inherited
203 24 ▶ unless ($options{no_inherited}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_inherited"}) 24 0 24
204  
205 24 my $parent = $self->parent;
206 24 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 3 24 27
207 3 push @items, values %{$parent->_relationships};
208 3 $parent = $parent->parent;
209 }
210 }
211  
212 24 ▶ return $options{names} ? map { $_->{name} } @items : @items;
Branch: 1 branch
BranchTrueFalseTotal
$options{'names'} ? : 0 24 24
213 }
214  
215  
216 sub register_relationship {
217 34 ▶ my ($self, $reltype, $params) = @_;
Subroutine register_relationship: called CC=11 SCAR=26.1
Pod undocumented
218  
219 # scalar: entity
220 34 ▶ $params = { entity => $params } unless ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params 26 8 34
221  
222 # array: name => Entity [, incoming_name ]
223 34 ▶ if (ref $params eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "ARRAY") 8 26 34
224  
225 8 $params = {
226 name => $params->[0],
227 entity => $params->[1],
228 incoming_name => $params->[2],
229 };
230 }
231  
232 die sprintf("Error: invalid %s relationship for entity '%s': missing 'entity' parameter.", $reltype, $self->name)
233 34 ▶ unless $params->{entity};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"entity"} 0 34 34
234  
235 34 my $other_entity = $self->core->type($params->{entity});
236  
237 34 ▶ $params->{name} ||= $reltype =~ /_many$/ ? lc Lingua::EN::Inflect::PL($other_entity->name)
Branch: 1 branch
BranchTrueFalseTotal
$reltype =~ /_many$/ ? : 18 8 26
Condition66%2/3
$params->{"name"} ||= $reltype =~ /_many$/ ? lc Lingua::EN::Inflect::PL($other_entity->name) : lc $other_entity->name
l!l&&r!l&&!r
8260
MC/DC0%0/2
$params->{"name"} ||= $reltype =~ /_many$/ ? lc Lingua::EN::Inflect::PL($other_entity->name) : lc $other_entity->name
$params->{"name"} $reltype =~ /_many$/ ? lc Lingua::EN::Inflect::PL($other_entity->name) : lc $other_entity->name
Truth table66%2/3
A ||= B
A= $params->{"name"}B= $reltype =~ /_many$/ ? lc Lingua::EN::Inflect::PL($other_entity->name) : lc $other_entity->name
ABresult
1X1
011
000
238 : lc $other_entity->name;
239  
240 34 ▶ $params->{incoming_name} ||= $reltype eq 'many_to_many' ? lc Lingua::EN::Inflect::PL($self->name)
Branch: 1 branch
BranchTrueFalseTotal
$reltype eq 'many_to_many' ? : 10 16 26
Condition66%2/3
$params->{"incoming_name"} ||= $reltype eq "many_to_many" ? lc Lingua::EN::Inflect::PL($self->name) : lc $self->name
l!l&&r!l&&!r
8260
MC/DC0%0/2
$params->{"incoming_name"} ||= $reltype eq "many_to_many" ? lc Lingua::EN::Inflect::PL($self->name) : lc $self->name
$params->{"incoming_name"} $reltype eq "many_to_many" ? lc Lingua::EN::Inflect::PL($self->name) : lc $self->name
Truth table66%2/3
A ||= B
A= $params->{"incoming_name"}B= $reltype eq "many_to_many" ? lc Lingua::EN::Inflect::PL($self->name) : lc $self->name
ABresult
1X1
011
000
241 : lc $self->name;
242  
243 my %rel = (
244 left_entity_type_id => $self->id,
245 right_entity_type_id => $other_entity->id,
246 name => $params->{name},
247 incoming_name => $params->{incoming_name},
248 34 "is_$reltype" => 1
249 );
250  
251 # update or insert
252 34 my $relationships_table = $self->core->table('relationships');
253 my $existing_rel = $relationships_table->select_one({
254 left_entity_type_id => $self->id,
255 name => $rel{name},
256 34 });
257  
258 34 ▶ if ($existing_rel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_rel) { } 0 34 34
259  
260 0 $rel{id} = $existing_rel->{id};
261  
262 # update
263 0 my %changed_cols = map { $_ => $rel{$_} }
264 0 grep { $rel{$_} ne $existing_rel->{$_} }
265 keys %rel;
266  
267 $relationships_table->update(\%changed_cols, { id => $rel{id} })
268 0 ▶ if keys %changed_cols > 0;
Branch: 1 branch
BranchTrueFalseTotal
if keys %changed_cols > 0 0 0 0
269 }
270 else {
271 34 my $id = $relationships_table->insert(\%rel);
272 die sprintf("Database error while registering '%s -> %s' relationship.", $self->name, $rel{name})
273 34 ▶ unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 34 34
274  
275 34 $rel{id} = $id;
276 }
277  
278 # install relationship
279 34 $self->_install_relationship(\%rel);
280 }
281  
282 sub _install_relationship {
283 38 ▶ my ($self, $rel) = @_;
Subroutine _install_relationship: called CC=3 SCAR=11.2
284 38 my $relname = $rel->{name};
285  
286 38 ▶ die sprintf("Entity '%s' already has relationship '%s'.", $self->name, $relname)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_relationship($relname) 0 38 38
287 if $self->has_relationship($relname);
288  
289 38 my $other_entity = $self->core->type_by_id($rel->{right_entity_type_id});
290  
291 # install our side
292 38 $self->_relationships->{$relname} = {
293 %$rel,
294 entity => $other_entity->name
295 };
296  
297 # install their side
298 die sprintf("Entity '%s' already has relationship '%s'.", $self->name, $relname)
299 38 ▶ if $other_entity->has_relationship($rel->{incoming_name});
Branch: 1 branch
BranchTrueFalseTotal
if $other_entity->has_relationship($rel->{"incoming_name"}) 0 38 38
300  
301 $other_entity->_relationships->{$rel->{incoming_name}} = {
302 %$rel,
303 entity => $self->name,
304 is_right_entity => 1,
305 name => $rel->{incoming_name},
306 incoming_name => $rel->{name},
307 38 };
308 }
309  
310  
311 # sub _install_relationship {
312 # my ($self, $relname, $rel) = @_;
313 #
314 # die sprintf("Entity '%s' already has relationship '%s'.", $self->name, $relname)
315 # if exists $self->_relationships->{$relname};
316 #
317 # $self->_relationships->{$relname} = $rel;
318 # }
319  
320  
321  
322 sub prune_attributes {
323 0 ▶ my ($self, $names) = @_;
Subroutine prune_attributes: not called CC=1 SCAR=6.9
Pod undocumented
324 # TODO implement prune_attributes
325 }
326  
327 sub prune_relationships {
328 0 ▶ my ($self, $names) = @_;
Subroutine prune_relationships: not called CC=1 SCAR=6.9
Pod undocumented
329 # TODO implement prune_relationships
330 }
331  
332  
333  
334  
335  
336  
337  
338  
339  
340 10 1;
341  
342  
343 __END__
« lib/DBIx/EAV/Cursor.pm ↑ Summary lib/DBIx/EAV.pm »