package Search::Tools::ArgNormalizer;
use Moo::Role;
use Carp;
use Scalar::Util qw( blessed );
use Data::Dump qw( dump );

our $VERSION = '1.007';

    my $self  = shift;
    my %args  = @_;
    my $q     = delete $args{query};
    my $debug = delete $args{debug};
    if ( !defined $q ) {
        confess "query required";
    if ( !ref($q) ) {
        require Search::Tools::QueryParser;
        $args{query} = Search::Tools::QueryParser->new(
            debug => $debug,
            map { $_ => delete $args{$_} }
                grep { $self->queryparser_can($_) } keys %args
    elsif ( ref($q) eq 'ARRAY' ) {
        carp "query ARRAY ref deprecated as of version 0.24";
        require Search::Tools::QueryParser;
        $args{query} = Search::Tools::QueryParser->new(
            debug => $debug,
            map { $_ => delete $args{$_} }
                grep { $self->queryparser_can($_) } keys %args
        )->parse( join( ' ', @$q ) );
    elsif ( blessed($q) and $q->isa('Search::Tools::Query') ) {
        $args{query} = $q;
    else {
            "query param required to be a scalar string or Search::Tools::Query object";
    $args{debug} = $debug;    # restore so it can be passed on
                              #dump \%args;
    return \%args;

sub queryparser_can {
    my $self = shift;
    my $attr = shift or confess "attr required";
    my $can  = Search::Tools::QueryParser->can($attr);

   #    sprintf( "QueryParser->can(%s)==%s\n", $attr, ( $can || '[undef]' ) );

    return $can;



=head1 NAME

Search::Tools::ArgNormalizer - Moo role for BUILDARGS


 package MyTools;
 use Moo;
 with 'Search::Tools::ArgNormalizer';


Moo::Role-based class for consistent BUILDARGS.

=head1 METHODS

=head2 queryparser_can( I<attribute> )

Simple wrapper around Search::Tools::QueryParser->can( I<attribute> )


Moo built-in method called by new(). This method standardizes new() params
across many Search::Tools::* classes.

=head1 BUGS

Please report any bugs or feature requests to C<bug-search-tools at>, or through
the web interface at L<>.  
I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Search::Tools

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN




Copyright 2006-2009, 2014 by Peter Karman.

This package is free software; you can redistribute it and/or modify it under the 
same terms as Perl itself.

=head1 SEE ALSO

HTML::HiLiter, SWISH::HiLiter, L<Moo>, L<Class::XSAccessor>, L<Text::Aspell>