File Coverage

blib/lib/EntityModel/Entity.pm
Criterion Covered Total %
statement 38 71 53.5
branch 8 24 33.3
condition 0 13 0.0
subroutine 8 15 53.3
pod 8 10 80.0
total 62 133 46.6


line stmt bran cond sub pod time code
1             package EntityModel::Entity;
2             {
3             $EntityModel::Entity::VERSION = '0.102';
4             }
5             use EntityModel::Class {
6 18         446 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 18     18   58734 };
  18         97107  
16 18     18   10300 no if $] >= 5.017011, warnings => "experimental::smartmatch";
  18         33  
  18         157  
17              
18 18     18   1836 use overload '""' => sub { 'entity:' . shift->name }, fallback => 1;
  18     20   32  
  18         178  
  20         3384  
19              
20             =head1 NAME
21              
22             EntityModel::Entity - entity definition for L
23              
24             =head1 VERSION
25              
26             version 0.102
27              
28             =head1 SYNOPSIS
29              
30             See L.
31              
32             =head1 DESCRIPTION
33              
34             See L.
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             sub new_from_name {
47 14     14 0 27 my $class = shift;
48 14         21 my $name = shift;
49 14         67 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 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 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             sub new {
84 20     20 1 47287 my $class = shift;
85             # Support the deprecated ->new('name') interface
86 20 100       118 return $class->new_from_name(@_) if @_ == 1;
87              
88 6         26 my %args = @_;
89 6         25 my $self = bless { }, $class;
90              
91 6         43 $self->name(delete $args{name});
92 6 100       269 my @fields = @{delete $args{field} || []};
  6         44  
93 6         17 my $primary = delete $args{primary};
94 6 100       24 if($args{auto_primary}) {
95 2         10 unshift @fields, {
96             name => $primary = 'id' . $self->name,
97             type => 'bigserial',
98             };
99             }
100 6         62 $self->add_field(EntityModel::Field->new(%$_)) for @fields;
101 6         25 $self->primary($primary);
102 6 50       69 $self->keyfield(delete $args{keyfield}) if exists $args{keyfield};
103 6         25 $self
104             }
105              
106             =head2 new_field
107              
108             Helper method to create a new field.
109              
110             =cut
111              
112             sub new_field {
113 0     0 1 0 my $self = shift;
114 0         0 my $name = shift;
115 0   0     0 my $param = shift || { };
116              
117 0         0 my $field = EntityModel::Field->new({ %$param, name => $name });
118 0         0 return $field;
119             }
120              
121             =head2 dependencies
122              
123             Report on the dependencies for this entity.
124              
125             Returns a list of L instances required for this entity.
126              
127             =cut
128              
129             sub dependencies {
130 0     0 1 0 my $self = shift;
131 0         0 return map { $_->refer->entity } grep { $_->refer } $self->field->list;
  0         0  
  0         0  
132             }
133              
134             =head2 matches
135              
136             Returns true if this entity has identical content to another L.
137              
138             =cut
139              
140             sub matches {
141 0     0 1 0 my ($self, $dst) = @_;
142 0 0       0 die "Not an EntityModel::Entity" unless $dst->isa('EntityModel::Entity');
143              
144 0 0       0 return 0 if $self->name ne $dst->name;
145 0 0       0 return 0 if $self->field->count != $dst->field->count;
146 0 0       0 return 0 unless $self->primary ~~ $dst->primary;
147              
148 0         0 my @srcF = sort { $a->name cmp $b->name } $self->field->list;
  0         0  
149 0         0 my @dstF = sort { $a->name cmp $b->name } $dst->field->list;
  0         0  
150 0   0     0 while(@srcF && @dstF) {
151 0         0 my $srcf = shift(@srcF);
152 0         0 my $dstf = shift(@dstF);
153 0 0 0     0 return 0 unless $srcf && $dstf;
154 0 0       0 return 0 unless $srcf->name eq $dstf->name;
155             }
156 0 0 0     0 return 0 if @srcF || @dstF;
157 0         0 return 1;
158             }
159              
160             sub dump {
161 0     0 1 0 my $self = shift;
162             my $out = shift || sub {
163 0     0   0 print join(' ', @_) . "\n";
164 0   0     0 };
165              
166 0         0 $self;
167             }
168              
169 0     0 0 0 sub asString { shift->name }
170              
171             =head2 create_from_definition
172              
173             Create a new L from the given definition (hashref).
174              
175             =cut
176              
177             sub create_from_definition {
178 13     13 1 24 my $class = shift;
179 13         18 my $def = shift;
180 13         65 my $self = $class->new(delete $def->{name});
181              
182 13 50       54 if(my $field = delete $def->{field}) {
183 13         85 $self->add_field(EntityModel::Field->create_from_definition($_)) foreach @$field;
184             }
185              
186             # Apply any remaining parameters
187 13         58 $self->$_($def->{$_}) foreach keys %$def;
188 13         95 return $self;
189             }
190              
191             =head2 add_field
192              
193             Add a new field to this entity.
194              
195             =cut
196              
197             sub add_field {
198 41     41 1 929 my $self = shift;
199 41         52 my $field = shift;
200 41         137 $self->field->push($field);
201 41         3624 return $self;
202             }
203              
204             =head2 field_by_name
205              
206             Returns the L 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     0 1   sub field_by_name { my $self = shift; my $name = shift; shift->field_map->{$name} }
  0            
  0            
215              
216             1;
217              
218             __END__