lib/Backbone/Events.pm

filter: Statementstmt 100% 88 / 88 Branchbran 87.5% 14 / 16 Conditioncond 72.7% 8 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 93.8% 137 / 146 SCAR 28.3
CC
17
Cov
96%
___bbe_wrap_multiple_events2
6.0
stop_listening
5.0
_bbe_trigger
4.0
CRAP 17.0
« blib/lib/Backbone/Events.pm ↑ Summary
1 package Backbone::Events;
2 9 $Backbone::Events::VERSION = '0.0.2';
3 9 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use List::MoreUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use namespace::autoclean -also => qr/^__bbe_/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: a port of the Backbone.js event API
10  
11  
12 has _bbe_events => (
13 is => 'ro',
14 25 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 9 );
16  
17 has _bbe_id => (
18 is => 'ro',
19 25 ▶ default => sub { __bbe_new_id() },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 9 );
21  
22 has _bbe_listening_to => (
23 is => 'ro',
24 25 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 9 );
26  
27 9 our $__bbe_last_id;
28 sub __bbe_new_id { ++$__bbe_last_id }
29  
30 sub _bbe_trigger {
31 40 ▶ my ($self, $event_ref, $event, @args) = @_;
Subroutine _bbe_trigger: called CC=4 SCAR=13.9
32 40 my $cb = $event_ref->{cb};
33  
34 40 ▶ if ($event_ref->{ns} eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_ref->{'ns'} eq 'all') { } 8 32 40
35 8 $cb->($event, @args);
36 } else {
37 32 $cb->(@args);
38 }
39  
40 40 ▶ if ($event_ref->{once}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event_ref->{"once"}) 5 35 40
41 5 my ($event, $listen_id) = @{$event_ref}{qw(event listen_id)};
42 5 ▶ $self->off($event, $cb, listen_id => $listen_id//'');
Condition100%2/2
$listen_id // ""
l!l
23
MC/DC100%1/1
$listen_id // ""
$listen_id
Truth table100%2/2
A
A= $listen_id
Aresult
11
00
43 }
44 }
45  
46 sub __bbe_wrap_multiple_events {
47 my ($orig, $self, $events, @args) = @_;
48 if (ref $events eq 'HASH') {
49 $self->$orig($_, $events->{$_}, @args) for keys %$events;
50 } elsif ($events and $events =~ /\s+/) {
51 my $result;
52 $result = $self->$orig($_, @args) for split /\s+/, $events;
53 # return last result
54 return $result;
55 } else {
56 return $self->$orig($events, @args);
57 }
58 }
59  
60 sub ___bbe_wrap_multiple_events2 {
61 50 ▶ my ($orig, $self, $other, $events, @args) = @_;
Subroutine ___bbe_wrap_multiple_events2: called CC=6 SCAR=17.9
62 50 ▶ if (ref $events eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $events eq 'HASH') { } 1 49 50
elsif ($events and $events =~ /\s+/) { } 1 48 49
63 1 $self->$orig($other, $_, $events->{$_}, @args) for keys %$events;
64 ▶ } elsif ($events and $events =~ /\s+/) {
Condition100%3/3
$events and $events =~ /\s+/
!ll&&!rl&&r
18301
MC/DC100%2/2
$events and $events =~ /\s+/
$events $events =~ /\s+/
Truth table100%3/3
A and B
A= $eventsB= $events =~ /\s+/
ABresult
0X0
100
111
65 1 my $result;
66 1 $result = $self->$orig($other, $_, @args) for split /\s+/, $events;
67 # return last result
68 1 return $result;
69 } else {
70 48 return $self->$orig($other, $events, @args);
71 }
72 }
73  
74 sub __bbe_parse_ns {
75 my ($event) = @_;
76 my ($ns, $type) = split(':', $event//'', 2);
77 return ($ns//'', $type//'');
78 }
79  
80 sub __bbe_query {
81 my ($ids, $q) = @_;
82 return grep {
83 my $id = $_;
84 my $match = 1;
85 for my $field (keys %$q) {
86 my $have = $ids->{$id}{$field} // '';
87 my $want = $q->{$field};
88  
89 my $type = ref $want;
90 if ($type eq 'ARRAY') {
91 if (none {$_ eq $have} @$want) {
92 $match = 0;
93 last;
94 }
95 } else {
96 if ($want ne $have) {
97 $match = 0;
98 last;
99 }
100 }
101 }
102 $match;
103 } keys %$ids;
104 }
105  
106 sub __bbe_does_events {
107 my ($obj) = @_;
108 return $obj
109 && blessed($obj)
110 && $obj->DOES(__PACKAGE__);
111 }
112  
113 9 around on => \&__bbe_wrap_multiple_events;
114 sub on {
115 47 ▶ my ($self, $event, $cb, %opts) = @_;
Subroutine on: called CC=1 SCAR=0.0
Pod documented
116 47 my ($ns, $type) = __bbe_parse_ns($event);
117 47 $self->_bbe_events->{__bbe_new_id()} = {
118 %opts,
119 cb => $cb,
120 ns => $ns,
121 type => $type,
122 };
123 47 return $cb;
124 }
125  
126 9 around off => \&__bbe_wrap_multiple_events;
127 sub off {
128 31 ▶ my ($self, $event, $cb, %opts) = @_;
Subroutine off: called CC=1 SCAR=0.0
Pod documented
129 31 my ($ns, $type) = __bbe_parse_ns($event);
130  
131 31 my @ids = __bbe_query($self->_bbe_events, {
132 %opts,
133 ( cb => $cb )x!! $cb,
134 ( ns => $ns )x!! $ns,
135 ( type => $type )x!! $type,
136 });
137 31 delete @{$self->_bbe_events}{@ids};
138 }
139  
140 9 around trigger => \&__bbe_wrap_multiple_events;
141 sub trigger {
142 48 ▶ my ($self, $event, @args) = @_;
Subroutine trigger: called CC=3 SCAR=11.0
Pod documented
143 48 my ($ns, $type) = __bbe_parse_ns($event);
144  
145 48 ▶ my @ids = __bbe_query($self->_bbe_events, {
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 3 45 48
146 ns => [ 'all', $ns ],
147 type => [ $type ? ($type, '') : ('') ],
148 });
149  
150 48 for my $id (@ids) {
151 40 my $event_ref = $self->_bbe_events->{$id};
152 40 $self->_bbe_trigger($event_ref, $event, @args);
153 }
154 }
155  
156 9 around once => \&__bbe_wrap_multiple_events;
157 sub once {
158 6 ▶ my ($self, $event, $cb) = @_;
Subroutine once: called CC=1 SCAR=0.0
Pod documented
159 6 $self->on($event, $cb, once => 1);
160 6 return $cb;
161 }
162  
163 9 around listen_to => \&___bbe_wrap_multiple_events2;
164 sub listen_to {
165 25 ▶ my ($self, $other, $event, $cb, %opts) = @_;
Subroutine listen_to: called CC=3 SCAR=11.6
Pod documented
166 25 ▶ confess "Cannot call listen_to on object that does not consume Backbone::Events"
Branch: 1 branch
BranchTrueFalseTotal
if $other and not __bbe_does_events($other) 0 25 25
Condition33%1/3
$other and not __bbe_does_events($other)
!ll&&!rl&&r
0250
MC/DC0%0/2
$other and not __bbe_does_events($other)
$other not __bbe_does_events($other)
Truth table33%1/3
A and not(B)
A= $otherB= not __bbe_does_events($other)
ABresult
0X0
100
111
167 if $other and not __bbe_does_events($other);
168  
169 25 my ($ns, $type) = __bbe_parse_ns($event);
170 25 $self->_bbe_listening_to->{__bbe_new_id()} = {
171 %opts,
172 cb => $cb,
173 event => $event,
174 ns => $ns,
175 other => $other,
176 other_id => $other->_bbe_id,
177 type => $type,
178 };
179 25 $other->on($event, $cb, %opts, listen_id => $self->_bbe_id);
180  
181 25 return $cb;
182 }
183  
184 9 around stop_listening => \&___bbe_wrap_multiple_events2;
185 sub stop_listening {
186 23 ▶ my ($self, $other, $event, $cb) = @_;
Subroutine stop_listening: called CC=5 SCAR=16.1
Pod documented
187 23 my ($ns, $type) = __bbe_parse_ns($event);
188 23 ▶ confess "Cannot call stop_listening on object that does not consume Backbone::Events"
Branch: 1 branch
BranchTrueFalseTotal
if $other and not __bbe_does_events($other) 0 23 23
Condition66%2/3
$other and not __bbe_does_events($other)
!ll&&!rl&&r
2030
MC/DC0%0/2
$other and not __bbe_does_events($other)
$other not __bbe_does_events($other)
Truth table66%2/3
A and not(B)
A= $otherB= not __bbe_does_events($other)
ABresult
0X0
100
111
189 if $other and not __bbe_does_events($other);
190  
191 23 my $query = {
192 ( cb => $cb )x!! $cb,
193 ( ns => $ns )x!! $ns,
194 ( type => $type )x!! $type,
195 };
196 23 ▶ $query->{other_id} = $other->_bbe_id if $other;
Branch: 1 branch
BranchTrueFalseTotal
if $other 3 20 23
197 23 my @ids = __bbe_query($self->_bbe_listening_to, $query);
198  
199 23 for my $id (@ids) {
200 18 my $listen_ref = $self->_bbe_listening_to->{$id};
201 18 my $other_obj = $listen_ref->{other};
202 18 my @args = @{$listen_ref}{qw(event cb)};
203 18 $other_obj->off(@args, listen_id => $self->_bbe_id);
204 }
205 23 delete @{$self->_bbe_listening_to}{@ids};
206 }
207  
208 9 around listen_to_once => \&___bbe_wrap_multiple_events2;
209 sub listen_to_once {
210 4 ▶ my ($self, $other, $event, $cb) = @_;
Subroutine listen_to_once: called CC=1 SCAR=0.0
Pod documented
211 4 $self->listen_to($other, $event, $cb, once => 1);
212 4 return $cb;
213 }
214  
215 9 1;
216  
217 __END__
« blib/lib/Backbone/Events.pm ↑ Summary