Scaffold.pm

filter: Statementstmt 79.3% 92 / 116 Branchbran 50.0% 21 / 42 Conditioncond 48.0% 12 / 25 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 76.0% 19 / 25 Podpod 75.0% 9 / 12 totaltotal 66.1% 156 / 236 CC 31 SCAR 25.5
CC
31
Cov
68%
repackage
56.0
scaffold
14.9
load_package
12.0
CRAP 12.8
↑ Summary Scaffold/Class.pm »
1 package MooseX::Scaffold;
2  
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 MooseX::Scaffold - Template metaprogramming with Moose
9  
10 =head1 VERSION
11  
12 Version 0.05
13  
14 =cut
15  
16 10 our $VERSION = '0.05';
17  
18 =head1 SYNOPSIS
19  
20 package MyScaffolder;
21  
22 use MooseX::Scaffold;
23  
24 MooseX::Scaffold->setup_scaffolding_import;
25  
26 sub SCAFFOLD {
27 my $class = shift; my %given = @_;
28  
29 $class->has($given{kind} => is => 'ro', isa => 'Int', required => 1);
30  
31 # Using MooseX::ClassAttribute
32 $class->class_has(kind => is => 'ro', isa => 'Str', default => $given{kind});
33 }
34  
35 package MyAppleClass;
36  
37 use Moose;
38 use MooseX::ClassAttribute;
39 use MyScaffolder kind => 'apple';
40  
41 package MyBananaClass;
42  
43 use Moose;
44 use MooseX::ClassAttribute;
45 use MyScaffolder kind => 'banana';
46  
47 # ... meanwhile, back at the Batcave ...
48  
49 use MyAppleClass;
50 use MyBananaClass;
51  
52 my $apple = MyAppleClass->new(apple => 1);
53 my $banana = MyBananaClass->new(banana => 2);
54  
55 =head1 DESCRIPTION
56  
57 MooseX::Scaffold is a tool for creating or augmenting Moose classes on-the-fly.
58  
59 Scaffolding can be triggered when a C<use> is executed (any import arguments are passed
60 to the scaffold subroutine) or you can explicitly call MooseX::Scaffold->scaffold with the scaffolding
61 subroutine and the package name for the class.
62  
63 Depending on what you're trying to do, MooseX::Scaffold can behave in three different ways (Assume My::Class is the class
64 you're trying to create/augment):
65  
66 load_and_scaffold (scaffold) - Attempt to require My::Class from My/Class.pm or do Moose::Meta::Class->create('My::Class')
67 to make the package on-the-fly. Scaffold the result.
68  
69 load_or_scaffold (load) - Attempt to require My::Class from My/Class.pm and stop if that works. If no My/Class.pm is
70 found in @INC, then make a Moose class on-the-fly and scaffold it.
71 This option can be used to create a default class if one isn't found.
72  
73 scaffold_without_load - Don't attempt to require My::Class, just create it on-the-fly and scaffold it.
74  
75 =head1 METHODS
76  
77 =head2 MooseX::Scaffold->scaffold( ... )
78  
79 Scaffold a class by either loading it or creating it. You can pass through the following:
80  
81 scaffolder
82 scaffolding_package This should be either a subroutine (sub { ... }) or a package name. If a package name
83 is given, then the package should contain a subroutine called SCAFFOLD
84  
85 class
86 class_package The package name of resulting class
87  
88 load_or_scaffold Attempt to load $class_package first and do nothing successful. Otherwise create
89 $class_package and scaffold it
90  
91 scaffold_without_load Scaffold $class_package without attempting to load it first. Does not have
92 any effect if $class_package has been loaded already
93  
94 no_class_attribute Set this to 1 to disable applying the MooseX::ClassAttribute meta-role
95 on class creation. This has no effect if the class is loaded (If you
96 want class_has with a loaded class, make sure to 'use MooseX::ClassAttribute')
97  
98 =head2 MooseX::Scaffold->load_and_scaffold( ... )
99  
100 An alias for ->scaffold
101  
102 =head2 MooseX::Scaffold->load_or_scaffold( ... )
103  
104 An alias for ->scaffold with C<load_or_scaffold> set to 1
105  
106 =head2 MooseX::Scaffold->load( ... )
107  
108 An alias for ->load_or_scaffold
109  
110 =head2 MooseX::Scaffold->scaffold_without_load( ... )
111  
112 An alias for ->scaffold with C<scaffold_without_load> set to 1
113  
114 =head2 MooseX::Scaffold->build_scaffolding_import( ... )
115  
116 Return an anonymous subroutine suitable for use an an import function
117  
118 Anything passable to ->scaffold is fair game. In addition:
119  
120 scaffolder This will default to the package of caller() if unspecified
121  
122 chain_import An (optional) subroutine that will goto'd after scaffolding is complete
123  
124 =head2 MooseX::Scaffold->setup_scaffolding_import( ... )
125  
126 Install an import subroutine. By default, caller() will be used for the exporting package, but
127 another may be specified.
128  
129 Anything passable to ->build_scaffolding_import is fair game. In addition:
130  
131 exporter
132 exporting_package The package that will house the import subroutine (the scaffolding will trigger
133 when the package is used or imported)
134  
135 =cut
136  
137 10 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 10 ▶use Carp::Clan;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 10 ▶use Moose();
Subroutine BEGIN: called CC=1 SCAR=0.0
140 10 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 10 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 10 ▶use MooseX::ClassAttribute();
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 10 ▶use MooseX::Scaffold::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 =head2 MooseX::Scaffold->load_package( $package )
147  
148 =head2 MooseX::Scaffold->load_class( $class )
149  
150 A convenience method that will attempt to require $package or $class if not already loaded
151  
152 Essentially does ...
153  
154 eval "require $package;" or die $@
155  
156 ... but uses Class::Inspector to check for $package existence first (%INC is not trustworthy)
157  
158 =cut
159  
160 0 ▶sub load_package {
Subroutine load_package: not called CC=3 SCAR=24.8
Pod documented
161 0 my $self = shift;
162 0 my $package = shift;
163 0 ▶ return 1 if Class::Inspector->loaded($package);
Branch: 1 branch
BranchTrueFalseTotal
if "Class::Inspector"->loaded($package) 0 0 0
164 0 ▶ eval "require $package;" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $package;" or die $@ 0 0 0
165 0 return 1; # FIXME
166 }
167  
168 0 ▶sub load_class {
Subroutine load_class: not called CC=1 SCAR=6.9
Pod documented
169 0 return shift->load_package(@_);
170 }
171  
172 6 ▶sub setup_scaffolding_import {
Subroutine setup_scaffolding_import: called CC=5 SCAR=16.2
Pod documented
173 6 my $self = shift;
174 6 my %given = @_;
175  
176 6 my $exporting_package = $given{exporting_package};
177 6 ▶ $exporting_package ||= $given{exporter} ? delete $given{exporter} : scalar caller;
Branch: 1 branch
BranchTrueFalseTotal
$given{'exporter'} ? : 0 5 5
Condition66%2/3
$exporting_package ||= $given{"exporter"} ? delete $given{"exporter"} : scalar &CORE::GLOBAL::caller()
l!l&&r!l&&!r
150
MC/DC0%0/2
$exporting_package ||= $given{"exporter"} ? delete $given{"exporter"} : scalar &CORE::GLOBAL::caller()
$exporting_package $given{"exporter"} ? delete $given{"exporter"} : scalar &CORE::GLOBAL::caller()
Truth table66%2/3
A ||= B
A= $exporting_packageB= $given{"exporter"} ? delete $given{"exporter"} : scalar &CORE::GLOBAL::caller()
ABresult
1X1
011
000
178  
179 6 ▶ my $scaffolder = $given{scaffolder} ||= scalar caller;
Condition100%2/2
$given{"scaffolder"} ||= scalar &CORE::GLOBAL::caller()
l!l
15
MC/DC100%1/1
$given{"scaffolder"} ||= scalar &CORE::GLOBAL::caller()
$given{"scaffolder"}
Truth table100%2/2
A
A= $given{"scaffolder"}
Aresult
11
00
180  
181 6 my ( $import, $unimport ) = $self->build_scaffolding_import( %given );
182  
183 6 eval "package $exporting_package;";
184 6 ▶ croak "Couldn't open exporting package $exporting_package since: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
185  
186 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
187 6 *{ $exporting_package . '::import' } = $import;
188 }
189  
190 6 ▶sub build_scaffolding_import {
Subroutine build_scaffolding_import: called CC=2 SCAR=7.1
Pod documented
191 6 my $self = shift;
192 6 my %given = @_;
193  
194 6 ▶ my $scaffolder = $given{scaffolder} ||= scalar caller;
Condition50%1/2
$given{"scaffolder"} ||= scalar &CORE::GLOBAL::caller()
l!l
60
MC/DC0%0/1
$given{"scaffolder"} ||= scalar &CORE::GLOBAL::caller()
$given{"scaffolder"}
Truth table50%1/2
A
A= $given{"scaffolder"}
Aresult
11
00
195 6 my $chain_import = $given{chain_import};
196  
197 7 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
198 7 my $CALLER = Moose::Exporter::_get_caller(@_);
199 7 my $exporting_package = shift;
200  
201 7 ▶ return if $CALLER eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $CALLER eq "main" 0 7 7
202  
203 # TODO Check to see if $CALLER is a Moose::Object?
204 7 $self->scaffold(
205 class_package => $CALLER,
206 exporting_package => $exporting_package,
207 %given, \@_
208 );
209  
210 3 ▶ goto &$chain_import if $chain_import;
Branch: 1 branch
BranchTrueFalseTotal
if $chain_import 0 3 3
211 6 };
212 }
213  
214 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
215 1 my $self = shift;
216 1 return $self->scaffold(@_, load_or_scaffold => 1);
217 }
218  
219 1 ▶sub load_or_scaffold {
Subroutine load_or_scaffold: called CC=1 SCAR=0.0
Pod documented
220 1 my $self = shift;
221 1 return $self->load(@_);
222 }
223  
224 0 ▶sub load_and_scaffold {
Subroutine load_and_scaffold: not called CC=1 SCAR=6.9
Pod documented
225 0 my $self = shift;
226 0 return $self->scaffold(@_);
227 }
228  
229 1 ▶sub scaffold_without_load {
Subroutine scaffold_without_load: called CC=1 SCAR=0.0
Pod documented
230 1 my $self = shift;
231 1 return $self->scaffold(@_, scaffold_without_load => 1);
232 }
233  
234 11 ▶sub scaffold {
Subroutine scaffold: called CC=14 SCAR=27.0
Pod documented
235 11 my $self = shift;
236 11 my $arguments = [];
237 11 ▶ $arguments = pop @_ if ref $_[-1] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "ARRAY" 7 4 11
238 11 my %given = @_;
239  
240 11 ▶ my $class_package = $given{class_package} || $given{class};
Condition66%2/3
$given{'class_package'} || $given{'class'}
l!l&&r!l&&!r
740
MC/DC0%0/2
$given{'class_package'} || $given{'class'}
$given{'class_package'} $given{'class'}
Truth table66%2/3
A || B
A= $given{'class_package'}B= $given{'class'}
ABresult
1X1
011
000
241 11 ▶ my $scaffolder = $given{scaffolding_package} || $given{scaffolder};
Condition33%1/3
$given{'scaffolding_package'} || $given{'scaffolder'}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$given{'scaffolding_package'} || $given{'scaffolder'}
$given{'scaffolding_package'} $given{'scaffolder'}
Truth table33%1/3
A || B
A= $given{'scaffolding_package'}B= $given{'scaffolder'}
ABresult
1X1
011
000
242 11 my $load_or_scaffold = $given{load_or_scaffold};
243 11 my $scaffold_without_load = $given{scaffold_without_load};
244 11 my $no_class_attribute = $given{no_class_attribute};
245  
246 11 ▶ if (Class::Inspector->loaded($class_package)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Class::Inspector'->loaded($class_package)) { } 4 7 11
247 4 ▶ return if ! $scaffold_without_load && $load_or_scaffold;
Branch: 1 branch
BranchTrueFalseTotal
if not $scaffold_without_load and $load_or_scaffold 0 4 4
Condition33%1/3
not $scaffold_without_load and $load_or_scaffold
!ll&&!rl&&r
040
MC/DC0%0/2
not $scaffold_without_load and $load_or_scaffold
not $scaffold_without_load $load_or_scaffold
Truth table33%1/3
not(A) and B
A= not $scaffold_without_loadB= $load_or_scaffold
ABresult
0X0
100
111
248 }
249 else {
250 7 ▶ if (! $scaffold_without_load && Class::Inspector->installed($class_package)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $scaffold_without_load and 'Class::Inspector'->installed($class_package)) { } 3 4 7
Condition100%3/3
not $scaffold_without_load and "Class::Inspector"->installed($class_package)
!ll&&!rl&&r
133
MC/DC100%2/2
not $scaffold_without_load and "Class::Inspector"->installed($class_package)
not $scaffold_without_load "Class::Inspector"->installed($class_package)
Truth table100%3/3
not(A) and B
A= not $scaffold_without_loadB= "Class::Inspector"->installed($class_package)
ABresult
0X0
100
111
251 3 eval "require $class_package;";
252 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
253 3 ▶ return if $load_or_scaffold;
Branch: 1 branch
BranchTrueFalseTotal
if $load_or_scaffold 1 2 3
254 }
255 else {
256 4 my $meta = Moose::Meta::Class->create($class_package);
257 4 ▶ unless ($no_class_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_class_attribute) 4 0 4
258 4 MooseX::ClassAttribute->init_meta( for_class => $class_package );
259 }
260 }
261 }
262  
263 10 my $scaffolding_package;
264 10 ▶ if (ref $scaffolder eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scaffolder eq 'CODE') { } 2 8 10
265 }
266 else {
267 8 $scaffolding_package = $scaffolder;
268 8 $self->_load_scaffolding_package( $scaffolding_package );
269 8 $scaffolder = $scaffolding_package->can('SCAFFOLD');
270 8 ▶ croak "Unable to find method SCAFFOLD in package $scaffolding_package" unless $scaffolder;
Branch: 1 branch
BranchTrueFalseTotal
unless $scaffolder 0 8 8
271 }
272  
273 10 $self->_scaffold( $class_package, $scaffolder, @$arguments, scaffolding_package => $scaffolding_package );
274  
275 }
276  
277 8 ▶sub _load_scaffolding_package {
Subroutine _load_scaffolding_package: called CC=3 SCAR=11.0
278 8 my $self = shift;
279 8 my $scaffolding_package = shift;
280 8 ▶ return if Class::Inspector->loaded($scaffolding_package);
Branch: 1 branch
BranchTrueFalseTotal
if "Class::Inspector"->loaded($scaffolding_package) 6 2 8
281 2 ▶ eval "require $scaffolding_package;" or croak "Unable to load scaffolding class $scaffolding_package since: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $scaffolding_package;" or croak("Unable to load scaffolding class $scaffolding_package since: $@") 2 0 2
282 }
283  
284 10 ▶sub _scaffold {
Subroutine _scaffold: called CC=1 SCAR=0.0
285 10 my $self = shift;
286 10 my $class_package = shift;
287 10 my $scaffolder = shift;
288  
289 10 my $class = MooseX::Scaffold::Class->new($class_package);
290 10 $scaffolder->($class, @_, class_package => $class_package);
291 }
292  
293 0 ▶sub parent_package {
Subroutine parent_package: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my $self = shift;
295 0 my $package = shift;
296 0 return $self->repackage($package, undef, shift);
297 }
298  
299 0 ▶sub child_package {
Subroutine child_package: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my $self = shift;
301 0 my $package = shift;
302 0 return $self->repackage($package, shift);
303 }
304  
305 0 ▶sub repackage {
Subroutine repackage: not called CC=7 SCAR=40.3
Pod undocumented
306 0 my $self = shift;
307 0 my $package = shift;
308 0 my $replacement = shift;
309 0 my $count = shift;
310  
311 0 ▶ $count = 0 unless defined $count && length $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and length $count 0 0 0
Condition0%0/3
defined $count and length $count
!ll&&!rl&&r
000
MC/DC0%0/2
defined $count and length $count
defined $count length $count
Truth table0%0/3
A and B
A= defined $countB= length $count
ABresult
0X0
100
111
312  
313 0 ▶ return $package unless $count >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $count >= 1 0 0 0
314
315 0 my @package = split m/::/, $package;
316 0 ▶ pop @package while $count--;
Branch: 1 branch
BranchTrueFalseTotal
$count-- and do { pop @package } 0 0 0
317 0 ▶ push @package, $replacement if defined $replacement && length $replacement;
Branch: 1 branch
BranchTrueFalseTotal
if defined $replacement and length $replacement 0 0 0
Condition0%0/3
defined $replacement and length $replacement
!ll&&!rl&&r
000
MC/DC0%0/2
defined $replacement and length $replacement
defined $replacement length $replacement
Truth table0%0/3
A and B
A= defined $replacementB= length $replacement
ABresult
0X0
100
111
318 0 return join '::', @package;
319 }
320  
321 =head1 AUTHOR
322  
323 Robert Krimen, C<< <rkrimen at cpan.org> >>
324  
325 =head1 SOURCE
326  
327 You can contribute or fork this project via GitHub:
328  
329 L<http://github.com/robertkrimen/moosex-scaffold/tree/master>
330  
331 git clone git://github.com/robertkrimen/moosex-scaffold.git MooseX-Scaffold
332  
333 =head1 BUGS
334  
335 Please report any bugs or feature requests to C<bug-moosex-classscaffold at rt.cpan.org>, or through
336 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Scaffold>. I will be notified, and then you'll
337 automatically be notified of progress on your bug as I make changes.
338  
339  
340  
341  
342 =head1 SUPPORT
343  
344 You can find documentation for this module with the perldoc command.
345  
346 perldoc MooseX::Scaffold
347  
348  
349 You can also look for information at:
350  
351 =over 4
352  
353 =item * RT: CPAN's request tracker
354  
355 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Scaffold>
356  
357 =item * AnnoCPAN: Annotated CPAN documentation
358  
359 L<http://annocpan.org/dist/MooseX-Scaffold>
360  
361 =item * CPAN Ratings
362  
363 L<http://cpanratings.perl.org/d/MooseX-Scaffold>
364  
365 =item * Search CPAN
366  
367 L<http://search.cpan.org/dist/MooseX-Scaffold>
368  
369 =back
370  
371  
372 =head1 ACKNOWLEDGEMENTS
373  
374  
375 =head1 COPYRIGHT & LICENSE
376  
377 Copyright 2008 Robert Krimen, all rights reserved.
378  
379 This program is free software; you can redistribute it and/or modify it
380 under the same terms as Perl itself.
381  
382  
383 =cut
384  
385 10 1; # End of MooseX::Scaffold
↑ Summary Scaffold/Class.pm »