Peer/Subscriptions.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 81.2% 13 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 92.9% 119 / 128 CC 12 SCAR 10.4
CC
12
Cov
94%
__ANON__
7.1
add
3.0
remove
3.0
CRAP 2.8
↑ Summary Peer.pm »
1 # ABSTRACT: A collection of Net::Object::Peer::Subscriptions
2 package Net::Object::Peer::Subscriptions;
3  
4 20 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Types::Standard qw[ ArrayRef InstanceOf ];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Ref::Util qw[ is_coderef ];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use List::Util qw[ all ];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 ▶use Net::Object::Peer::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Net::Object::Peer::Subscription::Ephemeral;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 20 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 20 our $VERSION = '0.06'; # TRIAL
20  
21 has _subscriptions => (
22 is => 'ro',
23 init_arg => undef,
24 isa => ArrayRef [ InstanceOf ['Net::Object::Peer::Subscription'] ],
25 110 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 20 lazy => 1,
27 );
28  
29 #pod =method list
30 #pod
31 #pod @subs = $subs->list;
32 #pod
33 #pod Returns a list of hashrefs containing attributes for all subscriptions.
34 #pod
35 #pod
36 #pod =cut
37  
38 4 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
39 4 return map { $_->as_hashref } @{ $_[0]->_subscriptions };
40 }
41  
42 #pod =method nelem
43 #pod
44 #pod $nelem = $subs->nelem;
45 #pod
46 #pod return the number of elements in the list of subscriptions.
47 #pod
48 #pod =cut
49  
50 4 ▶sub nelem {
Subroutine nelem: called CC=1 SCAR=0.0
Pod documented
51 4 return scalar @{ $_[0]->_subscriptions };
52 }
53  
54 #pod =method add
55 #pod
56 #pod $subs->add( %attr );
57 #pod
58 #pod Add a subscription. See L<Net::Object::Peer::Subscription> for the
59 #pod supported attributes.
60 #pod
61 #pod =cut
62  
63 100 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
64 100 my $self = shift;
65  
66 100 ▶ my %attr = ( @_ == 1 ? %{ $_[0] } : @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 42 58 100
67  
68 100 my $class = 'Net::Object::Peer::Subscription';
69 100 ▶ $class .= '::Ephemeral' if $attr{peer}->$_does( 'Net::Object::Peer::Ephemeral' );
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"peer"}->$Net::Object::Peer::Subscriptions::_does("Net::Object::Peer::Ephemeral") 1 99 100
70  
71 100 push @{ $self->_subscriptions }, $class->new( %attr );
72  
73 100 return;
74 }
75  
76 84 ▶sub _find_index {
Subroutine _find_index: called CC=2 SCAR=7.0
77  
78 84 my $self = shift;
79 84 my $subs = $self->_subscriptions;
80  
81 84 ▶ if ( is_coderef( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_coderef($_[0])) { } 9 75 84
82  
83 9 my $match = shift;
84 9 return grep { $match->( $subs->[$_] ) } 0 .. @$subs - 1;
85  
86 }
87 else {
88 75 my %match = @_;
89  
90 return grep {
91 75 my $sub = $subs->[$_];
92 115 ▶ all {
Subroutine __ANON__: called CC=6 SCAR=19.6
93 115 ▶ if ( $sub->can( $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->can($_)) { } 115 0 115
94 115 my $val = $sub->$_;
95  
96 !defined $val && !defined $match{$_} ? 1
97 : !defined $val || !defined $match{$_} ? 0
98 115 ▶ : $val eq $match{$_};
Branch: 2 branches
BranchTrueFalseTotal
!(defined $val || defined $match{$_}) ? : 0 115 115
!(defined $val && defined $match{$_}) ? : 1 114 115
Condition66%4/6
defined $val || defined $match{$_}
l!l&&r!l&&!r
11410
defined $val && defined $match{$_}
!ll&&!rl&&r
10114
MC/DC0%0/2
defined $val || defined $match{$_}
defined $val defined $match{$_}
MC/DC50%1/2
defined $val && defined $match{$_}
defined $val defined $match{$_}
Truth table66%2/3
A || B
A= defined $valB= defined $match{$_}
ABresult
1X1
011
000
Truth table66%2/3
A && B
A= defined $valB= defined $match{$_}
ABresult
0X0
100
111
99 }
100 else {
101 0 0;
102 }
103 }
104 95 keys %match;
105 } 0 .. @$subs - 1;
106 }
107 }
108  
109 #pod =method find
110 #pod
111 #pod my @subs = $subs->find( $coderef | %spec );
112 #pod
113 #pod Returns a list of hashrefs containing attributes for subscriptions
114 #pod which match the passed arguments.
115 #pod
116 #pod A single argument must be a coderef; it will be invoked with a
117 #pod L<Net::Peer::Subscription> object as an argument. It should return
118 #pod true if it matches, false otherwise.
119 #pod
120 #pod If a hash is passed, its values are compared to the attributes of
121 #pod subscriptions in the list.
122 #pod
123 #pod =cut
124  
125 9 ▶sub find {
Subroutine find: called CC=2 SCAR=7.0
Pod documented
126  
127 9 my $self = shift;
128  
129 9 my $subs = $self->_subscriptions;
130  
131 9 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 9 9
132  
133 9 my @indices = $self->_find_index( @_ );
134 9 return map { $_->as_hashref } @{$subs}[@indices];
135 }
136  
137  
138 #pod =method remove
139 #pod
140 #pod @hashrefs = $subs->remove( $coderef | %spec );
141 #pod
142 #pod Unsubscribe the matching subscriptions, remove them from the list of
143 #pod subscriptions, and return hashrefs containing the subscriptions' event
144 #pod names and peers.
145 #pod
146 #pod
147 #pod =cut
148  
149 180 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
150  
151 180 my $self = shift;
152  
153 180 my $subs = $self->_subscriptions;
154  
155 180 my @subs;
156  
157 180 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 75 105 180
158 # need to remove subscriptions from the back to front,
159 # or indices get messed up
160 75 my @indices = reverse sort $self->_find_index( @_ );
161  
162 75 @subs = reverse map { splice( @$subs, $_, 1 ) } @indices;
163 }
164  
165 else {
166 105 @subs = @$subs;
167 105 @$subs = ();
168 }
169  
170 180 $_->unsubscribe foreach @subs;
171  
172 180 return map { $_->as_hashref } @subs;
173 }
174  
175 20 1;
176 #
177 # This file is part of Net-Object-Peer
178 #
179 # This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory.
180 #
181 # This is free software, licensed under:
182 #
183 # The GNU General Public License, Version 3, June 2007
184 #
185  
186 =pod
187  
188 =head1 NAME
189  
190 Net::Object::Peer::Subscriptions - A collection of Net::Object::Peer::Subscriptions
191  
192 =head1 VERSION
193  
194 version 0.06
195  
196 =head1 DESCRIPTION
197  
198 A B<Net::Object::Peer::Subscriptions> object manages a collection
199 of L<Net::Object::Peer::Subscriptions> objects.
200  
201 =head1 METHODS
202  
203 =head2 list
204  
205 @subs = $subs->list;
206  
207 Returns a list of hashrefs containing attributes for all subscriptions.
208  
209 =head2 nelem
210  
211 $nelem = $subs->nelem;
212  
213 return the number of elements in the list of subscriptions.
214  
215 =head2 add
216  
217 $subs->add( %attr );
218  
219 Add a subscription. See L<Net::Object::Peer::Subscription> for the
220 supported attributes.
221  
222 =head2 find
223  
224 my @subs = $subs->find( $coderef | %spec );
225  
226 Returns a list of hashrefs containing attributes for subscriptions
227 which match the passed arguments.
228  
229 A single argument must be a coderef; it will be invoked with a
230 L<Net::Peer::Subscription> object as an argument. It should return
231 true if it matches, false otherwise.
232  
233 If a hash is passed, its values are compared to the attributes of
234 subscriptions in the list.
235  
236 =head2 remove
237  
238 @hashrefs = $subs->remove( $coderef | %spec );
239  
240 Unsubscribe the matching subscriptions, remove them from the list of
241 subscriptions, and return hashrefs containing the subscriptions' event
242 names and peers.
243  
244 =head1 AUTHOR
245  
246 Diab Jerius <djerius@cpan.org>
247  
248 =head1 COPYRIGHT AND LICENSE
249  
250 This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory.
251  
252 This is free software, licensed under:
253  
254 The GNU General Public License, Version 3, June 2007
255  
256 =cut
257  
258 __END__
↑ Summary Peer.pm »