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   18014 use strict;
  1         2  
  1         36  
2 1     1   4 use warnings;
  1         1  
  1         372  
3             package Net::MQTT::TopicStore;
4             $Net::MQTT::TopicStore::VERSION = '1.143260';
5             # ABSTRACT: Perl module to represent MQTT topic store
6              
7              
8             sub new {
9 9     9 1 1384 my $pkg = shift;
10 9         33 my $self = bless { topics => { } }, $pkg;
11 9         25 $self->add($_) foreach (@_);
12 9         35 $self
13             }
14              
15              
16             sub add {
17 10     10 1 14 my ($self, $topic_pattern) = @_;
18 10 50       29 unless (exists $self->{topics}->{$topic_pattern}) {
19 10         17 $self->{topics}->{$topic_pattern} = _topic_to_regexp($topic_pattern);
20             }
21             $topic_pattern
22 10         32 }
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 3442 my ($self, $topic) = @_;
33 19         23 my @res = ();
34 19         18 foreach my $t (keys %{$self->{topics}}) {
  19         51  
35 20         25 my $re = $self->{topics}->{$t};
36 20 100       175 next unless (defined $re ? $topic =~ $re : $topic eq $t);
    100          
37 13         31 push @res, $t;
38             }
39 19         76 return \@res;
40             }
41              
42             sub _topic_to_regexp {
43 10     10   11 my $topic = shift;
44 10         7 my $c;
45 10         19 $topic = quotemeta $topic;
46 10         21 $c += ($topic =~ s!\\/\\\+!\\/[^/]*!g);
47 10         23 $c += ($topic =~ s!\\/\\#$!(?:\$|/.*)!);
48 10         14 $c += ($topic =~ s!^\\\+\\/![^/]*\\/!g);
49 10         17 $c += ($topic =~ s!^\\\+$![^/]*!g);
50 10         13 $c += ($topic =~ s!^\\#$!.*!);
51 10 50       26 $topic .= '$' unless ($topic =~ m!\$$!);
52 10 100       18 unless ($c) {
53 1         5 return;
54             }
55             qr/^$topic/
56 9         193 }
57              
58             1;
59              
60             __END__