File Coverage

blib/lib/IPC/PubSub/Publisher.pm
Criterion Covered Total %
statement 40 40 100.0
branch 3 4 75.0
condition 3 6 50.0
subroutine 10 10 100.0
pod 1 5 20.0
total 57 65 87.6


line stmt bran cond sub pod time code
1             package IPC::PubSub::Publisher;
2 2     2   10 use strict;
  2         5  
  2         50  
3 2     2   8 use warnings;
  2         3  
  2         56  
4 2     2   10 use base qw/Class::Accessor::Fast/;
  2         3  
  2         2293  
5              
6             __PACKAGE__->mk_accessors(qw/expiry/);
7             __PACKAGE__->mk_ro_accessors(qw/_indice uuid _cache/);
8              
9             sub new {
10 6     6 1 125 my $class = shift;
11 6         12 my $cache = shift;
12              
13 6         1929 require Data::UUID;
14              
15 6         6107 my $uuid = Data::UUID->new->create_b64;
16 12         89 my $self = bless({
17             expiry => 0,
18             _cache => $cache,
19 6         800 _indice => { map { $_ => 1 } @_ },
20             uuid => $uuid,
21             });
22 6         201 $cache->add_publisher($_, $uuid) for @_;
23 6         18668 return $self;
24             }
25              
26             sub channels {
27 28     28 0 15967 my $self = shift;
28             wantarray
29 28 100       296 ? keys(%{$self->_indice})
  12         44  
30             : $self->_indice;
31             }
32              
33             sub publish {
34 4     4 0 5491 my $self = shift;
35             $self->_indice->{$_} ||= do {
36 2         26 $self->_cache->add_publisher($_, $self->uuid);
37 2         2855 1;
38 4   66     23 } for @_;
39             }
40              
41             sub unpublish {
42 4     4 0 10375 my $self = shift;
43 4   33     23 delete($self->_indice->{$_}) and $self->_cache->remove_publisher($_, $self->uuid) for @_;
44             }
45              
46             sub msg {
47 10     10 0 7960 my $self = shift;
48 10         43 my $uuid = $self->uuid;
49 10         85 my $indice = $self->_indice;
50 10         65 my $expiry = $self->expiry;
51 10         59 foreach my $msg (@_) {
52 10         49 while (my ($channel, $index) = each %$indice) {
53 16         69 $self->_cache->put($channel, $uuid, $index, $msg, $expiry);
54 16         55672 $indice->{$channel} = $index+1;
55             }
56             }
57             }
58              
59 2     2   10628 no warnings 'redefine';
  2         5  
  2         220  
60             sub DESTROY {
61 6     6   14406 my $self = shift;
62 6 50       30 return unless $self->_cache;
63 6         81 $self->_cache->remove_publisher($_, $self->uuid) for $self->channels;
64             }
65              
66             1;