Zing/Channel.pm

filter: Statementstmt 53.5% 30 / 56 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 7 / 14 Podpod 85.7% 6 / 7 totaltotal 51.8% 43 / 83 CC 4 SCAR 11.0
CC
4
Cov
48%
recv
6.0
renew
6.0
send
6.0
CRAP 3.0
« Zing/Store.pm ↑ Summary Zing/Queue.pm »
1 package Zing::Channel;
2  
3 49 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 49 ▶use registry 'Zing::Types';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use routines;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 49 ▶use Data::Object::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 49 extends 'Zing::PubSub';
14  
15 49 ▶use Zing::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 49 our $VERSION = '0.11'; # VERSION
18  
19 # BUILDERS
20  
21 0 ▶fun BUILD($self) {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
22 0 $self->{cursor} = $self->size;
23  
24 0 return $self;
25 49 }
26  
27 # METHODS
28  
29 0 ▶method recv() {
Subroutine recv: not called CC=2 SCAR=17.9
Pod documented
30 $self->{cursor}++ if (
31 0 ▶ my $data = $self->store->slot($self->term, int($self->{cursor}))
Branch: 1 branch
BranchTrueFalseTotal
if my $data = $self->store->slot($self->term, int $self->{"cursor"}) 0 0 0
32 );
33 0 return $data;
34 49 }
35  
36 0 ▶method renew() {
Subroutine renew: not called CC=2 SCAR=17.9
Pod documented
37 0 ▶ return !($self->{cursor} = 0) if $self->{cursor} > $self->size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cursor"} > $self->size 0 0 0
38 0 return 0;
39 49 }
40  
41 0 ▶method reset() {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
42 0 return !($self->{cursor} = 0);
43 49 }
44  
45 0 ▶method send(HashRef $val) {
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_plain_hashref($val) or Function::Parameters::_croak('In method send: parameter 1 ($val): ' . ${my $v = bless( {"_is_core" => 1,"_overload_coderef" => sub { package Sub::Defer; use warnings; use strict; $undeferred ||= undefer_sub($deferred_info->[4]); goto \&$undeferred;},"_overload_coderef_no_rebuild" => 1,"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_HashRef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"coercion_generator" => sub { package Types::Standard::HashRef; no warnings; use strict; my($parent, $child, $param) = @_; return unless $param->has_coercion; my $coercable_item = $param->coercion->_source_type_union; my $C = 'Type::Coercion'->new('type_constraint', $child); if ($param->coercion->can_be_inlined and $coercable_item->can_be_inlined) { $C->add_type_coercions($parent, &Types::Standard::Stringable(sub { my @code; push @code, 'do { my ($orig, $return_orig, %new) = ($_, 0);'; push @code, 'for (keys %$orig) {'; push @code, sprintf('$return_orig++ && last unless (%s);', $coercable_item->inline_check('$orig->{$_}')); push @code, sprintf('$new{$_} = (%s);', $param->coercion->inline_coercion('$orig->{$_}')); push @code, '}'; push @code, '$return_orig ? $orig : \\%new'; push @code, '}'; join $", @code; } )); } else { $C->add_type_coercions($parent, sub { my $value = @_ ? $_[0] : $_; my %new; foreach my $k (keys %$value) { return $value unless $coercable_item->check($value->{$k}); $new{$k} = $param->coerce($value->{$k}); } return \%new; } ); } return $C;},"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref $_ eq 'HASH';},"constraint_generator" => sub { package Types::Standard::HashRef; no warnings; use strict; return &Types::Standard::HashRef() unless @_; require Error::TypeTiny::WrongNumberOfParameters; Type::Tiny::check_parameter_count_for_parameterized_type('Types::Standard', 'HashRef', \@_, 1); my $param = shift(); Types::TypeTiny::is_TypeTiny($param) or _croak "Parameter to HashRef[`a] expected to be a type constraint; got $param"; my $param_compiled_check = $param->compiled_check; my $xsub; do { my $paramname = Type::Tiny::XS::is_known($param_compiled_check); $xsub = Type::Tiny::XS::get_coderef_for("HashRef[$paramname]") if $paramname }; return sub { my $hash = shift(); ($param->check($_) or return) foreach (values %$hash); return !0; } , $xsub;},"deep_explanation" => sub { package Types::Standard::HashRef; no warnings; use strict; require B; my($type, $value, $varname) = @_; my $param = $type->parameters->[0]; foreach my $k (sort keys %$value) { my $item = $value->{$k}; next if $param->check($item); return [sprintf('"%s" constrains each value in the hash with "%s"', $type, $param), @{$param->validate_explain($item, sprintf('%s->{%s}', $varname, B::perlstring($k)));}]; } return;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 544,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "HashRef","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => sub { package Types::Standard::HashRef; no warnings; use strict; my $param = shift(); my $compiled = $param->compiled_check; my $xsubname; unless ($Type::Tiny::AvoidCallbacks) { my $paramname = Type::Tiny::XS::is_known($compiled); $xsubname = Type::Tiny::XS::get_subname_for("HashRef[$paramname]"); } return unless $param->can_be_inlined; return sub { my $v = $_[1]; return "$xsubname($v)" if $xsubname and not $Type::Tiny::AvoidCallbacks; my $p = &Types::Standard::HashRef()->inline_check($v); my $param_check = $param->inline_check('$i'); "$p and do { " . 'my $ok = 1; ' . "for my \$i (values %{$v}) { " . "(\$ok = 0, last) unless $param_check " . '}; ' . '$ok ' . '}'; } ;},"inlined" => sub { package Types::Standard; no warnings; use strict; !$Type::Tiny::AvoidCallbacks ? "Ref::Util::XS::is_plain_hashref($_[1])" : "ref($_[1]) eq 'HASH'";},"library" => "Types::Standard","my_methods" => {"hashref_allows_key" => sub { package Types::Standard::HashRef; no warnings; use strict; my $self = shift(); Types::Standard::is_Str($_[0]);},"hashref_allows_value" => sub { package Types::Standard::HashRef; no warnings; use strict; my $self = shift(); my($key, $value) = @_; return !1 unless $self->my_hashref_allows_key($key); return !0 if $self == &Types::Standard::HashRef(); my $href = $self->find_parent(sub { $_->has_parent and $_->parent == &Types::Standard::HashRef(); } ); my $param = $href->type_parameter; Types::Standard::is_Str($key) and $param->check($value);}},"name" => "HashRef","name_generator" => sub { package Type::Tiny; use warnings; use strict; defined $_ and s/[\x00-\x1F]//gms foreach (my($s, @a) = @_); sprintf '%s[%s]', $s, join(',', map((!defined($_) ? 'undef' : (!ref($_) && /\W/ ? B::perlstring($_) : $_)), @a));},"parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; not ref $_[0] and _failed_check(16, 'Ref', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Ref","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref $_;},"constraint_generator" => sub { package Types::Standard; no warnings; use strict; return $meta->get_type('Ref') unless @_; Type::Tiny::check_parameter_count_for_parameterized_type('Types::Standard', 'Ref', \@_, 1); my $reftype = shift(); $reftype =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE|FORMAT|IO|VSTRING|REGEXP|Regexp)$/i or _croak "Parameter to Ref[`a] expected to be a Perl ref type; got $reftype"; $reftype = "$reftype"; return sub { ref $_[0] and builtin::reftype($_[0]) eq $reftype; } ;},"deep_explanation" => sub { package Types::Standard; no warnings; use strict; require B; my($type, $value, $varname) = @_; my $param = $type->parameters->[0]; return if $type->check($value); my $reftype = builtin::reftype($value); return [sprintf('"%s" constrains reftype(%s) to be equal to %s', $type, $varname, B::perlstring($param)), sprintf('reftype(%s) is %s', $varname, defined $reftype ? B::perlstring($reftype) : 'undef')];},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 438,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Ref","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => sub { package Types::Standard; no warnings; use strict; my $reftype = shift(); return sub { my $v = $_[1]; &$maybe_load_modules('Scalar::Util', "ref($v) and Scalar::Util::reftype($v) eq q($reftype)"); } ;},"inlined" => sub { package Types::Standard; no warnings; use strict; "!!ref($_[1])";},"library" => "Types::Standard","name" => "Ref","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; defined $_[0] or _failed_check(7, 'Defined', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Defined","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; defined $_[0] and _failed_check(6, 'Undef', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Undef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => {},"constraint" => sub { package Types::Standard; no warnings; use strict; not defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 254,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Undef","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "!defined($_[1])";},"library" => "Types::Standard","name" => "Undef","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Item","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Type::Tiny; use warnings; use strict; !0;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 226,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Item","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Item","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => do{my $o},"display_name" => "to_Any","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complement_name" => "None","constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 220,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Any","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Any","type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 1}, 'Type::Tiny' ),"uniq" => 2}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 6}, 'Type::Tiny' ),"constraint" => sub { package Types::Standard; no warnings; use strict; defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 262,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Defined","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1])";},"library" => "Types::Standard","name" => "Defined","parent" => {},"uniq" => 7}, 'Type::Tiny' ),"uniq" => 16}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return {};},"type_default_generator" => sub { package Types::Standard; no warnings; use strict; return $Type::Tiny::parameterize_type->type_default if @_ < 2; return undef;},"uniq" => 22}, 'Type::Tiny' );$v->{"coercion"}{"type_constraint"} = $v;$v->{"compiled_type_constraint"} = $v->{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"};$v->{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"};$v->{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"};$v->{"parent"}{"parent"}{"complementary_type"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"complementary_type"} = $v->{"parent"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"constraint"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parent"}{"parent"}{"parent"} = $v->{"parent"}{"parent"}{"complementary_type"}{"parent"}; \$v}->get_message($val)) 0 0 0
46 0 return $self->store->push($self->term, $val);
47 49 }
48  
49 0 ▶method size() {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
50 0 return $self->store->size($self->term);
51 49 }
52  
53 0 ▶method term() {
Subroutine term: not called CC=1 SCAR=6.9
Pod documented
54 0 return Zing::Term->new($self)->channel;
55 49 }
56  
57 49 1;
58  
59 =encoding utf8
60  
61 =head1 NAME
62  
63 Zing::Channel - Shared Communication
64  
65 =cut
66  
67 =head1 ABSTRACT
68  
69 Multi-process Communication Mechanism
70  
71 =cut
72  
73 =head1 SYNOPSIS
74  
75 use Zing::Channel;
76  
77 my $chan = Zing::Channel->new(name => 'share');
78  
79 # $chan->recv;
80  
81 =cut
82  
83 =head1 DESCRIPTION
84  
85 This package provides represents a mechanism of interprocess communication and
86 synchronization via message passing.
87  
88 =cut
89  
90 =head1 INHERITS
91  
92 This package inherits behaviors from:
93  
94 L<Zing::PubSub>
95  
96 =cut
97  
98 =head1 LIBRARIES
99  
100 This package uses type constraints from:
101  
102 L<Zing::Types>
103  
104 =cut
105  
106 =head1 ATTRIBUTES
107  
108 This package has the following attributes:
109  
110 =cut
111  
112 =head2 name
113  
114 name(Str)
115  
116 This attribute is read-only, accepts C<(Str)> values, and is optional.
117  
118 =cut
119  
120 =head1 METHODS
121  
122 This package implements the following methods:
123  
124 =cut
125  
126 =head2 recv
127  
128 recv() : Maybe[HashRef]
129  
130 The recv method receives a single new message from the channel.
131  
132 =over 4
133  
134 =item recv example #1
135  
136 my $chan = Zing::Channel->new(name => 'recv-01');
137  
138 $chan->recv;
139  
140 =back
141  
142 =over 4
143  
144 =item recv example #2
145  
146 my $chan = Zing::Channel->new(name => 'recv-02');
147  
148 $chan->send({ status => 'works' });
149  
150 $chan->recv;
151  
152 =back
153  
154 =cut
155  
156 =head2 renew
157  
158 renew() : Int
159  
160 The renew method returns truthy if it resets the internal cursor, otherwise
161 falsy.
162  
163 =over 4
164  
165 =item renew example #1
166  
167 my $chan = Zing::Channel->new(name => 'renew-01');
168  
169 $chan->send({ status => 'works' }) for 1..5;
170  
171 $chan->renew;
172  
173 =back
174  
175 =over 4
176  
177 =item renew example #2
178  
179 my $chan = Zing::Channel->new(name => 'renew-02');
180  
181 $chan->send({ status => 'works' }) for 1..5;
182 $chan->recv;
183 $chan->drop;
184  
185 $chan->renew;
186  
187 =back
188  
189 =cut
190  
191 =head2 reset
192  
193 reset() : Int
194  
195 The reset method always reset the internal cursor and return truthy.
196  
197 =over 4
198  
199 =item reset example #1
200  
201 my $chan = Zing::Channel->new(name => 'reset-01');
202  
203 $chan->send({ status => 'works' }) for 1..5;
204 $chan->recv;
205 $chan->recv;
206  
207 $chan->reset;
208  
209 =back
210  
211 =cut
212  
213 =head2 send
214  
215 send(HashRef $value) : Int
216  
217 The send method sends a new message to the channel and return the message
218 count.
219  
220 =over 4
221  
222 =item send example #1
223  
224 my $chan = Zing::Channel->new(name => 'send-01');
225  
226 $chan->send({ status => 'works' });
227  
228 =back
229  
230 =cut
231  
232 =head2 size
233  
234 size() : Int
235  
236 The size method returns the message count of the channel.
237  
238 =over 4
239  
240 =item size example #1
241  
242 my $chan = Zing::Channel->new(name => 'size-01');
243  
244 $chan->send({ status => 'works' }) for 1..5;
245  
246 $chan->size;
247  
248 =back
249  
250 =cut
251  
252 =head2 term
253  
254 term() : Str
255  
256 The term method returns the name of the channel.
257  
258 =over 4
259  
260 =item term example #1
261  
262 my $chan = Zing::Channel->new(name => 'term-01');
263  
264 $chan->term;
265  
266 =back
267  
268 =cut
269  
270 =head1 AUTHOR
271  
272 Al Newkirk, C<awncorp@cpan.org>
273  
274 =head1 LICENSE
275  
276 Copyright (C) 2011-2019, Al Newkirk, et al.
277  
278 This is free software; you can redistribute it and/or modify it under the terms
279 of the The Apache License, Version 2.0, as elucidated in the L<"license
280 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
281  
282 =head1 PROJECT
283  
284 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
285  
286 L<Project|https://github.com/iamalnewkirk/zing>
287  
288 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
289  
290 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
291  
292 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
293  
294 L<Issues|https://github.com/iamalnewkirk/zing/issues>
295  
296 =cut
« Zing/Store.pm ↑ Summary Zing/Queue.pm »