File Coverage

blib/lib/Net/MQTT/TopicStore.pm
Criterion Covered Total %
statement 34 36 94.4
branch 8 10 80.0
condition n/a
subroutine 6 7 85.7
pod 4 4 100.0
total 52 57 91.2


line stmt bran cond sub pod time code
1 1     1   20775 use strict;
  1         2  
  1         47  
2 1     1   7 use warnings;
  1         4  
  1         405  
3             package Net::MQTT::TopicStore;
4             $Net::MQTT::TopicStore::VERSION = '1.163170';
5             # ABSTRACT: Perl module to represent MQTT topic store
6              
7              
8             sub new {
9 9     9 1 1284 my $pkg = shift;
10 9         26 my $self = bless { topics => { } }, $pkg;
11 9         28 $self->add($_) foreach (@_);
12 9         64 $self
13             }
14              
15              
16             sub add {
17 10     10 1 14 my ($self, $topic_pattern) = @_;
18 10 50       25 unless (exists $self->{topics}->{$topic_pattern}) {
19 10         18 $self->{topics}->{$topic_pattern} = _topic_to_regexp($topic_pattern);
20             }
21             $topic_pattern
22 10         27 }
23              
24              
25             sub delete {
26 0     0 1 0 my ($self, $topic) = @_;
27 0         0 delete $self->{topics}->{$topic};
28             }
29              
30              
31             sub values {
32 19     19 1 3376 my ($self, $topic) = @_;
33 19         26 my @res = ();
34 19         17 foreach my $t (keys %{$self->{topics}}) {
  19         54  
35 20         24 my $re = $self->{topics}->{$t};
36 20 100       158 next unless (defined $re ? $topic =~ $re : $topic eq $t);
    100          
37 13         28 push @res, $t;
38             }
39 19         39 return \@res;
40             }
41              
42             sub _topic_to_regexp {
43 10     10   10 my $topic = shift;
44 10         9 my $c;
45 10         14 $topic = quotemeta $topic;
46 10         25 $c += ($topic =~ s!\\/\\\+!\\/[^/]*!g);
47 10         20 $c += ($topic =~ s!\\/\\#$!(?:\$|/.*)!);
48 10         14 $c += ($topic =~ s!^\\\+\\/![^/]*\\/!g);
49 10         14 $c += ($topic =~ s!^\\\+$![^/]*!g);
50 10         13 $c += ($topic =~ s!^\\#$!.*!);
51 10 50       24 $topic .= '$' unless ($topic =~ m!\$$!);
52 10 100       22 unless ($c) {
53 1         3 return;
54             }
55             qr/^$topic/
56 9         206 }
57              
58             1;
59              
60             __END__