blib/lib/Search/Tools/ArgNormalizer.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 65 SCAR 34.0
CC
5
Cov
0%
BUILDARGS
30.0
queryparser_can
2.0
BEGIN
2.0
CRAP 30.0
1 package Search::Tools::ArgNormalizer;
2 use Moo::Role;
3 use Carp;
4 use Scalar::Util qw( blessed );
5 use Data::Dump qw( dump );
6  
7 our $VERSION = '1.004';
8  
9 sub BUILDARGS {
10 my $self = shift;
11 my %args = @_;
12 my $q = delete $args{query};
13 my $debug = delete $args{debug};
14 if ( !defined $q ) {
15 confess "query required";
16 }
17 if ( !ref($q) ) {
18 require Search::Tools::QueryParser;
19 $args{query} = Search::Tools::QueryParser->new(
20 debug => $debug,
21 map { $_ => delete $args{$_} }
22 grep { $self->queryparser_can($_) } keys %args
23 )->parse($q);
24 }
25 elsif ( ref($q) eq 'ARRAY' ) {
26 carp "query ARRAY ref deprecated as of version 0.24";
27 require Search::Tools::QueryParser;
28 $args{query} = Search::Tools::QueryParser->new(
29 debug => $debug,
30 map { $_ => delete $args{$_} }
31 grep { $self->queryparser_can($_) } keys %args
32 )->parse( join( ' ', @$q ) );
33 }
34 elsif ( blessed($q) and $q->isa('Search::Tools::Query') ) {
35 $args{query} = $q;
36 }
37 else {
38 confess
39 "query param required to be a scalar string or Search::Tools::Query object";
40 }
41 $args{debug} = $debug; # restore so it can be passed on
42 #dump \%args;
43 return \%args;
44 }
45  
46 sub queryparser_can {
47 my $self = shift;
48 my $attr = shift or confess "attr required";
49 my $can = Search::Tools::QueryParser->can($attr);
50  
51 #warn
52 # sprintf( "QueryParser->can(%s)==%s\n", $attr, ( $can || '[undef]' ) );
53  
54 return $can;
55 }
56  
57 1;
58  
59 __END__