| 1 | package EntityModel::Support::Perl::Base; | ||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||
| 3 | 1 | $EntityModel::Support::Perl::Base::VERSION = '0.102'; | |||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use EntityModel::Class { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | }; | |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | no if $] >= 5.017011, warnings => "experimental::smartmatch"; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | EntityModel::Support::Perl::Base - base class for entity instances | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | version 0.102 | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | say $_->name foreach Entity::Thing->find({name=>'test'}); | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | All entities are derived from this base class by default. | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | =head1 ASYNCHRONOUS HANDLING | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | When data has not yet been loaded for an entity, some additional logic is used to allow | ||||||||||||||||||||||||||||||||
| 28 | asynchronous requests via chained method accessors. | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | Given a chain $book->author->address->city, where the first three entries are regular entities | ||||||||||||||||||||||||||||||||
| 31 | and the last item in the chain is an accessor for a scalar method: | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | First, we check $book to see whether it has an author yet. If the author information is loaded | ||||||||||||||||||||||||||||||||
| 34 | (to the extent that ->author contains an entity instance), then we can use this existing instance. | ||||||||||||||||||||||||||||||||
| 35 | If not, we instantiate a new entity of this type, marked as incomplete and as a pending request, and | ||||||||||||||||||||||||||||||||
| 36 | continue. | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | This means that a chain where some of the elements can be null is still valid. As data is populated, | ||||||||||||||||||||||||||||||||
| 39 | entries in this chain will be filled out, and cases where the foreign key value was null will end up | ||||||||||||||||||||||||||||||||
| 40 | marked as invalid entities. | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | In general, unless you know beforehand that all entities in the chain have been populated, all access | ||||||||||||||||||||||||||||||||
| 43 | to chained entities should go through the L<EntityModel::Gather> interface to ensure that values are | ||||||||||||||||||||||||||||||||
| 44 | consistent. | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | This also allows the backend storage to apply optimisations if available - if several pending requests | ||||||||||||||||||||||||||||||||
| 47 | address related storage areas, it may be possible for the storage engine to combine queries and return | ||||||||||||||||||||||||||||||||
| 48 | data faster. | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =head1 EVENTS | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | Two types of events can be defined: | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | =item * task - this is a one-shot event, typically used to call a piece of code after data has been written | ||||||||||||||||||||||||||||||||
| 57 | to or read from storage | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | =item * event - this is an event handler that will be called every time an event occurs. | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | These are handled by the L</_queue_task> and L</_attach_event> methods respectively. | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | use Time::HiRes qw{time}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | use POSIX::strptime (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | use Tie::Cache::LRU; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _supported_callbacks { qw(before_commit after_load on_not_found on_create) } | ||||||||||||||||||||||||||||||
|
Subroutine
_supported_callbacks: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =head2 new | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | Instantiate from an ID or a pre-fabricated object (hashref). | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =over 4 | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =item * Create a new, empty object: | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | EntityModel::Support::Perl::Base->new(1) | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =item * Instantiate from ID: | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | EntityModel::Support::Perl::Base->new(1) | ||||||||||||||||||||||||||||||||
| 86 | EntityModel::Support::Perl::Base->new('123-456') | ||||||||||||||||||||||||||||||||
| 87 | EntityModel::Support::Perl::Base->new([123,456]) | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | =item * Create an object and assign initial values: | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | EntityModel::Support::Perl::Base->new({ x => 1, y => 2 }) | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | Any remaining options indicate callbacks: | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | =over 4 | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | =item * before_commit - just before commit | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | =item * after_commit - after this has been committed to the database | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =item * on_load - when the data has been read from storage | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =item * on_not_found - when storage reports that this item is not found | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | The before_XXX callbacks are also aliased to on_XXX for convenience. | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=13 SCAR=50.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 114 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $spec = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 116 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 0 | my %opt; | |||||||||||||||||||||||||||||||
| 119 | 0 | my $self = bless { | |||||||||||||||||||||||||||||||
| 120 | _incomplete => 1 | ||||||||||||||||||||||||||||||||
| 121 | }, $class; | ||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return $self if $args{pending}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # Now we might want to provide some callbacks | ||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | while(my ($k, $v) = each %args) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if($k eq 'create') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $opt{create} = $v ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | } elsif($k ~~ $class->_supported_callbacks) { | ||||||||||||||||||||||||||||||||
| 129 | 0 | $self->{_callback}->{$k} = $v; | |||||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||||||
| 131 | 0 | warn "Unknown callback $k requested"; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | # An arrayref or plain value is used as an ID | ||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if(!ref($spec) || ref($spec) eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
not ref $spec or ref $spec eq "ARRAY"
MC/DC0%0/2
not ref $spec or ref $spec eq "ARRAY"
not ref $spec
ref $spec eq "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $specB= ref $spec eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 137 | $class->_storage->read( | ||||||||||||||||||||||||||||||||
| 138 | entity => $class->_entity, | ||||||||||||||||||||||||||||||||
| 139 | id => $spec, | ||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | on_complete => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 141 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||
| 142 | 0 | $self->{$_} = $data->{$_} for keys %$data; | |||||||||||||||||||||||||||||||
| 143 | 0 | delete $self->{_incomplete}; | |||||||||||||||||||||||||||||||
| 144 | 0 | $self->_event('on_load'); | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | 0 | ); | |||||||||||||||||||||||||||||||
| 147 | # A hashref (possibly empty) means we create a new object with the given values | ||||||||||||||||||||||||||||||||
| 148 | } elsif(ref($spec) eq 'HASH') { | ||||||||||||||||||||||||||||||||
| 149 | 0 | my $data = $class->_spec_from_hashref($spec); | |||||||||||||||||||||||||||||||
| 150 | 0 | $self->{$_} = $data->{$_} for keys %$data; | |||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if($opt{create}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $self->_queue_task(on_create => delete $args{on_complete}) if exists $args{on_complete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 0 | $self->{ _insert_required } = 1; | |||||||||||||||||||||||||||||||
| 154 | $self->_insert( | ||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | on_complete => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 156 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 158 | # warn "Created " . Dumper($data); | ||||||||||||||||||||||||||||||||
| 159 | 0 | delete $self->{_incomplete}; | |||||||||||||||||||||||||||||||
| 160 | 0 | $self->_event('on_create'); | |||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | 0 | ); | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | =head2 _queue_task | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | Queues a new one-shot task for the given event type. | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | Supports the following event types: | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | =over 4 | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | =item * on_load - data has been read from storage | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | =item * on_create - initial data has been written to storage | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =item * on_update - values have been updated, but not necessarily written to storage | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | =item * on_remove - this entry has been removed from storage | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | =item * on_not_found - could not find this entry in backend storage | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub _queue_task { | ||||||||||||||||||||||||||||||
|
Subroutine
_queue_task: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | while(@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 0 | my ($evt, $task) = splice @_, 0, 2; | |||||||||||||||||||||||||||||||
| 194 | 0 | push @{$self->{_task_pending}->{$evt}}, $task; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | =head2 _event | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | Pass the given event through to any defined callbacks. | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub _event { | ||||||||||||||||||||||||||||||
|
Subroutine
_event: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||
| 206 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 207 | 0 | my $ev = shift; | |||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | if(my $task = shift @{$self->{_task_pending}->{$ev}}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 0 | $task->($self, @_); | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | if(exists $self->{_callback}->{$ev}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | 0 | $_->($self, @_) for @{$self->{_callback}->{$ev}}; | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | # also alias before_XXX to on_XXX | ||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if($ev =~ /^before_(.*)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 218 | 0 | $ev = "on_$1"; | |||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if(exists $self->{_callback}->{$ev}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | 0 | $_->($self, @_) for @{$self->{_callback}->{$ev}}; | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | =head2 _spec_from_hashref | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | Private method to generate hashref containing spec information suitable for bless to requested class, | ||||||||||||||||||||||||||||||||
| 229 | given a hashref which represents the keys/values for the object. | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | This will flatten any Entity objects down to the required ID key+values. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub _spec_from_hashref { | ||||||||||||||||||||||||||||||
|
Subroutine
_spec_from_hashref: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 236 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 237 | 0 | my $spec = shift; | |||||||||||||||||||||||||||||||
| 238 | 0 | my %details; | |||||||||||||||||||||||||||||||
| 239 | 0 | foreach my $k (sort keys %$spec) { | |||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if(ref $spec->{$k} && eval { $spec->{$k}->isa(__PACKAGE__) }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $spec->{$k} and eval {
do {
$spec->{$k}->isa(__PACKAGE__)
}
}
MC/DC0%0/2
ref $spec->{$k} and eval {
do {
$spec->{$k}->isa(__PACKAGE__)
}
}
ref $spec->{$k}
eval {
do {
$spec->{$k}->isa(__PACKAGE__)
}
}
Truth table0%0/3
A and B
A= ref $spec->{$k}B= eval {
do {
$spec->{$k}->isa(__PACKAGE__)
}
}
| |||||||||||||||||||||||||||||||||
| 241 | 0 | $details{"id$k"} = $spec->{$k}->id; | |||||||||||||||||||||||||||||||
| 242 | } else { | ||||||||||||||||||||||||||||||||
| 243 | 0 | $details{$k} = $spec->{$k}; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | $details{id} = $spec->{$k} if $k eq $class->_entity->primary; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||
| 247 | 0 | return \%details; | |||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | =head2 create | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | Create a new object. | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | Takes a hashref, and sets the flag so that ->commit does the insert. | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | =cut | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 259 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 260 | 0 | my $self = $class->new(@_, create => 1); | |||||||||||||||||||||||||||||||
| 261 | 0 | $self->commit; | |||||||||||||||||||||||||||||||
| 262 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | sub find { | ||||||||||||||||||||||||||||||
|
Subroutine
find: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 266 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 0 | my %spec = %$args; | |||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | # Convert refs to IDs | ||||||||||||||||||||||||||||||||
| 272 | 0 | foreach my $k (sort keys %spec) { | |||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | $spec{"id$k"} = delete($spec{$k})->id if eval { $spec{$k}->isa(__PACKAGE__); }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 0 | return map { $class->new($_) } $class->_storage->find( | |||||||||||||||||||||||||||||||
| 277 | entity => $class->_entity, | ||||||||||||||||||||||||||||||||
| 278 | data => \%spec, | ||||||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub iterate { | ||||||||||||||||||||||||||||||
|
Subroutine
iterate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 283 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 284 | 0 | my $code = pop; | |||||||||||||||||||||||||||||||
| 285 | 0 | my $q = $class->_find_query(@_); | |||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | $q->iterate(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 287 | 0 | my $self = $class->new($_[0]); | |||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | $code->($self) if $self; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | 0 | }); | |||||||||||||||||||||||||||||||
| 290 | 0 | return $class; | |||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | sub _extract_data { | ||||||||||||||||||||||||||||||
|
Subroutine
_extract_data: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 294 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 295 | return { | ||||||||||||||||||||||||||||||||
| 296 | 0 | map { $_ => $self->$_ } grep { exists $self->{$_} } map { $_->name } $self->_entity->field->list | |||||||||||||||||||||||||||||||
| 297 | }; | ||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | =head2 _update | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | Write current values back to storage. | ||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub _update { | ||||||||||||||||||||||||||||||
|
Subroutine
_update: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 307 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 308 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 0 | $self->{_active_write} = 1; | |||||||||||||||||||||||||||||||
| 311 | 0 | my $primary = $self->_entity->primary; | |||||||||||||||||||||||||||||||
| 312 | $self->_storage->store( | ||||||||||||||||||||||||||||||||
| 313 | entity => $self->_entity, | ||||||||||||||||||||||||||||||||
| 314 | id => $self->id, | ||||||||||||||||||||||||||||||||
| 315 | data => $self->_extract_data, | ||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | on_complete => $self->sap(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 317 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 318 | 0 | $self->{_active_write} = 0; | |||||||||||||||||||||||||||||||
| 319 | }) | ||||||||||||||||||||||||||||||||
| 320 | 0 | ); | |||||||||||||||||||||||||||||||
| 321 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | =head2 _select | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | Populate this instance with values from the database. | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | =cut | ||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub _select { | ||||||||||||||||||||||||||||||
|
Subroutine
_select: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 331 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 332 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 0 | my $primary = $self->_entity->primary; | |||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | die "Undef primary element found for $self" if grep !defined, $primary; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 336 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | my $data = $self->_storage->read( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 338 | entity => $self->_entity, | ||||||||||||||||||||||||||||||||
| 339 | id => $self->id, | ||||||||||||||||||||||||||||||||
| 340 | ) or return EntityModel::Error->new("Failed to read"); | ||||||||||||||||||||||||||||||||
| 341 | 0 | $self->{$_} = $data->{$_} for keys %$data; | |||||||||||||||||||||||||||||||
| 342 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | =head2 _pending_insert | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | Returns true if this instance is due to be committed to the database. | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub _pending_insert { return shift->{_insert_required} ? 1 : 0; } | ||||||||||||||||||||||||||||||
|
Subroutine
_pending_insert: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | =head2 _pending_update | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | Returns true if this instance is due to be committed to the database. | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | =cut | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | sub _pending_update { return shift->{_update_required} ? 1 : 0; } | ||||||||||||||||||||||||||||||
|
Subroutine
_pending_update: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | =head2 _insert | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | Insert this instance into the db. | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | sub _insert { | ||||||||||||||||||||||||||||||
|
Subroutine
_insert: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 368 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 369 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 370 | 0 | my $primary = $self->_entity->primary; | |||||||||||||||||||||||||||||||
| 371 | # FIXME haxx | ||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | delete $self->{$primary} unless defined $self->{$primary}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | $self->_storage->create( | ||||||||||||||||||||||||||||||||
| 375 | entity => $self->_entity, | ||||||||||||||||||||||||||||||||
| 376 | data => $self->_extract_data, | ||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | on_complete => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 378 | 0 | my $v = shift; | |||||||||||||||||||||||||||||||
| 379 | 0 | $self->{id} = $v; | |||||||||||||||||||||||||||||||
| 380 | 0 | delete $self->{_insert_required}; | |||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | $args{on_complete}->($v) if $args{on_complete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||
| 383 | 0 | ); | |||||||||||||||||||||||||||||||
| 384 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | =head2 commit | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | Commit any pending changes to the database. | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 394 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | $self->_insert(@_) if $self->_pending_insert; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | $self->_update(@_) if $self->_pending_update; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 397 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | sub done { | ||||||||||||||||||||||||||||||
|
Subroutine
done: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 401 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 402 | 0 | ▶ | return $self if $self->{failure}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 403 | 0 | my $code = shift; | |||||||||||||||||||||||||||||||
| 404 | 0 | $code->($self); | |||||||||||||||||||||||||||||||
| 405 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | sub fail { | ||||||||||||||||||||||||||||||
|
Subroutine
fail: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 409 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | return $self unless $self->{failure}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 411 | 0 | my $code = shift; | |||||||||||||||||||||||||||||||
| 412 | 0 | $code->($self, $self->{failure}); | |||||||||||||||||||||||||||||||
| 413 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | 0 | ▶ | sub then { | ||||||||||||||||||||||||||||||
|
Subroutine
then: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 417 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 418 | 0 | $self->commit; | |||||||||||||||||||||||||||||||
| 419 | 0 | push @{$self->{_callback}->{on_load}}, @_; | |||||||||||||||||||||||||||||||
| 420 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | =head2 revert | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | Revert any pending changes to the database. | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | sub revert { | ||||||||||||||||||||||||||||||
|
Subroutine
revert: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 430 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | return if $self->_pending_insert; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 432 | 0 | return $self->_select(@_); | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | sub waitFor { | ||||||||||||||||||||||||||||||
|
Subroutine
waitFor: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 436 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||
| 440 | =head2 id | ||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | Primary key (id) for this instance. | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | =cut | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | sub id { | ||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 447 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | logStack("Self is not an instance?") unless ref $self; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | if(@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 450 | 0 | $self->{id} = shift; | |||||||||||||||||||||||||||||||
| 451 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||
| 453 | 0 | ▶ | return $self->{id} if exists $self->{id}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 454 | 0 | logDebug({%$self}); | |||||||||||||||||||||||||||||||
| 455 | # logDebug("Expect from " . $_) foreach $self->_entity->list_primary; | ||||||||||||||||||||||||||||||||
| 456 | 0 | ▶ | $self->{id} = join('-', map { $self->{$_} // 'undef' } $self->_entity->primary); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{$_} // "undef"
MC/DC0%0/1
$self->{$_} // "undef"
$self->{$_}
Truth table0%0/2
A
A= $self->{$_}
| |||||||||||||||||||||||||||||||||
| 457 | 0 | $self->{$_} = $self->{id} foreach $self->_entity->primary; | |||||||||||||||||||||||||||||||
| 458 | # logDebug("Found ID as " . $self->{id}); | ||||||||||||||||||||||||||||||||
| 459 | 0 | return $self->{id}; | |||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | =head2 fromID | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | Instantiate from an ID. | ||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | =cut | ||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | sub fromID { | ||||||||||||||||||||||||||||||
|
Subroutine
fromID: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 469 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 470 | 0 | my $id = shift; | |||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||
| 472 | # logDebug("Instantiate " . ($id // 'undef')); | ||||||||||||||||||||||||||||||||
| 473 | 0 | my $self = bless { }, $class; | |||||||||||||||||||||||||||||||
| 474 | 0 | $self->{id} = $id; | |||||||||||||||||||||||||||||||
| 475 | 0 | $self->{$_} = $id foreach $self->_entity->primary; | |||||||||||||||||||||||||||||||
| 476 | 0 | $self->{_incomplete} = 1; | |||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | return EntityModel::Error->new('Permission denied') if $self->can('hasAccess') && !$self->hasAccess('read'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->can("hasAccess") and not $self->hasAccess("read")
MC/DC0%0/2
$self->can("hasAccess") and not $self->hasAccess("read")
$self->can("hasAccess")
not $self->hasAccess("read")
Truth table0%0/3
A and not(B)
A= $self->can("hasAccess")B= not $self->hasAccess("read")
| |||||||||||||||||||||||||||||||||
| 478 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | sub _request_load { | ||||||||||||||||||||||||||||||
|
Subroutine
_request_load: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 482 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 483 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | if($self->{_incomplete}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 485 | $self->_queue_task( | ||||||||||||||||||||||||||||||||
| 486 | on_load => $args{on_complete} | ||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | ) if exists $args{on_complete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 488 | $self->_queue_task( | ||||||||||||||||||||||||||||||||
| 489 | on_error => $args{on_error} | ||||||||||||||||||||||||||||||||
| 490 | 0 | ▶ | ) if exists $args{on_error}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 491 | } else { | ||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | $args{on_complete}->($self) if exists $args{on_complete}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||
| 494 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | =head2 remove | ||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||
| 499 | Remove this instance from the database. | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | =cut | ||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | sub remove { | ||||||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 504 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 505 | 0 | ▶ | $self->_storage->remove( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 506 | entity => $self->_entity, | ||||||||||||||||||||||||||||||||
| 507 | id => $self->id | ||||||||||||||||||||||||||||||||
| 508 | ) or return EntityModel::Error->new("Failed to remove"); | ||||||||||||||||||||||||||||||||
| 509 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||
| 512 | =head2 _view | ||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||
| 514 | Returns the view corresponding to this object. | ||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | =cut | ||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | 0 | ▶ | sub _view { | ||||||||||||||||||||||||||||||
|
Subroutine
_view: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 519 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | return $self->_viewClass->new(instance => $self) if $self->can('_viewClass'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 521 | 0 | return EntityModel::View->new( | |||||||||||||||||||||||||||||||
| 522 | instance => $self, | ||||||||||||||||||||||||||||||||
| 523 | ); | ||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||
| 526 | 0 | ▶ | sub _paged { | ||||||||||||||||||||||||||||||
|
Subroutine
_paged: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 527 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 528 | 0 | return EntityModel::Pager->new({ | |||||||||||||||||||||||||||||||
| 529 | entity => $class | ||||||||||||||||||||||||||||||||
| 530 | }); | ||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | # wrong place? | ||||||||||||||||||||||||||||||||
| 534 | 1 | my %TimestampCache; | |||||||||||||||||||||||||||||||
| 535 | 1 | tie %TimestampCache, 'Tie::Cache::LRU', 50000; | |||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | 0 | ▶ | sub _timeStamp { | ||||||||||||||||||||||||||||||
|
Subroutine
_timeStamp: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 538 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 539 | 0 | my $fieldName = shift; | |||||||||||||||||||||||||||||||
| 540 | 0 | my $v = $self->{$fieldName}; | |||||||||||||||||||||||||||||||
| 541 | 0 | ▶ | return undef unless $fieldName && $v; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fieldName and $v
MC/DC0%0/2
$fieldName and $v
$fieldName
$v
Truth table0%0/3
A and B
A= $fieldNameB= $v
| |||||||||||||||||||||||||||||||||
| 542 | 0 | ▶ | return $TimestampCache{$v} if exists $TimestampCache{$v}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | 0 | my $ts; | |||||||||||||||||||||||||||||||
| 545 | 0 | ▶ | if($self->{$fieldName} =~ m/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+)(?:\.(\d{1,9}))?/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 546 | 0 | (my $v = $self->{$fieldName}) =~ tr/ /T/; | |||||||||||||||||||||||||||||||
| 547 | 0 | $ts = gmtime POSIX::strptime($v, '%Y-%m-%sT%H:%M:%S'); | |||||||||||||||||||||||||||||||
| 548 | 0 | ▶ | $ts .= $1 if $v =~ /(\.\d+)$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||
| 550 | 0 | $TimestampCache{$v} = $ts; | |||||||||||||||||||||||||||||||
| 551 | 0 | return $ts; | |||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||
| 554 | END { logDebug("Had %d entries in the timestamp cache", (tied %TimestampCache)->curr_size); } | ||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||
| 556 | 1 | 1; | |||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | __END__ | ||||||||||||||||||||||||||||||||