Component/Component/Moosenize.pm untested

filter: Statementstmt 0.0% 0 / 266 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 487 CC 26 SCAR 28.0
CC
26
Cov
0%
_check_requires
56.0
check_requires
42.0
requires
20.0
CRAP 16.5
1 package Class::Component::Component::Moosenize;
2 use strict;
3 use warnings;
4  
5 sub import {
6 my($class, %args) = @_;
7 $class->NEXT( import => %args );
8  
9 my $role_pkg = "$class\::Role";
10 # instasll import method to Plugin base class
11 no strict 'refs';
12 *{"$role_pkg\::import"} = \&{"Class::Component::Component::Moosenize::Implement::inject_import"};
13 unshift @{"$role_pkg\::ISA"}, 'Class::Component::Component::Moosenize::Role';
14  
15 my $plugin_pkg = "$class\::Plugin";
16 # install register method or wrapping to Plugin base class
17 Class::Component::Component::Moosenize::Implement::install_register_method($plugin_pkg);
18 }
19  
20 package # hide pause
21 Class::Component::Component::Moosenize::Implement;
22  
23 use strict;
24 use warnings;
25  
26 use Carp::Clan qw/Class::Component/;
27 use Class::Inspector;
28 use UNIVERSAL::require;
29  
30 my $METADATA = {};
31 sub INSTALL_DEFAULT_METADATA {
32 +{
33 '%requires' => {},
34 '@requires_with' => [],
35 '$moosenized' => 0,
36 '%method_modifier_role' => {},
37 '%method_modifier_applied' => {},
38 };
39 }
40  
41 sub install_moose_methods {
42 my $pkg = shift;
43 $METADATA->{$pkg} ||= INSTALL_DEFAULT_METADATA;
44  
45 for my $method (qw/ requires requires_with_attributes with before after around /) {
46 no strict 'refs';
47 *{"$pkg\::$method"} = sub { unshift @_, $pkg; goto &$method };
48 }
49 }
50  
51 sub install_register_method {
52 my $pkg = shift;
53 $pkg->require or die $@;
54 $METADATA->{$pkg} ||= INSTALL_DEFAULT_METADATA;
55  
56 my $inject_register = \&inject_register;
57 no strict 'refs';
58 if (${"$pkg\::"}{register}) {
59 my $code = \&{"$pkg\::register"};
60 no warnings 'redefine';
61 *{"$pkg\::register"} = sub {
62 $code->(@_);
63 goto &inject_register;
64 };
65 } else {
66 my $code = "package $pkg;
67 *register = sub {
68 my \$class = shift;
69 \$class->SUPER::register(\@_);
70 unshift \@_, \$class;
71 goto \$inject_register;
72 };
73 ";
74 eval $code;## no critic
75 }
76 }
77  
78 # MyApp::Role->import
79 sub inject_import {
80 my $class = shift;
81 my $caller = caller;
82  
83 # instasll Moose like methods to caller class
84 install_moose_methods($caller);
85 $class->import_after($caller, @_);
86 }
87  
88 # MyApp::Plugin->register
89 sub inject_register {
90 my $self = shift;
91 my $caller = ref $self || $self;
92 return unless $METADATA->{$caller};
93 return if $METADATA->{$caller}->{'$moosenized'}++;
94 check_requires($self, $caller, @_);
95 }
96  
97 sub check_requires {
98 my($self, $caller, $c) = @_;
99  
100 my @error;
101 for my $class (reverse(@{ Class::Component::Implement->isa_list_cache($caller, $caller) }), $caller) {
102 next unless exists $METADATA->{$class};# Moosenize class only
103  
104 my %class_requires; # not role class requires
105 if (scalar(keys %{ $METADATA->{$class}->{'%requires'} }) && $caller ne $class) {
106 %class_requires = %{ $METADATA->{$class}->{'%requires'} };
107 } else {
108 next unless @{ $METADATA->{$class}->{'@requires_with'} };
109 }
110
111 for my $role (@{ $METADATA->{$class}->{'@requires_with'} }) {
112 while (my($method, $attr) = each %{ $METADATA->{$role}->{'%requires'} }) {
113 if (my $msg = _check_requires($self, $c, $role, $caller, $method, $attr)) {
114 push @error, $msg;
115 }
116 }
117 }
118  
119 while (my($method, $attr) = each %class_requires) {
120 if (my $msg = _check_requires($self, $c, $class, $caller, $method, $attr)) {
121 push @error, $msg;
122 }
123 }
124 }
125 @error and croak join("\n", @error);
126 }
127 sub _check_requires {
128 my($self, $c, $caller, $role, $method, $attr) = @_;
129  
130 my $code = $self->can($method);
131 unless ($code) {
132 return sprintf("'%s' requires the method '%s' to be implemented by '%s'", $role, $method, $caller);
133 }
134 return unless $attr;
135  
136 # set attribute
137 my $attributes;
138 if (ref $attr eq 'HASH') {
139 $attributes = [ $attr ]
140 } elsif (ref $attr eq 'ARRAY') {
141 $attributes = $attr;
142 } else {
143 croak 'unimplimented refarence type';
144 }
145  
146 # fetch attribute class, value and go
147 for my $data (@{ $attributes }) {
148 my($attribute, $value);
149 if (ref $data eq 'HASH') {
150 ($attribute, $value) = each %{ $data };
151 } else {
152 $attribute = $data;
153 }
154  
155 my $attr_class;
156 if (($attr_class = $attribute) =~ s/^\+//) {
157 $attr_class->require or croak $@;
158 } else {
159 $attr_class = Class::Component::Implement->pkg_require($c => "Attribute::$attribute");
160 }
161  
162 $attr_class->register($self, $c, $method, $value, $code);
163 }
164  
165 return;
166 }
167  
168 #
169 # moose like methods
170 #
171 sub requires {
172 my $caller = shift;
173 my %methods = (@_ == 1) ? ( $_[0] => undef ) :
174 ref $_[1] ? @_ :
175 map { $_ => undef } @_;
176 my $kaller = ref $caller || $caller;
177 return unless $METADATA->{$kaller};
178 while (my($key, $value) = each %methods) {
179 $METADATA->{$kaller}->{'%requires'}->{$key} = $value;
180 }
181 }
182 sub requires_with_attributes {
183 my $caller = shift;
184 my $attributes = shift;
185 requires($caller => map { $_ => $attributes } @_ );
186 }
187  
188 sub with {
189 my $caller = shift;
190 my $role = shift;
191 return unless $METADATA->{$caller};
192  
193 $role->require or croak $@;
194 my %has_methods = map { $_ => 1 } @{ Class::Inspector->functions($caller) };
195 for my $method (@{ Class::Inspector->functions($role) }) {
196 next if $has_methods{$method};
197 no strict 'refs';
198 *{"$caller\::$method"} = *{"$role\::$method"};
199 }
200 push @{ $METADATA->{$caller}->{'@requires_with'} }, $role;
201  
202 apply_method_modifier($caller, $role);
203 }
204  
205 sub before {
206 my $caller = shift;
207 my $code = pop;
208 add_method_modifier($caller, 'before', $_, $code) for @_;
209 }
210  
211 sub after {
212 my $caller = shift;
213 my $code = pop;
214 add_method_modifier($caller, 'after', $_, $code) for @_;
215 }
216  
217 sub around {
218 my $caller = shift;
219 my $code = pop;
220 add_method_modifier($caller, 'around', $_, $code) for @_;
221 }
222  
223  
224 #
225 # method modifier
226 #
227 sub apply_method_modifier {
228 my($caller, $role) = @_;
229 return unless $METADATA->{$role};
230  
231 # collect method modifier
232 my $apply_methods = {};
233 while (my($method, $methods) = each %{ $METADATA->{$role}->{'%method_modifier_role'} }) {
234 while (my($type, $codelist) = each %{ $methods }) {
235 $METADATA->{$caller}->{'%method_modifier_applied'}->{$method} ||= {};
236 $METADATA->{$caller}->{'%method_modifier_applied'}->{$method}->{$type} ||= [];
237 push @{ $METADATA->{$caller}->{'%method_modifier_applied'}->{$method}->{$type} }, @{ $codelist };
238 }
239 method_modifier($caller, $method);
240 }
241  
242 }
243  
244 sub add_method_modifier {
245 my($caller, $type, $method, $code) = @_;
246 return unless $METADATA->{$caller};
247  
248 if ($caller->isa('Class::Component::Component::Moosenize::Role')) {
249 $METADATA->{$caller}->{'%method_modifier_role'}->{$method} ||= {};
250 $METADATA->{$caller}->{'%method_modifier_role'}->{$method}->{$type} ||= [];
251 push @{ $METADATA->{$caller}->{'%method_modifier_role'}->{$method}->{$type} }, $code;
252 } else {
253 $METADATA->{$caller}->{'%method_modifier_applied'}->{$method} ||= {};
254 $METADATA->{$caller}->{'%method_modifier_applied'}->{$method}->{$type} ||= [];
255 push @{ $METADATA->{$caller}->{'%method_modifier_applied'}->{$method}->{$type} }, $code;
256  
257 method_modifier($caller, $method);
258 }
259 }
260  
261  
262 # copied from Class::MOP::Method::Wrapped
263 my $_build_wrapped_method = sub {
264 my $modifier_table = shift;
265 my ($before, $after, $around) = (
266 $modifier_table->{before},
267 $modifier_table->{after},
268 $modifier_table->{around},
269 );
270 if (@$before && @$after) {
271 $modifier_table->{cache} = sub {
272 $_->(@_) for reverse @{$before};
273 my @rval;
274 ((defined wantarray) ?
275 ((wantarray) ?
276 (@rval = $modifier_table->{around_cache}->(@_))
277 :
278 ($rval[0] = $modifier_table->{around_cache}->(@_)))
279 :
280 $modifier_table->{around_cache}->(@_));
281 $_->(@_) for @{$after};
282 return unless defined wantarray;
283 return wantarray ? @rval : $rval[0];
284 }
285 }
286 elsif (@$before && !@$after) {
287 $modifier_table->{cache} = sub {
288 $_->(@_) for reverse @{$before};
289 return $modifier_table->{around_cache}->(@_);
290 }
291 }
292 elsif (@$after && !@$before) {
293 $modifier_table->{cache} = sub {
294 my @rval;
295 ((defined wantarray) ?
296 ((wantarray) ?
297 (@rval = $modifier_table->{around_cache}->(@_))
298 :
299 ($rval[0] = $modifier_table->{around_cache}->(@_)))
300 :
301 $modifier_table->{around_cache}->(@_));
302 $_->(@_) for @{$after};
303 return unless defined wantarray;
304 return wantarray ? @rval : $rval[0];
305 }
306 }
307 else {
308 $modifier_table->{cache} = $modifier_table->{around_cache};
309 }
310 };
311 my $compile_around_method = sub {{
312 my $f1 = pop;
313 return $f1 unless @_;
314 my $f2 = pop;
315 push @_, sub { $f2->( $f1, @_ ) };
316 redo;
317 }};
318  
319 sub method_modifier {
320 my($caller, $method) = @_;
321 return unless $METADATA->{$caller};
322  
323 my $code = $caller->can($method);
324 croak sprintf("Could not load class (%s) because : The method '%s' is not found in the inheritance hierarchy for class %s", $caller, $method, $caller)
325 unless $code;
326  
327 my $modifier_table = $METADATA->{$caller}->{'%method_modifier_applied'}->{$method};
328 $modifier_table->{before} ||= [];
329 $modifier_table->{after} ||= [];
330 $modifier_table->{around} ||= [];
331 $modifier_table->{cache} ||= $code;
332 $modifier_table->{orig} ||= $code;
333 $modifier_table->{around_cache} ||= $code;
334  
335 if (@{ $modifier_table->{around} }) {
336 $modifier_table->{around_cache} = $compile_around_method->(
337 reverse(@{ $modifier_table->{around} }),
338 $modifier_table->{orig}
339 );
340 }
341  
342 $_build_wrapped_method->($modifier_table);
343  
344 no strict 'refs';
345 no warnings 'redefine';
346 *{"$caller\::$method"} = sub { goto $modifier_table->{cache} };
347 }
348  
349 package # hide pause
350 Class::Component::Component::Moosenize::Role;
351 use strict;
352 use warnings;
353 use Carp::Clan qw/Class::Component/;
354  
355 sub import_after {}
356 1;
357  
358  
359 __END__