File Coverage

blib/lib/AnyData2/Role/GuessImplementation.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 4 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 37 39 94.8


line stmt bran cond sub pod time code
1             package AnyData2::Role::GuessImplementation;
2              
3 3     3   40 use 5.008001;
  3         6  
4 3     3   8 use strict;
  3         3  
  3         48  
5 3     3   6 use warnings FATAL => 'all';
  3         3  
  3         71  
6              
7 3     3   8 use base "Exporter";
  3         2  
  3         209  
8             our @EXPORT = "_guess_suitable_class";
9              
10 3     3   9 use Carp qw/croak/;
  3         3  
  3         108  
11 3     3   1324 use List::MoreUtils qw(firstval);
  3         22029  
  3         13  
12 3     3   1206 use Module::Runtime qw(require_module);
  3         4  
  3         14  
13              
14             =head1 NAME
15              
16             AnyData2::Role::GuessImplementation - provides role for guessing suitable implementation
17              
18             =cut
19              
20             our $VERSION = '0.002';
21              
22             =head1 METHODS
23              
24             =head2 _guess_suitable_class(@suitables)
25              
26             Finds first suitable class from C<@suitables>. Results are cached.
27              
28             =cut
29              
30             my %suitable_classes;
31              
32             sub _guess_suitable_class
33             {
34 1     1   2 my ( $class, @classlist ) = @_;
35             $suitable_classes{$class} or $suitable_classes{$class} = firstval
36             {
37 1     1   2 eval { require_module($_); }
  1         2  
38             }
39 1 50       13 @classlist;
40 1 50       22 $suitable_classes{$class} or croak( "No suitable class out of (" . join( ", ", @classlist ) . ")for $class available" );
41 1         2 $suitable_classes{$class};
42             }
43              
44             =head1 LICENSE AND COPYRIGHT
45              
46             Copyright 2015,2016 Jens Rehsack.
47              
48             This program is free software; you can redistribute it and/or modify it
49             under the terms of either: the GNU General Public License as published
50             by the Free Software Foundation; or the Artistic License.
51              
52             See http://dev.perl.org/licenses/ for more information.
53              
54             If your Modified Version has been derived from a Modified Version made
55             by someone other than you, you are nevertheless required to ensure that
56             your Modified Version complies with the requirements of this license.
57              
58             This license does not grant you the right to use any trademark, service
59             mark, tradename, or logo of the Copyright Holder.
60              
61             This license includes the non-exclusive, worldwide, free-of-charge
62             patent license to make, have made, use, offer to sell, sell, import and
63             otherwise transfer the Package with respect to any patent claims
64             licensable by the Copyright Holder that are necessarily infringed by the
65             Package. If you institute patent litigation (including a cross-claim or
66             counterclaim) against any party alleging that the Package constitutes
67             direct or contributory patent infringement, then this License
68             to you shall terminate on the date that such litigation is filed.
69              
70             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
71             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
72             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
73             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
74             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
75             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
76             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
77             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78              
79             =cut
80              
81             1;