File Coverage

blib/lib/Class/Adapter/Builder.pm
Criterion Covered Total %
statement 118 134 88.0
branch 38 52 73.0
condition 4 9 44.4
subroutine 27 28 96.4
pod 1 9 11.1
total 188 232 81.0


line stmt bran cond sub pod time code
1             package Class::Adapter::Builder;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Class::Adapter::Builder - Generate Class::Adapter classes
8              
9             =head1 SYNOPSIS
10              
11             package My::Adapter;
12            
13             use strict;
14             use Class::Adapter::Builder
15             ISA => 'Specific::API',
16             METHODS => [ qw{foo bar baz} ],
17             method => 'different_method';
18            
19             1;
20              
21             =head1 DESCRIPTION
22              
23             C is another mechanism for letting you create
24             I classes of your own.
25              
26             It is intended to act as a toolkit for generating the guts of many varied
27             and different types of I classes.
28              
29             For a simple base class you can inherit from and change a specific method,
30             see L.
31              
32             =head2 The Pragma Interface
33              
34             The most common method for defining I classes, as shown in the
35             synopsis, is the pragma interface.
36              
37             This consists of a set of key/value pairs provided when you load the module.
38              
39             # The format for building Adapter classes
40             use Class::Adapter::Builder PARAM => VALUE, ...
41              
42             =over 4
43              
44             =item ISA
45              
46             The C param is provided as either a single value, or a reference
47             to an C containing is list of classes.
48              
49             Normally this is just a straight list of classes. However, if the value
50             for C is set to C<'_OBJECT_'> the object will identify itself as
51             whatever is contained in it when the C<-Eisa> and C<-Ecan> method
52             are called on it.
53              
54             =item NEW
55              
56             Normally, you need to create your C objects separately:
57              
58             # Create the object
59             my $query = CGI->new( 'param1', 'param2' );
60            
61             # Create the Decorator
62             my $object = My::Adapter->new( $query );
63              
64             If you provide a class name as the C param, the Decorator will
65             do this for you, passing on any constructor arguments.
66              
67             # Assume we provided the following
68             # NEW => 'CGI',
69            
70             # We can now do the above in one step
71             my $object = My::Adapter->new( 'param1', 'param2' );
72              
73             =item AUTOLOAD
74              
75             By default, a C does not pass on any methods, with the
76             methods to be passed on specified explicitly with the C<'METHODS'>
77             param.
78              
79             By setting C to true, the C will be given the
80             standard C function to to pass through all unspecified
81             methods to the parent object.
82              
83             By default the AUTOLOAD will pass through any and all calls, including
84             calls to private methods.
85              
86             If the AUTOLOAD is specifically set to 'PUBLIC', the AUTOLOAD setting
87             will ONLY apply to public methods, and any private methods will not
88             be passed through.
89              
90             =item METHODS
91              
92             The C param is provided as a reference to an array of all
93             the methods that are to be passed through to the parent object as is.
94              
95             =back
96              
97             Any params other than the ones specified above are taken as translated
98             methods.
99              
100             # If you provide the following
101             # foo => bar
102            
103             # It the following are equivalent
104             $decorator->foo;
105             $decorator->_OBJECT_->bar;
106              
107             This capability is provided primarily because in Perl one of the main
108             situations in which you hit the limits of Perl's inheritance model is
109             when your class needs to inherit from multiple different classes that
110             containing clashing methods.
111              
112             For example:
113              
114             # If your class is like this
115             package Foo;
116            
117             use base 'This', 'That';
118            
119             1;
120              
121             If both Cmethod> exists and Cmethod> exists,
122             and both mean different things, then Cmethod> becomes
123             ambiguous.
124              
125             A C could be used to wrap your C object, with
126             the C becoming the C sub-class, and passing
127             C<$decorator-Emethod> through to C<$object-Ethat_method>.
128              
129             =head1 METHODS
130              
131             Yes, C has public methods and later on you will
132             be able to access them directly, but for now they are remaining
133             undocumented, so that I can shuffle things around for another few
134             versions.
135              
136             Just stick to the pragma interface for now.
137              
138             =cut
139              
140 8     8   142156 use 5.005;
  8         37  
  8         340  
141 8     8   387 use strict;
  7         13  
  7         320  
142 8     8   41 use Carp ();
  8         19  
  8         164  
143 8     8   3014 use Class::Adapter ();
  8         50  
  8         166  
144              
145 8     8   411 use vars qw{$VERSION};
  9         22  
  9         374  
146             BEGIN {
147 8     8   19904 $VERSION = '1.07';
148             }
149              
150              
151              
152              
153              
154             #####################################################################
155             # Constructor
156              
157             sub new {
158 11   33 9 0 561 my $class = ref $_[0] || $_[0];
159 10         81 return bless {
160             target => $_[1],
161             isa => [ 'Class::Adapter' ],
162             modules => {},
163             methods => {},
164             }, $class;
165             }
166              
167             sub import {
168 9     9   1724 my $class = shift;
169              
170             # Must have at least one param
171 9 100       53 return 1 unless @_;
172              
173             # Create the Builder object
174 8         19 my $target = caller;
175 8         27 my $self = $class->new( $target );
176 8 50       58 unless ( $self ) {
177 0         0 Carp::croak("Failed to create Class::Adapter::Builder object");
178             }
179              
180             # Process the option pairs
181 8         29 while ( @_ ) {
182 21         31 my $key = shift;
183 21         28 my $value = shift;
184 21 100       77 if ( $key eq 'NEW' ) {
    100          
    50          
    0          
185 5         10 $self->set_NEW( $value );
186             } elsif ( $key eq 'ISA' ) {
187 8         26 $self->set_ISA( $value );
188             } elsif ( $key eq 'AUTOLOAD' ) {
189 8         27 $self->set_AUTOLOAD( $value );
190             } elsif ( $key eq 'METHODS' ) {
191 0         0 $self->set_METHODS( $value );
192             } else {
193 0         0 $self->set_method( $key, $value );
194             }
195             }
196              
197             # Generate the code
198 8 50       23 my $code = $self->make_class or Carp::croak(
199             "Failed to generate Class::Adapter::Builder class"
200             );
201              
202             # Compile the combined code via a temp file so that debugging works
203             #require File::Temp;
204             #my ($fh, $filename) = File::Temp::tempfile();
205             #$fh->print("$code");
206             #close $fh;
207             #require $filename;
208             #print "Loaded '$filename'\n";
209              
210 7 100 66 7 1 592 eval "$code";
  7 50 33 6 0 353  
  6 50   6   205  
  6 100   6   36  
  6     7   6  
  6     10   141  
  6     1   30  
  6     12   7  
  6         206  
  6         1189  
  8         1381  
  7         4752  
  7         34  
  7         43  
  1         179  
  6         17  
  10         3842  
  4         18  
  5         528  
  12         2393  
211 8 50       31 $@ and Carp::croak(
212             "Error while compiling Class::Adapter::Builder class '$target' ($@)"
213             );
214              
215 8         4320 $target;
216             }
217              
218              
219              
220              
221              
222             #####################################################################
223             # Main Methods
224              
225             sub set_NEW {
226 15     8 0 156 my $self = shift;
227 7         74 $self->{new} = shift;
228              
229             # We always need Scalar::Util to pass through new
230 6         480 $self->{modules}->{'Scalar::Util'} = 1;
231              
232             # Add a use for the module unless it is already loaded.
233             # We test with the can call instead of just blindly require'ing in
234             # case we want to NEW to something that doesn't have it's own
235             # .pm file.
236 6 100       60 unless ( $self->{new}->can('new') ) {
237 2         33 $self->{modules}->{ $self->{new} } = 1;
238             }
239              
240 6         24 return 1;
241             }
242              
243             sub set_ISA {
244 10     10 0 3748 my $self = shift;
245 10 100       69 my $array = ref $_[0] eq 'ARRAY' ? shift : [ @_ ];
246 10         52 $self->{isa} = $array;
247 10         47 return 1;
248             }
249              
250             sub set_AUTOLOAD {
251 10     10 0 1620 my $self = shift;
252 10 100       54 if ( $_[0] ) {
253 9         64 $self->{autoload} = 1;
254 9         32 $self->{modules}->{Carp} = 1;
255 8 100       33 if ( $_[0] eq 'PUBLIC' ) {
256 1         3 $self->{autoload_public} = 1;
257             }
258             } else {
259 1         3 delete $self->{autoload};
260             }
261 9         31 return 1;
262             }
263              
264             sub set_METHODS {
265 0     1 0 0 my $self = shift;
266 0 50       0 my $array = ref $_[0] eq 'ARRAY' ? shift : [ @_ ];
267 0         0 foreach my $name ( @$array ) {
268 0 50       0 $self->set_method( $name, $name ) or return undef;
269             }
270 0         0 return 1;
271             }
272              
273             sub set_method {
274 0     1 0 0 my $self = shift;
275 0 50       0 if ( @_ == 1 ) {
    50          
276 0         0 $self->{methods}->{$_[0]} = $_[0];
277             } elsif ( @_ == 2 ) {
278 0         0 $self->{methods}->{$_[0]} = $_[1];
279             } else {
280 0         0 return undef;
281             }
282 0         0 return 1;
283             }
284              
285              
286              
287              
288              
289             #####################################################################
290             # Code Generation Functions
291              
292             sub make_class {
293 9     9 0 555 my $self = shift;
294              
295             # Generate derived lists
296 9         26 my %seen = ();
297 14         59 $self->{load} = [
298 14         135 grep { $_ !~ /^Class::Adapter(?:::Builder)?$/ }
299 9         51 sort grep { ! $seen{$_}++ }
300 9         16 keys %{$self->{modules}}
301             ];
302 3         10 $self->{fake} = [
303 9         19 grep { ! $seen{$_} } grep { $_ ne '_OBJECT_' } @{$self->{isa}}
  9         31  
  9         22  
304             ];
305              
306             # Build up the parts of the class
307 9         61 my @parts = (
308             "package $self->{target};\n\n"
309             . "# Generated by Class::Abstract::Builder\n"
310             );
311              
312 9 50       32 if ( keys %{$self->{modules}} ) {
  9         34  
313 9         28 push @parts, $self->_make_modules;
314             }
315              
316 9 100       39 if ( $self->{new} ) {
317 5         13 push @parts, $self->_make_new( $self->{new} );
318             }
319              
320 9         17 my $methods = $self->{methods};
321 9         48 foreach my $name ( keys %$methods ) {
322 0         0 push @parts, $self->_make_method( $name, $methods->{$name} );
323             }
324              
325 9 50       22 if ( @{$self->{isa}} == 1 ) {
  9         36  
326 9 100       31 if ( $self->{isa}->[0] eq '_OBJECT_' ) {
327 6         17 push @parts, $self->_make_OBJECT;
328             } else {
329 3         3 push @parts, $self->_make_ISA( @{$self->{isa}} );
  3         8  
330             }
331             }
332              
333 9 100       32 if ( $self->{autoload} ) {
334 8         45 push @parts, $self->_make_AUTOLOAD( $self->{target}, $self->{autoload_public} );
335             }
336              
337 9         104 return join( "\n", @parts, "1;\n" );
338             }
339              
340             sub _make_modules {
341 9     9   13 my $self = shift;
342 9         20 my $pkg = $self->{target};
343 14         45 my $load = join '',
344 9         20 map { "\nuse $_ ();" }
345 9         13 @{$self->{load}};
346              
347             # Foo->isa('Foo') returns false if the namespace does not exist
348             # Use the package command in a scope to create namespaces where needed.
349 2         9 my $namespaces = join '',
350 9         21 map { "\n\t$_->isa('$_') or do { package $_ };" }
351 9         17 @{$self->{fake}};
352              
353 9         50 return <<"END_MODULES";
354             use strict;${load}
355             use Class::Adapter ();
356              
357             BEGIN {
358             \@${pkg}::ISA = 'Class::Adapter';${namespaces}
359             }
360             END_MODULES
361             }
362              
363 5     5   12 sub _make_new { <<"END_NEW" }
364             sub new {
365             my \$class = ref \$_[0] ? ref shift : shift;
366             my \$object = $_[1]\->new(\@_);
367             Scalar::Util::blessed(\$object) or return undef;
368             \$class->SUPER::new(\$object);
369             }
370             END_NEW
371              
372              
373              
374 0     0   0 sub _make_method { <<"END_METHOD" }
375             sub $_[1] { shift->_OBJECT_->$_[2](\@_) }
376             END_METHOD
377              
378              
379              
380 6     6   14 sub _make_OBJECT { <<"END_OBJECT" }
381             sub isa {
382             ref(\$_[0])
383             ? shift->_OBJECT_->isa(\@_)
384             : shift->isa(\@_);
385             }
386              
387             sub can {
388             ref(\$_[0])
389             ? shift->_OBJECT_->can(\@_)
390             : shift->can(\@_);
391             }
392             END_OBJECT
393              
394             sub _make_ISA {
395 3     3   5 my $self = shift;
396 3         20 my @lines = (
397             "sub isa {\n",
398 2         13 ( map { "\treturn 1 if \$_[1]->isa('$_');\n" } @_ ),
399             "\treturn undef;\n",
400             "}\n",
401             "\n",
402             "sub can {\n",
403             # If we are pretending to be a fake ISA, and we get a can call,
404             # we should try to require the module (even if it doesn't exist)
405             # so that we can provide an accurate answer in the case where
406             # we are faking a module that exists.
407 3         15 ( map { "\trequire $_ unless $_->isa('UNIVERSAL');\n" } @{$self->{fake}} ),
  3         14  
408             "\treturn 1 if \$_[0]->SUPER::can(\$_[1]);\n",
409 3         5 ( map { "\treturn 1 if $_->can(\$_[1]);\n" } @_ ),
410             "\treturn undef;\n",
411             "}\n",
412             );
413 3         17 return join '', @lines;
414             }
415              
416              
417              
418 8 100   8   32 sub _make_AUTOLOAD { my $pub = $_[2] ? 'and substr($method, 0, 1) ne "_"' : ''; return <<"END_AUTOLOAD" }
  8         43  
419             sub AUTOLOAD {
420             my \$self = shift;
421             my (\$method) = \$$_[1]::AUTOLOAD =~ m/^.*::(.*)\\z/s;
422             unless ( ref(\$self) $pub) {
423             Carp::croak(
424             qq{Can't locate object method "\$method" via package "\$self" }
425             . qq{(perhaps you forgot to load "\$self")}
426             );
427             }
428             \$self->_OBJECT_->\$method(\@_);
429             }
430              
431             sub DESTROY {
432             if ( defined \$_[0]->{OBJECT} and \$_[0]->{OBJECT}->can('DESTROY') ) {
433             undef \$_[0]->{OBJECT};
434             }
435             }
436             END_AUTOLOAD
437              
438             1;
439              
440             =pod
441              
442             =head1 SUPPORT
443              
444             Bugs should be reported via the CPAN bug tracker at
445              
446             L
447              
448             For other issues, contact the author.
449              
450             =head1 AUTHOR
451              
452             Adam Kennedy Eadamk@cpan.orgE
453              
454             =head1 SEE ALSO
455              
456             L, L
457              
458             =head1 COPYRIGHT
459              
460             Copyright 2005 - 2011 Adam Kennedy.
461              
462             This program is free software; you can redistribute
463             it and/or modify it under the same terms as Perl itself.
464              
465             The full text of the license can be found in the
466             LICENSE file included with this module.
467              
468             =cut