Ryu/Observable.pm

filter: Statementstmt 74.2% 75 / 101 Branchbran 61.1% 11 / 18 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 65.7% 23 / 35 Podpod 100% 14 / 14 totaltotal 69.6% 124 / 178 CC 15 SCAR 10.2
CC
15
Cov
70%
set_numeric
12.0
unsubscribe
6.0
notify_all
4.0
CRAP 2.8
« Ryu/Exception.pm ↑ Summary Ryu.pm »
1 package Ryu::Observable;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '4.001'; # VERSION
9 2 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Ryu::Observable - plus ça change
16  
17 =head1 SYNOPSIS
18  
19 # Set initial value
20 my $observed = Ryu::Observable->new(123)
21 # and a callback for any changes
22 ->subscribe(sub { print "New value, is now: $_\n" });
23 # Basic numeric increment/decrement should trigger a notification
24 ++$observed;
25 # To assign a new value, use ->set_numeric or ->set_string
26 $observed->set_numeric(88);
27  
28 =head1 DESCRIPTION
29  
30 Simple monitorable variables.
31  
32 =cut
33  
34 use overload
35 1 ▶ '""' => sub { shift->as_string },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 ▶ '0+' => sub { shift->as_number },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶ '++' => sub { my $v = ++$_[0]->{value}; $_[0]->notify_all; $v },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 0 ▶ '--' => sub { my $v = --$_[0]->{value}; $_[0]->notify_all; $v },
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 6 ▶ 'bool' => sub { !!shift->{value} },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 fallback => 1;
41  
42 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use List::UtilsBy;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use Ryu::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # Slightly odd way of applying this - we don't want to require Sentinel,
48 # but the usual tricks of ->import or using *Sentinel::sentinel directly
49 # only work for the pure-perl version. So, we try to load it, making the
50 # syntax available, and we then use sentinel() as if it were a function...
51 # providing a fallback *sentinel only when the load failed.
52 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
53 eval {
54 2 require Sentinel;
55 0 Sentinel->import;
56 0 1
57 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Sentinel; "Sentinel"->import; 1 } } or do { *sentinel = sub { die "This requires the Sentinel module to be installed"; } } 0 2 2
58 0 ▶ *sentinel = sub { die 'This requires the Sentinel module to be installed' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
59 }
60 }
61  
62 =head1 METHODS
63  
64 Public API, such as it is.
65  
66 =head2 as_string
67  
68 Returns the string representation of this value.
69  
70 =cut
71  
72 1 ▶sub as_string { '' . shift->{value} }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
73  
74 =head2 as_number
75  
76 =head2 as_numeric
77  
78 Returns the numeric representation of this value.
79  
80 (this method is available as C<as_number> or C<as_numeric>, both operate the same way)
81  
82 =cut
83  
84 1 ▶sub as_number { 0 + shift->{value} }
Subroutine as_number: called CC=1 SCAR=0.0
Pod documented
85  
86 2 *as_numeric = *as_number;
87  
88 =head2 new
89  
90 Instantiates with the given value.
91  
92 my $observed = Ryu::Observable->new('whatever');
93  
94 =cut
95  
96 8 ▶sub new { bless { value => $_[1] }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
97  
98 =head2 subscribe
99  
100 Requests notifications when the value changes.
101  
102 my $observed = Ryu::Observable->new('whatever')
103 ->subscribe(sub { print "New value - $_\n" });
104  
105 =cut
106  
107 1 ▶sub subscribe { my $self = shift; push @{$self->{subscriptions}}, @_; $self }
Subroutine subscribe: called CC=1 SCAR=0.0
Pod documented
108  
109 =head2 unsubscribe
110  
111 Removes an existing callback.
112  
113 my $code;
114 my $observed = Ryu::Observable->new('whatever')
115 ->subscribe($code = sub { print "New value - $_\n" })
116 ->set_string('test')
117 ->unsubscribe($code);
118  
119 =cut
120  
121 0 ▶sub unsubscribe {
Subroutine unsubscribe: not called CC=2 SCAR=17.9
Pod documented
122 0 my ($self, @code) = @_;
123 0 for my $addr (map Scalar::Util::refaddr($_), @code) {
124 0 ▶ List::UtilsBy::extract_by { Scalar::Util::refaddr($_) == $addr } @{$self->{subscriptions}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 }
126 $self
127 0 }
128  
129 =head2 set
130  
131 Sets the value to the given scalar, then notifies all subscribers (regardless
132 of whether the value has changed or not).
133  
134 =cut
135  
136 1 ▶sub set { my ($self, $v) = @_; $self->{value} = $v; $self->notify_all }
Subroutine set: called CC=1 SCAR=0.0
Pod documented
137  
138 =head2 value
139  
140 Returns the raw value.
141  
142 =cut
143  
144 0 ▶sub value { shift->{value} }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
145  
146 =head2 set_numeric
147  
148 =head2 set_number
149  
150 Applies a new numeric value, and notifies subscribers if the value is numerically
151 different to the previous one (or if we had no previous value).
152  
153 Returns C<$self>.
154  
155 (this method is available as C<set_number> or C<set_numeric>, both operate the same way)
156  
157 =cut
158  
159 0 ▶sub set_numeric {
Subroutine set_numeric: not called CC=3 SCAR=24.8
Pod documented
160 0 my ($self, $v) = @_;
161 0 my $prev = $self->{value};
162 0 ▶ return $self if defined($prev) && $prev == $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev and $prev == $v 0 0 0
Condition0%0/3
defined $prev and $prev == $v
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prev and $prev == $v
defined $prev $prev == $v
Truth table0%0/3
A and B
A= defined $prevB= $prev == $v
ABresult
0X0
100
111
163 0 $self->{value} = $v;
164 0 $self->notify_all
165 }
166  
167 2 *set_number = *set_numeric;
168  
169 =head2 set_string
170  
171 Applies a new string value, and notifies subscribers if the value stringifies to a
172 different value than the previous one (or if we had no previous value).
173  
174 Returns C<$self>.
175  
176 =cut
177  
178 1 ▶sub set_string {
Subroutine set_string: called CC=3 SCAR=11.8
Pod documented
179 1 my ($self, $v) = @_;
180 1 my $prev = $self->{value};
181 1 ▶ return $self if defined($prev) && $prev eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev and $prev eq $v 0 1 1
Condition33%1/3
defined $prev and $prev eq $v
!ll&&!rl&&r
010
MC/DC0%0/2
defined $prev and $prev eq $v
defined $prev $prev eq $v
Truth table33%1/3
A and B
A= defined $prevB= $prev eq $v
ABresult
0X0
100
111
182 1 $self->{value} = $v;
183 1 $self->notify_all
184 }
185  
186 =head2 source
187  
188 Returns a L<Ryu::Source>, which will emit each new value
189 until the observable is destroyed.
190  
191 =cut
192  
193 2 ▶sub source {
Subroutine source: called CC=2 SCAR=6.9
Pod documented
194 2 my ($self) = @_;
195 2 ▶ return $self->{source} if $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"source"} 1 1 2
196 1 $self->{source} = my $src = Ryu::Source->new;
197 1 Scalar::Util::weaken(my $copy = $self);
198 1 ▶ $src->_completed->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
199 1 ▶ delete $copy->{source} if $copy
Branch: 1 branch
BranchTrueFalseTotal
if $copy 1 0 1
200 1 });
201 1 $src;
202 }
203  
204 =head1 LVALUE METHODS
205  
206 B<< These require L<Sentinel> to be installed >>.
207  
208 =head2 lvalue_str
209  
210 Returns a L<Sentinel> lvalue accessor for the string value.
211  
212 This can be used with refaliasing or C<foreach> loops to reduce typing:
213  
214 for($observable->lvalue_str) {
215 chomp;
216 s/_/-/g;
217 }
218  
219 Any attempt to retrieve or set the value will be redirected to L</as_string>
220 or L</set_string> as appropriate.
221  
222 =cut
223  
224 0 ▶sub lvalue_str : lvalue {
Subroutine lvalue_str: not called CC=1 SCAR=6.9
Pod documented
225 0 my ($self) = @_;
226 0 ▶ sentinel(get => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 0 return $self->as_string(shift);
228 0 ▶ }, set => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 return $self->set_string(shift);
230 0 });
231 }
232  
233 =head2 lvalue_num
234  
235 Returns a L<Sentinel> lvalue accessor for the numeric value.
236  
237 This can be used with refaliasing or C<foreach> loops to reduce typing:
238  
239 for($observable->lvalue_num) {
240 ++$_;
241 $_ *= 3;
242 }
243  
244 Any attempt to retrieve or set the value will be redirected to L</as_number>
245 or L</set_number> as appropriate.
246  
247 =cut
248  
249 0 ▶sub lvalue_num : lvalue {
Subroutine lvalue_num: not called CC=1 SCAR=6.9
Pod documented
250 0 my ($self) = @_;
251 0 ▶ sentinel(get => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
252 0 return $self->as_number(shift);
253 0 ▶ }, set => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 return $self->set_number(shift);
255 0 });
256 }
257  
258 =head2 finish
259  
260 Mark this observable as finished.
261  
262 No further events or notifications will be sent.
263  
264 =cut
265  
266 8 ▶sub finish {
Subroutine finish: called CC=3 SCAR=11.0
Pod documented
267 8 my $self = shift;
268 8 @{$self->{subscriptions}} = ();
269 8 ▶ if(my $src = $self->{source}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $src = $self->{"source"}) 1 7 8
270 1 ▶ $src->finish unless $src->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $src->is_ready 1 0 1
271 }
272 8 return $self;
273 }
274  
275 =head1 METHODS - Internal
276  
277 Don't use these.
278  
279 =head2 notify_all
280  
281 Notifies all currently-subscribed callbacks with the current value.
282  
283 =cut
284  
285 3 ▶sub notify_all {
Subroutine notify_all: called CC=4 SCAR=13.9
Pod documented
286 3 my $self = shift;
287 3 my $v = $self->{value};
288 3 ▶ $self->{source}->emit($v) if $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"source"} 1 2 3
289 3 for my $sub (@{$self->{subscriptions}}) {
290 2 $sub->($_) for $v;
291 }
292 $self
293 3 }
294  
295 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
296 8 my ($self) = @_;
297 8 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 8 8
298 8 $self->finish;
299 8 delete $self->{value};
300 8 return;
301 }
302  
303 2 1;
304  
305 __END__
« Ryu/Exception.pm ↑ Summary Ryu.pm »