File Coverage

lib/DBIx/EAV/EntityType.pm
Criterion Covered Total %
statement 101 112 90.1
branch 42 52 80.7
condition 2 3 66.6
subroutine 17 19 89.4
pod 1 15 6.6
total 163 201 81.0


line stmt bran cond sub pod time code
1             package DBIx::EAV::EntityType;
2              
3 10     10   66 use Moo;
  10         16  
  10         84  
4 10     10   3661 use strictures 2;
  10         80  
  10         430  
5 10     10   2221 use Carp qw/ confess /;
  10         22  
  10         14911  
6              
7              
8              
9             has 'core', is => 'ro', required => 1;
10             has 'id', is => 'ro', required => 1;
11             has 'name', is => 'ro', required => 1;
12             has 'parent', is => 'ro', predicate => 1;
13             has '_static_attributes', is => 'ro', init_arg => undef, lazy => 1, builder => 1;
14             has '_attributes', is => 'ro', init_arg => 'attributes', default => sub { {} };
15             has '_relationships', is => 'ro', init_arg => 'relationships', default => sub { {} };
16              
17              
18             sub _build__static_attributes {
19 19     19   205 my $self = shift;
20             +{
21 65         313 map { $_ => {name => $_, is_static => 1} }
22 19         36 @{$self->core->table('entities')->columns}
  19         377  
23             }
24             }
25              
26             sub load {
27 2     2 0 5 my ($class, $row) = @_;
28 2 50       5 die "load() is a class method" if ref $class;
29              
30 2         42 my $self = $class->new($row);
31              
32             # load attributes
33 2         40 my $sth = $self->core->table('attributes')->select({ entity_type_id => $self->id });
34              
35 2         34 while (my $attr = $sth->fetchrow_hashref) {
36 7         73 $self->_attributes->{$attr->{name}} = $attr;
37             }
38              
39             # load relationships
40 2         44 $sth = $self->core->table('relationships')->select([ {left_entity_type_id => $self->id} , {right_entity_type_id => $self->id} ]);
41              
42 2         42 while (my $rel = $sth->fetchrow_hashref) {
43              
44 5 100       19 if ($self->id eq $rel->{left_entity_type_id}) {
45 2         34 $self->_relationships->{$rel->{name}} = $rel;
46             }
47             else {
48             $self->_relationships->{$rel->{incoming_name}} = {
49             %$rel,
50             is_right_entity => 1,
51             name => $rel->{incoming_name},
52             incoming_name => $rel->{name},
53 3         55 };
54             }
55             }
56              
57 2         21 $self;
58             }
59              
60             sub parents {
61 6     6 0 6909 my ($self) = @_;
62 6 50       21 return () unless $self->has_parent;
63 6         8 my @parents;
64 6         11 my $parent = $self->parent;
65 6         13 while ($parent) {
66 9         14 push @parents, $parent;
67 9         18 $parent = $parent->parent;
68             }
69              
70 6         14 @parents;
71             }
72              
73             sub is_type($) {
74 34     34 0 2365 my ($self, $type) = @_;
75 34 50       89 confess 'usage: is_type($type)' unless $type;
76 34 100       209 return 1 if $self->name eq $type;
77 5         15 foreach my $parent ($self->parents) {
78 6 100       27 return 1 if $parent->name eq $type;
79             }
80 0         0 0;
81             }
82              
83              
84              
85              
86             sub has_attribute {
87 127     127 0 2126 my ($self, $name) = @_;
88 127 100 66     1354 return 1 if exists $self->_attributes->{$name} || exists $self->_static_attributes->{$name};
89 21 100       274 return 0 unless $self->has_parent;
90              
91 12         18 my $parent = $self->parent;
92 12         18 while ($parent) {
93 16 100       22 return 1 if $parent->has_own_attribute($name);
94 5         36 $parent = $parent->parent;
95             }
96              
97 1         5 0;
98             }
99              
100             sub has_static_attribute {
101 355     355 0 2719 my ($self, $name) = @_;
102 355         6798 exists $self->_static_attributes->{$name};
103             }
104              
105             sub has_own_attribute {
106 19     19 0 31 my ($self, $name) = @_;
107 19 100       178 exists $self->_attributes->{$name} || exists $self->_static_attributes->{$name};
108             }
109              
110             sub has_inherited_attribute {
111 5     5 0 845 my ($self, $name) = @_;
112 5 50       16 return 0 unless $self->has_parent;
113 5         18 my $parent = $self->parent;
114 5         15 while ($parent) {
115 5 50       25 return 1 if exists $parent->_attributes->{$name};
116 0         0 $parent = $parent->parent;
117             }
118 0         0 0;
119             }
120              
121             sub attribute {
122 624     624 0 1122 my ($self, $name) = @_;
123              
124             # our attr
125             return $self->_attributes->{$name}
126 624 100       2190 if exists $self->_attributes->{$name};
127              
128             return $self->_static_attributes->{$name}
129 120 100       1817 if exists $self->_static_attributes->{$name};
130              
131             # parent attr
132 113         706 my $parent = $self->parent;
133 113         161 while ($parent) {
134             return $parent->_attributes->{$name}
135 168 100       382 if exists $parent->_attributes->{$name};
136 55         68 $parent = $parent->parent;
137             }
138              
139             # unknown attribute
140 0         0 die sprintf("Entity '%s' does not have attribute '%s'.", $self->name, $name);
141             }
142              
143             sub attributes {
144 82     82 1 276 my ($self, %options) = @_;
145 82         113 my @items;
146              
147             # static
148 1         21 push @items, values %{$self->_static_attributes}
149 82 100       191 unless $options{no_static};
150              
151             # own
152 82         291 push @items, values %{$self->_attributes}
153 82 50       185 unless $options{no_own};
154              
155             # inherited
156 82 50       201 unless ($options{no_inherited}) {
157              
158 82         164 my $parent = $self->parent;
159 82         199 while ($parent) {
160 23         24 push @items, values %{$parent->_attributes};
  23         41  
161 23         47 $parent = $parent->parent;
162             }
163             }
164              
165 82 100       243 return $options{names} ? map { $_->{name} } @items : @items;
  309         785  
166             }
167              
168              
169              
170              
171             sub has_own_relationship {
172 88     88 0 91 my ($self, $name) = @_;
173 88         179 exists $self->_relationships->{$name};
174             }
175              
176             sub has_relationship {
177 419     419 0 2041 my ($self, $name) = @_;
178 419 100       1224 return 1 if exists $self->_relationships->{$name};
179 365 100       1397 return 0 unless $self->has_parent;
180              
181 59         66 my $parent = $self->parent;
182 59         80 while ($parent) {
183 88 100       106 return 1 if $parent->has_own_relationship($name);
184 80         129 $parent = $parent->parent;
185             }
186              
187 51         89 0;
188             }
189              
190             sub relationship {
191 100     100 0 210 my ($self, $name) = @_;
192              
193             # our
194             return $self->_relationships->{$name}
195 100 100       425 if exists $self->_relationships->{$name};
196              
197             # parent
198 15         22 my $parent = $self->parent;
199 15         22 while ($parent) {
200             return $parent->_relationships->{$name}
201 15 50       51 if exists $parent->_relationships->{$name};
202 0         0 $parent = $parent->parent;
203             }
204              
205             # unknown
206 0         0 die sprintf("Entity '%s' does not have relationship '%s'.", $self->name, $name);
207             }
208              
209             sub relationships {
210 16     16 0 43 my ($self, %options) = @_;
211              
212             # ours
213 16         26 my @items = values %{$self->_relationships};
  16         71  
214              
215             # inherited
216 16 50       50 unless ($options{no_inherited}) {
217              
218 16         41 my $parent = $self->parent;
219 16         47 while ($parent) {
220 3         6 push @items, values %{$parent->_relationships};
  3         10  
221 3         10 $parent = $parent->parent;
222             }
223             }
224              
225 16 50       76 return $options{names} ? map { $_->{name} } @items : @items;
  0            
226             }
227              
228              
229              
230             sub prune_attributes {
231 0     0 0   my ($self, $names) = @_;
232             # TODO implement prune_attributes
233 0           die "not implemented yet";
234             }
235              
236             sub prune_relationships {
237 0     0 0   my ($self, $names) = @_;
238             # TODO implement prune_relationships
239 0           die "not implemented yet";
240             }
241              
242              
243              
244              
245              
246              
247              
248              
249              
250             1;
251              
252              
253             __END__