MenuGrinder.pm untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 269 CC 23 SCAR 34.8
CC
23
Cov
0%
load_plugin
72.0
load_plugins
72.0
get_menu
20.0
CRAP 32.4
1 package WWW::MenuGrinder;
2 BEGIN {
3 $WWW::MenuGrinder::VERSION = '0.06';
4 }
5  
6 # ABSTRACT: A tool for managing dynamic website menus - base class.
7  
8 use strict;
9 use warnings;
10  
11 use Moose;
12  
13 use WWW::MenuGrinder::Role::Plugin;
14 use WWW::MenuGrinder::Visitor;
15  
16  
17 has 'menu' => (
18 is => 'rw',
19 );
20  
21  
22 has 'plugins' => (
23 is => 'rw',
24 isa => 'ArrayRef[WWW::MenuGrinder::Role::Plugin]',
25 default => sub { [] },
26 );
27  
28 has 'plugin_hash' => (
29 is => 'rw',
30 default => sub { + {} },
31 );
32  
33 has 'loader' => (
34 is => 'rw',
35 );
36  
37 has 'on_load_plugins' => (
38 is => 'rw',
39 default => sub { [] },
40 );
41  
42 has 'per_request_plugins' => (
43 is => 'rw',
44 default => sub { [] },
45 );
46  
47 has 'outputs' => (
48 is => 'rw',
49 default => sub { [] },
50 );
51  
52 has 'outputs_by_name' => (
53 is => 'rw',
54 default => sub { + {} },
55 );
56  
57 has 'config' => (
58 is => 'rw',
59 default => sub { + {} },
60 );
61  
62  
63 sub rolename {
64 my ($name) = @_;
65  
66 return __PACKAGE__ . "::Role::$name";
67 }
68  
69  
70 sub plugins_with {
71 my ($self, $role) = @_;
72  
73 if ($role =~ s/^-//) {
74 $role = rolename($role);
75 }
76  
77 return [ grep $_->does($role), @{ $self->plugins } ]
78 }
79  
80 sub _register_plugin {
81 my ($self, $class, $plugin) = @_;
82  
83 push @{ $self->plugins }, $plugin;
84 $self->plugin_hash->{$class} = $plugin;
85 }
86  
87 sub _ensure_loaded {
88 my ($self, $class) = @_;
89  
90 my $file = $class . '.pm';
91 $file =~ s{::}{/}g;
92  
93 return 1 if $INC{$file};
94  
95 return eval qq{CORE::require(\$file)};
96 }
97  
98  
99 sub load_plugin {
100 my ($self, $class) = @_;
101  
102 my $shortname;
103  
104 if ($class =~ /^\+/) {
105 $class =~ s/^\+//;
106 } else {
107 $shortname = $class;
108 $class =~ s/^/WWW::MenuGrinder::Plugin::/;
109 }
110  
111 return $self->plugin_hash->{$class} if $self->plugin_hash->{$class};
112  
113 $self->_ensure_loaded($class) or die $@;
114  
115 if ($class->can('plugin_depends')) {
116 my @deps = $class->plugin_depends;
117 for my $dep (@deps) {
118 eval {
119 $self->load_plugin($dep);
120 };
121 if ($@) {
122 die "$@ while loading $dep, which was required by $class";
123 };
124 }
125 }
126  
127 my %plugin_config;
128  
129 if (defined $shortname) {
130 my $config = $self->config->{$shortname};
131 %plugin_config = %$config if defined $config;
132 } else {
133 my $config = $self->config->{$class};
134 %plugin_config = %$config if defined $config;
135 }
136  
137 my $plugin = $class->new( %plugin_config, grinder => $self );
138  
139 $plugin->verify_plugin;
140  
141 $self->_register_plugin($class, $plugin);
142 return $plugin;
143 }
144  
145  
146 # Load and verify all of the plugins in the config.
147 sub load_plugins {
148 my ($self, @args) = @_;
149  
150 my $plugins = $self->config->{plugins};
151  
152 my $loader = $plugins->{loader};
153 die "config->{plugins}{loader} is mandatory" unless defined $loader;
154 my $loaderclass = $self->load_plugin($loader);
155 die "Specified plugin $loader is not a Loader" unless $loaderclass->does(rolename('Loader'));
156 $self->loader($loaderclass);
157  
158 my $on_load = $plugins->{on_load} || [];
159 for my $name (@$on_load) {
160 my $plugin = $self->load_plugin($name);
161 die "On-load plugin $name is not a Mogrifier or ItemMogrifier"
162 unless $plugin->does(rolename('Mogrifier')) or $plugin->does(rolename('ItemMogrifier'));
163 push @{ $self->on_load_plugins }, $plugin;
164 }
165  
166 my $per_request = $plugins->{per_request} || [];
167 for my $name (@$per_request) {
168 my $plugin = $self->load_plugin($name);
169 die "Per-request plugin $name is not a Mogrifier, ItemMogrifier, or BeforeMogrify"
170 unless $plugin->does(rolename('Mogrifier')) or $plugin->does(rolename('ItemMogrifier')) or $plugin->does(rolename('BeforeMogrify'));
171 push @{ $self->per_request_plugins }, $plugin;
172 }
173  
174 my $outputs = $plugins->{outputs};
175 $outputs = [ $plugins->{output} ] if !defined $outputs && defined $plugins->{output};
176 $outputs = [] if !defined $outputs;
177  
178 for my $output (@$outputs) {
179 my $plugin = $self->load_plugin($output);
180 die "Specified plugin $output is not an Output" unless $plugin->does(rolename('Output'));
181 $self->outputs_by_name->{$output} = $plugin;
182 push @{ $self->outputs }, $plugin;
183 }
184  
185 }
186  
187  
188 sub init_menu {
189 my ($self) = @_;
190  
191 my $menu = $self->loader->load;
192 $menu = $self->mogrify( $menu, 'on-load', @{ $self->on_load_plugins } );
193 $self->menu($menu);
194 $_->on_init($menu) for @{ $self->plugins_with(-OnInit) };
195 }
196  
197 sub BUILD {
198 my ($self) = @_;
199  
200 $self->load_plugins;
201 $self->init_menu;
202 }
203  
204 # Remove items from the beginning of an array that pass some test and return
205 # them. Stop as soon as we find an item that fails the test.
206 sub _remove_initial_subsequence (&\@) {
207 my ($criterion, $arr) = @_;
208 my @ret;
209  
210 while (@$arr && do { local $_ = $arr->[0]; $criterion->() }) {
211 push @ret, shift @$arr;
212 }
213  
214 return @ret;
215 }
216  
217  
218 sub mogrify {
219 my ($self, $menu, $stage, @plugins) = @_;
220  
221 # warn "$stage: ", (join ", ", map ref $_, @plugins), "\n";
222  
223 # We've got a list of plugins that are to run at this stage.
224 # There are two kinds of p
225 while (@plugins) {
226 my @im = _remove_initial_subsequence { $_->does(rolename('ItemMogrifier')) } @plugins;
227 @im = map +{
228 plugin => $_,
229 methods => [ $_->item_mogrify_methods ],
230 }, @im;
231  
232 # Process the first method of every plugin, then the second method of every
233 # plugin, then the third etc. until there are no more.
234 while (@im) {
235 my @actions = map +{
236 plugin => $_->{plugin},
237 method => shift( @{ $_->{methods} } ),
238 }, @im;
239  
240 $menu = WWW::MenuGrinder::Visitor->visit_menu($menu, \@actions);
241  
242 @im = grep @{ $_->{methods} }, @im;
243 }
244  
245 last unless @plugins;
246  
247 my $mogrifier = shift @plugins;
248  
249 if ($mogrifier->does(rolename('Mogrifier'))) {
250 $menu = $mogrifier->mogrify($menu);
251 }
252 }
253  
254 return $menu;
255 }
256  
257  
258 sub get_menu {
259 my ($self, $outputtype) = @_;
260  
261 $_->before_mogrify($self->menu) for @{ $self->plugins_with(-BeforeMogrify) };
262  
263 my $menu = $self->menu;
264  
265 $menu = $self->mogrify( $menu, 'per-request', @{ $self->per_request_plugins } );
266  
267 if (!defined $outputtype) {
268 if (@{ $self->outputs } == 1) {
269 return $self->outputs->[0]->output($menu);
270 } else {
271 return $menu;
272 }
273 }
274  
275 my $output = $self->outputs_by_name->{$outputtype};
276 die "Output plugin $outputtype does not exist" unless defined $output;
277 return $output->output($menu);
278 }
279  
280 sub cleanup {
281 my ($self, $menu) = @_;
282  
283 for my $plugin (@{ $self->plugins }) {
284 $plugin->cleanup() if $plugin->can('cleanup');
285 }
286 }
287  
288 __PACKAGE__->meta->make_immutable;
289  
290 no Moose;
291 1;
292  
293  
294  
295 __END__