MenuGrinder/Plugin/Visitor.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 130 CC 18 SCAR 48.8
CC
18
Cov
0%
_visit
210.0
pre_mogrify
12.0
mogrify
12.0
CRAP 131.2
1 package WWW::MenuGrinder::Plugin::Visitor;
2 our $VERSION = '0.01_01';
3  
4  
5 # ABSTRACT: WWW::MenuGrinder plugin that allows item-by-item mogrification.
6  
7 use Moose;
8  
9 use Scalar::Util;
10  
11 with 'WWW::MenuGrinder::Role::PreMogrifier';
12 with 'WWW::MenuGrinder::Role::Mogrifier';
13  
14 has item_premogrifiers => (
15 is => 'rw',
16 lazy => 1,
17 default => sub {
18 my $self = shift;
19 $self->grinder->plugins_with( -ItemPreMogrifier );
20 },
21 );
22  
23 has item_mogrifiers => (
24 is => 'rw',
25 lazy => 1,
26 default => sub {
27 my $self = shift;
28 $self->grinder->plugins_with( -ItemMogrifier );
29 },
30 );
31  
32 # Nothing on CPAN gives me a nice simple interface as well as post-order
33 # traversal so here's one we prepared earlier.
34  
35 # Traverse a tree-like data structure, making a copy of it and modifying it
36 # along the way.
37 sub _visit {
38 my ( $obj, $cb ) = @_;
39  
40 my $reftype = Scalar::Util::reftype($obj);
41  
42 if ( defined $reftype ) {
43 # Don't bother with any fancy inheritance checks, just mostly leave objects
44 # alone.
45 if (Scalar::Util::blessed($obj)) {
46 return $cb->{OBJECT}->($obj) if defined $cb->{OBJECT};
47 return $obj;
48 }
49 if ( $reftype eq 'HASH' ) {
50 my $tmp = {};
51 for my $key ( keys %{$obj} ) {
52 my @ret = _visit( $_[0]{$key}, $cb );
53 $tmp->{$key} = $ret[0] if @ret;
54 }
55  
56 return $cb->{HASH}->($tmp) if exists $cb->{HASH};
57 return $tmp;
58 } elsif ( $reftype eq 'ARRAY' ) {
59 my $tmp = [];
60 for my $val ( @{$obj} ) {
61 push @$tmp, _visit( $val, $cb );
62 }
63  
64 return $cb->{ARRAY}->($tmp) if exists $cb->{ARRAY};
65 return $tmp;
66 } elsif ( $reftype eq 'SCALAR' ) {
67 my $tmp = \( _visit( $$obj, $cb ) );
68  
69 return $cb->{SCALARREF}->($tmp) if exists $cb->{SCALARREF};
70 return $tmp;
71 } elsif ( $reftype eq 'GLOB' ) {
72 my $tmp = $obj;
73 return $cb->{GLOB}->($tmp) if exists $cb->{GLOB};
74 return $tmp;
75 } else {
76 warn "Ignoring a $reftype-reference I don't know how to handle.";
77 return $obj;
78 }
79 } else { # Not a reference
80 return $cb->{SCALAR}->($obj) if defined $cb->{SCALAR};
81 return $obj;
82 }
83 }
84  
85 sub pre_mogrify {
86 my ( $self, $menu ) = @_;
87  
88 my @ipm = map +{
89 plugin => $_,
90 methods => [ $_->item_pre_mogrify_methods ]
91 }, @{ $self->item_premogrifiers };
92  
93  
94 # If there aren't any ItemPreMogrifiers then don't bother doing anything.
95 return $menu unless @ipm;
96  
97 # Process each plugin's pass-1 method, then the pass-2 methods of eah plugin
98 # that has one, then pass-3 as necessary etc. until no more.
99 while (@ipm) {
100 $menu = _visit($menu, {
101 HASH => sub {
102 my ( $item ) = @_;
103 for my $ipm (@ipm) {
104 my $plugin = $ipm->{plugin};
105 my $method = $ipm->{methods}[0];
106 $item = $plugin->$method($item);
107 return () unless defined $item;
108 }
109 return $item;
110 }
111 }
112 );
113  
114 shift @{ $_->{methods} } for @ipm;
115 @ipm = grep @{ $_->{methods} }, @ipm;
116 }
117  
118 return $menu;
119 }
120  
121 sub mogrify {
122 my ( $self, $menu ) = @_;
123  
124 my @im = map +{
125 plugin => $_,
126 methods => [ $_->item_mogrify_methods ]
127 }, @{ $self->item_mogrifiers };
128  
129 # If there aren't any ItemMogrifiers then don't bother doing anything.
130 return $menu unless @im;
131  
132 # Process each plugin's pass-1 method, then the pass-2 methods of eah plugin
133 # that has one, then pass-3 as necessary etc. until no more.
134 while (@im) {
135 $menu = _visit($menu, {
136 HASH => sub {
137 my ( $item ) = @_;
138 for my $im (@im) {
139 my $plugin = $im->{plugin};
140 my $method = $im->{methods}[0];
141 $item = $plugin->$method($item);
142 return () unless defined $item;
143 }
144 return $item;
145 }
146 }
147 );
148  
149 shift @{ $_->{methods} } for @im;
150 @im = grep @{ $_->{methods} }, @im;
151 }
152  
153 return $menu;
154 }
155  
156 no Moose;
157 1;
158  
159  
160 __END__
161 =head1 NAME
162  
163 WWW::MenuGrinder::Plugin::Visitor - WWW::MenuGrinder plugin that allows item-by-item mogrification.
164  
165 =head1 VERSION
166  
167 version 0.01_01
168  
169 =head1 DESCRIPTION
170  
171 C<WWW::MenuGrinder::Plugin::Visitor> is a plugin for C<WWW::MenuGrinder>. You
172 should not use it directly, but include it in the C<plugins> section of a
173 C<WWW::MenuGrinder> config.
174  
175 In fact, you probably won't include it in a configuration either; the Visitor
176 plugin is usually loaded as a prerequisite of some other plugin. All
177 C<WWW::MenuGrinder> plugins implementing the C<ItemMogrifier> or
178 C<ItemPreMogrifier> interfaces make use of Visitor.
179  
180 =head1 AUTHOR
181  
182 Andrew Rodland <andrew@hbslabs.com>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is copyright (c) 2009 by HBS Labs, LLC..
187  
188 This is free software; you can redistribute it and/or modify it under
189 the same terms as perl itself.
190