File Coverage

lib/Dancer/RPCPlugin/PluginNames.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 8 75.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 47 49 95.9


line stmt bran cond sub pod time code
1             package Dancer::RPCPlugin::PluginNames;
2 11     11   125622 use warnings;
  11         36  
  11         338  
3 11     11   57 use strict;
  11         21  
  11         277  
4              
5 11     11   52 use constant DEFAULT_PLUGINS => [qw/ jsonrpc restrpc xmlrpc /];
  11         22  
  11         3149  
6              
7             my $_plugins;
8             sub new {
9 87     87 1 291 my $class = shift;
10              
11 87 100       261 if (!defined $_plugins) {
12 12 100       50 $_plugins = bless @_ ? [@_] : [ @{DEFAULT_PLUGINS()} ], $class;
  11         47  
13             }
14 87         293 return $_plugins;
15             }
16              
17 1     1   7 sub _reset { undef $_plugins }
18              
19             sub add_names {
20 1     1 1 418 my $self = shift;
21              
22 1         2 for my $new_plugin (@_) {
23 2 50       3 if (!grep $_ eq $new_plugin, @{$self}) {
  2         9  
24 2         4 push @{$self}, $new_plugin;
  2         5  
25             }
26             }
27 1         2 return $self;
28             }
29              
30             sub names {
31 92     92 1 900 my $self = shift;
32 92 50       147 return sort { length($b) <=> length($a) || $a cmp $b } @{$self};
  278         936  
  92         395  
33             }
34              
35             sub regex {
36 89     89 1 165 my $self = shift;
37 89         242 my $alts = join('|', $self->names);
38              
39 89         1117 return qr/(?:$alts)/;
40             }
41              
42             1;
43              
44             =head1 NAME
45              
46             Dancer::RPCPlugin::PluginNames - Register Dancer::Plugin::RPC plugin-names
47              
48             =head1 SYNOPSIS
49              
50             use Dancer::RPCPlugin::PluginNames;
51             my $pt = Dancer::RPCPlugin::PluginNames->new();
52              
53             say "Plugin: $_" for $pt->names;
54              
55             if ($my_name =~ $pt->regex) {
56             say "$my_name is a registered plugin-name";
57             }
58              
59             =head1 DESCRIPTION
60              
61             =head2 Dancer::RPCPlugin::PluginNames->new(@names)
62              
63             Returns a singleton-object of this class.
64              
65             =head3 Arguments
66              
67             List of names or none.
68              
69             =head2 $pn->add_names(@names)
70              
71             Adds the names given to the singleton-object and returns that.
72              
73             =head2 $pn->names
74              
75             Returns a list of registered plugin-names ordered by:
76              
77             =over
78              
79             =item 1. length of the name
80              
81             =item 2. ASCII-betical
82              
83             =back
84              
85             =head2 $pn->regex
86              
87             Returns a C<Regexp> object with all the names as alternatives.
88              
89             =head1 COPYRIGHT
90              
91             (c) MMXVII - Abe Timmerman <abetim@cpan.org>
92              
93             =cut