File Coverage

blib/lib/App/RecordStream/BaseRegistry.pm
Criterion Covered Total %
statement 39 71 54.9
branch 5 12 41.6
condition 0 2 0.0
subroutine 9 12 75.0
pod 0 8 0.0
total 53 105 50.4


line stmt bran cond sub pod time code
1             package App::RecordStream::BaseRegistry;
2              
3 69     69   20697 use App::RecordStream::Site;
  69         150  
  69         1658  
4              
5 69     69   351 use strict;
  69         126  
  69         990  
6 69     69   264 use warnings;
  69         121  
  69         1267  
7 69     69   25815 use Module::Pluggable::Object;
  69         456807  
  69         38189  
8              
9             sub load_implementations
10             {
11 46     46 0 269 my $registry_class = shift;
12              
13 46         198 my $subtree = $registry_class->subtree();
14              
15             # include sites, overriding lower priority with higher priority
16 46         261 App::RecordStream::Site::bootstrap();
17 46         197 my @sites = sort { $a->{'priority'} <=> $b->{'priority'} } App::RecordStream::Site::list_sites();
  0         0  
18              
19             Module::Pluggable::Object->new(
20             require => 1,
21             search_path => [
22             "App::RecordStream::$subtree",
23 46         484 map { "$_->{path}::$subtree" } @sites
  0         0  
24             ],
25             )->plugins;
26             }
27              
28             {
29             my %class_registry;
30              
31             sub register_implementation
32             {
33 332     332 0 584 my $registry_class = shift;
34 332         446 my $name = shift;
35 332         687 my $class = shift;
36              
37 332         1140 $class_registry{$registry_class}->{$name} = $class;
38             }
39              
40             sub parse_single_nameless_implementation
41             {
42 38     38 0 89 my $registry_class = shift;
43 38         58 my $spec = shift;
44              
45 38         109 my @spec = split(/,/, $spec);
46              
47 38 50       113 if(!@spec)
48             {
49 0         0 die "Bad " . $registry_class->typename() . " spec: " . $spec . "\n";
50             }
51              
52 38         80 my $aggr_name = shift(@spec);
53 38         126 my $class = $class_registry{$registry_class}->{$aggr_name};
54 38 50       114 if(!$class)
55             {
56 0         0 die "Bad " . $registry_class->typename() . ": " . $aggr_name . "\n";
57             }
58              
59 38         297 my $argct = $class->argct();
60 38 100       103 if(!ref($argct))
61             {
62 36         79 $argct = [$argct];
63             }
64 38 50       76 if(!(grep { $_ == @spec } @$argct))
  40         155  
65             {
66 0         0 print $class->long_usage();
67 0         0 exit 1;
68             }
69              
70 38         282 return $class->new(@spec);
71             }
72              
73             sub list_implementations
74             {
75 0     0 0 0 my $registry_class = shift;
76 0   0     0 my $prefix = shift || '';
77              
78 0         0 my %reverse;
79             my @classes;
80 0         0 for my $name (sort(keys(%{$class_registry{$registry_class}})))
  0         0  
81             {
82 0         0 my $class = $class_registry{$registry_class}->{$name};
83 0         0 my $ar = $reverse{$class};
84 0 0       0 if(!$ar)
85             {
86 0         0 $reverse{$class} = $ar = [];
87 0         0 push @classes, $class;
88             }
89 0         0 push @$ar, $name;
90             }
91 0         0 my $ret = "";
92 0         0 for my $class (@classes)
93             {
94 0         0 my $usage = $class->short_usage();
95 0         0 $ret .= $prefix . join(", ", @{$reverse{$class}}) . ": " . $usage . "\n";
  0         0  
96             }
97 0         0 return $ret;
98             }
99              
100             sub show_implementation
101             {
102 0     0 0 0 my $registry_class = shift;
103 0         0 my $name = shift;
104              
105 0         0 my $class = $class_registry{$registry_class}->{$name};
106 0 0       0 if(!$class)
107             {
108 0         0 print "Bad " . $registry_class->typename() . ": " . $name . "\n";
109 0         0 exit 1;
110             }
111              
112 0         0 print $class->long_usage();
113             }
114             }
115              
116             # subclasses may override these all if they wish
117              
118             sub subtree
119             {
120 46     46 0 102 my $registry_class = shift;
121              
122 46         180 return $registry_class->begin_sentence_typename();
123             }
124              
125             sub begin_sentence_typename
126             {
127 46     46 0 91 my $registry_class = shift;
128              
129 46         209 my $t = $registry_class->typename();
130              
131 46         224 return uc(substr($t, 0, 1)) . substr($t, 1);
132             }
133              
134             sub typename
135             {
136 0     0 0   my $registry_class = shift;
137              
138 0           die "BaseRegistry subclass $registry_class did not implement typename()";
139             }
140              
141             1;