prep release
[p5sagit/Config-Any.git] / lib / Config / Any / Base.pm
1 package Config::Any::Base;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 Config::Any::Base - Base class for loaders
9
10 =head1 DESCRIPTION
11
12 This is a base class for all loaders. It currently handles the specification
13 of dependencies in order to ensure the subclass can load the config file
14 format.
15
16 =head1 METHODS
17
18 =head2 is_supported( )
19
20 Allows us to determine if the file format can be loaded. The can be done via
21 one of two subclass methods:
22
23 =over 4
24
25 =item * C<requires_all_of()> - returns an array of items that must all be present in order to work
26
27 =item * C<requires_any_of()> - returns an array of items in which at least one must be present
28
29 =back
30
31 You can specify a module version by passing an array reference in the return.
32
33     sub requires_all_of { [ 'My::Module', '1.1' ], 'My::OtherModule' }
34
35 Lack of specifying these subs will assume you require no extra modules to function.
36
37 =cut
38
39 sub is_supported {
40     my ( $class ) = shift;
41     if ( $class->can( 'requires_all_of' ) ) {
42         eval join( '', map { _require_line( $_ ) } $class->requires_all_of );
43         return $@ ? 0 : 1;
44     }
45     if ( $class->can( 'requires_any_of' ) ) {
46         for ( $class->requires_any_of ) {
47             eval _require_line( $_ );
48             return 1 unless $@;
49         }
50         return 0;
51     }
52
53     # requires nothing!
54     return 1;
55 }
56
57 sub _require_line {
58     my ( $input ) = shift;
59     my ( $module, $version ) = ( ref $input ? @$input : $input );
60     return "require $module;"
61         . ( $version ? "${module}->VERSION('${version}');" : '' );
62 }
63
64 =head1 AUTHOR
65
66 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
67
68 =head1 COPYRIGHT AND LICENSE
69
70 Copyright 2008-2009 by Brian Cassidy
71
72 This library is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself. 
74
75 =head1 SEE ALSO
76
77 =over 4 
78
79 =item * L<Config::Any>
80
81 =back
82
83 =cut
84
85 1;