EntityModel/Entity.pm

filter: Statementstmt 55.4% 41 / 74 Branchbran 25.0% 6 / 24 Conditioncond 12.5% 2 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 53.3% 8 / 15 Podpod 80.0% 8 / 10 totaltotal 44.3% 66 / 149 CC 22 SCAR 40.0
CC
22
Cov
43%
matches
156.0
new
6.0
new_field
6.0
CRAP 54.5
↑ Summary EntityModel/Template.pm »
1 package EntityModel::Entity;
2 {
3 53 $EntityModel::Entity::VERSION = '0.102';
4 }
5 53 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 name => { type => 'string' },
7 'package' => { type => 'string' },
8 type => { type => 'string' },
9 description => { type => 'string' },
10 primary => { type => 'string' },
11 keyfield => { type => 'EntityModel::Field' },
12 constraint => { type => 'array', subclass => 'EntityModel::Entity::Constraint' },
13 field => { type => 'array', subclass => 'EntityModel::Field' },
14 field_map => { type => 'hash', scope => 'private', watch => { field => 'name' } },
15 53 };
16 53 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 53 ▶use overload '""' => sub { 'entity:' . shift->name }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 EntityModel::Entity - entity definition for L<EntityModel>
23  
24 =head1 VERSION
25  
26 version 0.102
27  
28 =head1 SYNOPSIS
29  
30 See L<EntityModel>.
31  
32 =head1 DESCRIPTION
33  
34 See L<EntityModel>.
35  
36 =head1 METHODS
37  
38 =cut
39  
40 =head2 new
41  
42 Creates a new entity with the given name.
43  
44 =cut
45  
46 14 ▶sub new_from_name {
Subroutine new_from_name: called CC=1 SCAR=0.0
Pod undocumented
47 14 my $class = shift;
48 14 my $name = shift;
49 14 return bless { name => $name }, $class;
50 }
51  
52 =head2 new
53  
54 Instantiates a new object.
55  
56 Takes the following parameters:
57  
58 =over 4
59  
60 =item * name - the name to apply to this entity
61  
62 =item * field - an arrayref defining the field structure, see L<EntityModel::Field/new> for
63 more information on the expected format here.
64  
65 =item * primary - which field(s) to use as the primary key, as a string or arrayref
66  
67 =item * auto_primary (optional) - automatically create an appropriate
68 primary key and sequence
69  
70 =item * type (optional) - type information, currently unused
71  
72 =back
73  
74 Returns the new instance
75  
76 For backwards-compatibility reasons, when called with a single parameter
77 this will have the same effect as the L</new_from_name> method.
78 Use of this interface is strongly discouraged in new code, since it is
79 likely to be deprecated in the near future.
80  
81 =cut
82  
83 20 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
84 20 my $class = shift;
85 # Support the deprecated ->new('name') interface
86 20 ▶ return $class->new_from_name(@_) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 14 6 20
87  
88 6 my %args = @_;
89 6 my $self = bless { }, $class;
90  
91 6 $self->name(delete $args{name});
92 6 ▶ my @fields = @{delete $args{field} || []};
Condition100%2/2
delete $args{'field'} || []
l!l
24
MC/DC100%1/1
delete $args{'field'} || []
delete $args{'field'}
Truth table100%2/2
A
A= delete $args{'field'}
Aresult
11
00
93 6 my $primary = delete $args{primary};
94 6 ▶ if($args{auto_primary}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"auto_primary"}) 2 4 6
95 2 unshift @fields, {
96 name => $primary = 'id' . $self->name,
97 type => 'bigserial',
98 };
99 }
100 6 $self->add_field(EntityModel::Field->new(%$_)) for @fields;
101 6 $self->primary($primary);
102 6 ▶ $self->keyfield(delete $args{keyfield}) if exists $args{keyfield};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"keyfield"} 0 6 6
103 6 $self
104 }
105  
106 =head2 new_field
107  
108 Helper method to create a new field.
109  
110 =cut
111  
112 0 ▶sub new_field {
Subroutine new_field: not called CC=2 SCAR=17.9
Pod documented
113 0 my $self = shift;
114 0 my $name = shift;
115 0 ▶ my $param = shift || { };
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
116  
117 0 my $field = EntityModel::Field->new({ %$param, name => $name });
118 0 return $field;
119 }
120  
121 =head2 dependencies
122  
123 Report on the dependencies for this entity.
124  
125 Returns a list of L<EntityModel::Entity> instances required for this entity.
126  
127 =cut
128  
129 0 ▶sub dependencies {
Subroutine dependencies: not called CC=1 SCAR=6.9
Pod documented
130 0 my $self = shift;
131 0 return map { $_->refer->entity } grep { $_->refer } $self->field->list;
132 }
133  
134 =head2 matches
135  
136 Returns true if this entity has identical content to another L<EntityModel::Entity>.
137  
138 =cut
139  
140 0 ▶sub matches {
Subroutine matches: not called CC=12 SCAR=50.5
Pod documented
141 0 my ($self, $dst) = @_;
142 0 ▶ die "Not an EntityModel::Entity" unless $dst->isa('EntityModel::Entity');
Branch: 1 branch
BranchTrueFalseTotal
unless $dst->isa("EntityModel::Entity") 0 0 0
143  
144 0 ▶ return 0 if $self->name ne $dst->name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->name ne $dst->name 0 0 0
145 0 ▶ return 0 if $self->field->count != $dst->field->count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->field->count != $dst->field->count 0 0 0
146 0 ▶ return 0 unless $self->primary ~~ $dst->primary;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->primary ~~ $dst->primary 0 0 0
147  
148 0 my @srcF = sort { $a->name cmp $b->name } $self->field->list;
149 0 my @dstF = sort { $a->name cmp $b->name } $dst->field->list;
150 0 ▶ while(@srcF && @dstF) {
Branch: 1 branch
BranchTrueFalseTotal
if (@srcF and @dstF) 0 0 0
Condition0%0/3
@srcF and @dstF
!ll&&!rl&&r
000
MC/DC0%0/2
@srcF and @dstF
@srcF @dstF
Truth table0%0/3
A and B
A= @srcFB= @dstF
ABresult
0X0
100
111
151 0 my $srcf = shift(@srcF);
152 0 my $dstf = shift(@dstF);
153 0 ▶ return 0 unless $srcf && $dstf;
Branch: 1 branch
BranchTrueFalseTotal
unless $srcf and $dstf 0 0 0
Condition0%0/3
$srcf and $dstf
!ll&&!rl&&r
000
MC/DC0%0/2
$srcf and $dstf
$srcf $dstf
Truth table0%0/3
A and B
A= $srcfB= $dstf
ABresult
0X0
100
111
154 0 ▶ return 0 unless $srcf->name eq $dstf->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $srcf->name eq $dstf->name 0 0 0
155 }
156 0 ▶ return 0 if @srcF || @dstF;
Branch: 1 branch
BranchTrueFalseTotal
if @srcF or @dstF 0 0 0
Condition0%0/3
@srcF or @dstF
l!l&&r!l&&!r
000
MC/DC0%0/2
@srcF or @dstF
@srcF @dstF
Truth table0%0/3
A or B
A= @srcFB= @dstF
ABresult
1X1
011
000
157 0 return 1;
158 }
159  
160 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
161 0 my $self = shift;
162 0 ▶ my $out = shift || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 print join(' ', @_) . "\n";
164 0 ▶ };
Condition0%0/3
shift() || sub { print join(' ', @_) . "\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { print join(' ', @_) . "\n"; }
shift() sub { print join(' ', @_) . "\n"; }
Truth table0%0/3
A || B
A= shift()B= sub { print join(' ', @_) . "\n"; }
ABresult
1X1
011
000
165  
166 0 $self;
167 }
168  
169 0 ▶sub asString { shift->name }
Subroutine asString: not called CC=1 SCAR=6.9
Pod undocumented
170  
171 =head2 create_from_definition
172  
173 Create a new L<EntityModel::Entity> from the given definition (hashref).
174  
175 =cut
176  
177 13 ▶sub create_from_definition {
Subroutine create_from_definition: called CC=4 SCAR=13.9
Pod documented
178 13 my $class = shift;
179 13 my $def = shift;
180 13 my $self = $class->new(delete $def->{name});
181  
182 13 ▶ if(my $field = delete $def->{field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = delete $def->{"field"}) 13 0 13
183 13 $self->add_field(EntityModel::Field->create_from_definition($_)) foreach @$field;
184 }
185  
186 # Apply any remaining parameters
187 13 $self->$_($def->{$_}) foreach keys %$def;
188 13 return $self;
189 }
190  
191 =head2 add_field
192  
193 Add a new field to this entity.
194  
195 =cut
196  
197 41 ▶sub add_field {
Subroutine add_field: called CC=1 SCAR=0.0
Pod documented
198 41 my $self = shift;
199 41 my $field = shift;
200 41 $self->field->push($field);
201 41 return $self;
202 }
203  
204 =head2 field_by_name
205  
206 Returns the L<EntityModel::Field> matching the given name.
207  
208 Takes $name as a single parameter.
209  
210 Returns undef if not found.
211  
212 =cut
213  
214 0 ▶sub field_by_name { my $self = shift; my $name = shift; shift->field_map->{$name} }
Subroutine field_by_name: not called CC=1 SCAR=6.9
Pod documented
215  
216 53 1;
217  
218 __END__
↑ Summary EntityModel/Template.pm »