| 1 | package EntityModel::Collection; | ||||||||||||
| 2 | { | ||||||||||||
| 3 | 4 | $EntityModel::Collection::VERSION = '0.102'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | 4 | ▶ | use EntityModel::Class { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | pending => 'int', | |||||||||||
| 7 | event_handler => 'hash', | ||||||||||||
| 8 | 4 | }; | |||||||||||
| 9 | 4 | ▶ | no if $] >= 5.017011, warnings => "experimental::smartmatch"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | EntityModel::Collection - manage entity model definitions | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 VERSION | ||||||||||||
| 16 | |||||||||||||
| 17 | version 0.102 | ||||||||||||
| 18 | |||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||
| 20 | |||||||||||||
| 21 | =cut | ||||||||||||
| 22 | |||||||||||||
| 23 | =head2 OPERATORS | ||||||||||||
| 24 | |||||||||||||
| 25 | The coderef operator is overloaded by default, allowing syntax such as C< $self->(event => @data) >. | ||||||||||||
| 26 | |||||||||||||
| 27 | =cut | ||||||||||||
| 28 | |||||||||||||
| 29 | use overload | ||||||||||||
| 30 | # Allow ->(event => @parameter) for laziness | ||||||||||||
| 31 | 4 | ▶ | '&{}' => sub { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||||
| 32 | 8 | my $self = shift; | |||||||||||
| 33 | 6 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.5
| |||||||||||||
| 34 | 6 | my $event = shift; | |||||||||||
| 35 | 6 | my @param = @_; | |||||||||||
| 36 | 6 | logDebug("[%s] %s", $event, join ', ', @param); | |||||||||||
| 37 | 6 | ▶ | if(my $handler = $self->{event_handler}->{$event}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | try { | ||||||||||||
| 39 | # Use a temporary so we don't end up in void context | ||||||||||||
| 40 | 6 | my $rslt; | |||||||||||
| 41 | 6 | $rslt = $_->($self, @param) for @$handler; | |||||||||||
| 42 | 6 | } catch { | |||||||||||
| 43 | 6 | my $failure = $_; | |||||||||||
| 44 | 6 | logError("Handler on %s for [%s] threw error %s", $self, $event, $failure); | |||||||||||
| 45 | 6 | ▶ | die "No failure handler available" unless $self->has_event_handlers_for('fail'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | # Raise a failure event, but avoid loops | ||||||||||||
| 47 | 3 | try { $self->(fail => $failure); } | |||||||||||
| 48 | 0 | ▶ | catch { logError("Also failed in failure handler, this time [%s] original [%s]", $_, $failure) } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | unless $event eq 'fail'; | ||||||||||||
| 50 | }; | ||||||||||||
| 51 | } else { | ||||||||||||
| 52 | 0 | logWarning("No handler for [%s]", $event); | |||||||||||
| 53 | } | ||||||||||||
| 54 | return defined wantarray | ||||||||||||
| 55 | 0 | ▶ | ? $self | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | : $self->commit; | ||||||||||||
| 57 | } | ||||||||||||
| 58 | 8 | }, | |||||||||||
| 59 | # Could dump out more info on "" perhaps | ||||||||||||
| 60 | 4 | fallback => 1; | |||||||||||
| 61 | |||||||||||||
| 62 | =head2 each | ||||||||||||
| 63 | |||||||||||||
| 64 | Execute the given code for each item that matches the current chain. | ||||||||||||
| 65 | |||||||||||||
| 66 | =cut | ||||||||||||
| 67 | |||||||||||||
| 68 | 2 | ▶ | sub each { | ||||||||||
|
Subroutine
each: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 69 | 2 | my $self = shift; | |||||||||||
| 70 | 2 | my $code = shift; | |||||||||||
| 71 | 2 | logDebug("Each"); | |||||||||||
| 72 | 2 | $self->add_handler(item => $code); | |||||||||||
| 73 | return defined wantarray | ||||||||||||
| 74 | 2 | ▶ | ? $self | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | : $self->commit | ||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | =head2 done | ||||||||||||
| 79 | |||||||||||||
| 80 | Supply a coderef which will be called on successful completion of the chain so far, guaranteed | ||||||||||||
| 81 | to be after any items have been processed. | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 2 | ▶ | sub done { | ||||||||||
|
Subroutine
done: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 86 | 2 | my $self = shift; | |||||||||||
| 87 | 2 | my $code = shift; | |||||||||||
| 88 | 2 | logDebug("Done"); | |||||||||||
| 89 | 2 | $self->add_handler(done => $code); | |||||||||||
| 90 | return defined wantarray | ||||||||||||
| 91 | 2 | ▶ | ? $self | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | : $self->commit | ||||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | =head2 fail | ||||||||||||
| 96 | |||||||||||||
| 97 | Supply a coderef which will be called on error. Default behaviour is to die(). | ||||||||||||
| 98 | |||||||||||||
| 99 | =cut | ||||||||||||
| 100 | |||||||||||||
| 101 | 2 | ▶ | sub fail { | ||||||||||
|
Subroutine
fail: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 102 | 2 | my $self = shift; | |||||||||||
| 103 | 2 | my $code = shift; | |||||||||||
| 104 | 2 | logDebug("Set handler for failed"); | |||||||||||
| 105 | 2 | $self->add_handler(fail => $code); | |||||||||||
| 106 | return defined wantarray | ||||||||||||
| 107 | 2 | ▶ | ? $self | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | : $self->commit | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | =head2 commit | ||||||||||||
| 112 | |||||||||||||
| 113 | =cut | ||||||||||||
| 114 | |||||||||||||
| 115 | 1 | ▶ | sub commit { | ||||||||||
|
Subroutine
commit: called CC=6 SCAR=23.0
Pod
documented
| |||||||||||||
| 116 | 1 | my $self = shift; | |||||||||||
| 117 | 1 | my $code = shift; | |||||||||||
| 118 | # Protect against unnecessary calls | ||||||||||||
| 119 | 1 | ▶ | unless($self->{pending}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 0 | logDebug("Commit with nothing pending"); | |||||||||||
| 121 | 0 | ▶ | $code->() if $code; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 0 | return $self; | |||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | logDebug("Commit"); | |||||||||||
| 126 | # Apply anything we can - and make sure we're not in void context to avoid commit loops | ||||||||||||
| 127 | 1 | ▶ | if(my $apply = $self->can('apply')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 0 | my $x; $x = $apply->($self); | |||||||||||
| 129 | } | ||||||||||||
| 130 | 1 | $self->pending(0); | |||||||||||
| 131 | 1 | ▶ | logDebug("Has pending? %s", ($self->has_pending ? 'yes' : 'no')); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | |||||||||||||
| 133 | 1 | ▶ | $code->($self) if $code; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 1 | $self->('done' => $self); | |||||||||||
| 135 | 0 | return $self; | |||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | =head2 add_handler | ||||||||||||
| 139 | |||||||||||||
| 140 | =cut | ||||||||||||
| 141 | |||||||||||||
| 142 | 6 | ▶ | sub add_handler { | ||||||||||
|
Subroutine
add_handler: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 143 | 6 | my $self = shift; | |||||||||||
| 144 | 6 | ▶ | while(@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 6 | my ($event, $code) = splice @_, 0, 2; | |||||||||||
| 146 | 6 | logDebug("Defining handler %s for event %s", "$code", $event); | |||||||||||
| 147 | 6 | push @{$self->{event_handler}->{$event}}, $code; | |||||||||||
| 148 | 6 | ▶ | ++$self->{pending} if $event ~~ [qw(item done)]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | } | ||||||||||||
| 150 | 6 | return $self; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | =head2 has_event_handlers_for | ||||||||||||
| 154 | |||||||||||||
| 155 | Returns how many event handlers are defined for this event. | ||||||||||||
| 156 | |||||||||||||
| 157 | =cut | ||||||||||||
| 158 | |||||||||||||
| 159 | 6 | ▶ | sub has_event_handlers_for { | ||||||||||
|
Subroutine
has_event_handlers_for: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 160 | 6 | my $self = shift; | |||||||||||
| 161 | 6 | ▶ | my $event = shift or die "Invalid event passed"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | 6 | return scalar @{$self->{event_handler}->{$event}}; | |||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | =head2 has_pending | ||||||||||||
| 166 | |||||||||||||
| 167 | Returns true if there's anything pending, false otherwise. | ||||||||||||
| 168 | |||||||||||||
| 169 | =cut | ||||||||||||
| 170 | |||||||||||||
| 171 | 2 | ▶ | sub has_pending { (shift->pending || 0) > 0 } | ||||||||||
|
Subroutine
has_pending: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
(shift())->pending || 0
MC/DC100%1/1
(shift())->pending || 0
(shift())->pending
Truth table100%2/2
A
A= (shift())->pending
| |||||||||||||
| 172 | |||||||||||||
| 173 | =head2 DESTROY | ||||||||||||
| 174 | |||||||||||||
| 175 | When we go out of scope, we want any pending actions to be applied immediately. | ||||||||||||
| 176 | |||||||||||||
| 177 | =cut | ||||||||||||
| 178 | |||||||||||||
| 179 | 1 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||||
| 180 | 1 | my $self = shift; | |||||||||||
| 181 | 1 | ▶ | $self->commit if $self->has_pending; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | 4 | 1; | |||||||||||