File Coverage

blib/lib/App/ElasticSearch/Utilities/QueryString/Plugin.pm
Criterion Covered Total %
statement 30 32 93.7
branch 4 8 50.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 43 50 86.0


line stmt bran cond sub pod time code
1             package App::ElasticSearch::Utilities::QueryString::Plugin;
2             # ABSTRACT: Moo::Role for implementing QueryString Plugins
3              
4 1     1   9824 use v5.16;
  1         4  
5 1     1   6 use warnings;
  1         3  
  1         47  
6              
7             our $VERSION = '8.6'; # VERSION
8              
9 1     1   6 use Hash::Merge::Simple qw(clone_merge);
  1         2  
  1         104  
10 1     1   11 use Moo::Role;
  1         2  
  1         7  
11 1     1   419 use Ref::Util qw(is_arrayref is_hashref);
  1         2  
  1         56  
12 1     1   7 use Types::Standard qw( Str Int );
  1         3  
  1         18  
13              
14              
15             # Attributes
16             has name => (
17             is => 'lazy',
18             isa => Str,
19             );
20             sub _build_name {
21 7     7   134 my $self = shift;
22 7         15 my $class = ref $self;
23 7         126 return (split /::/, $class)[-1];
24             }
25              
26              
27             has priority => (
28             is => 'lazy',
29             isa => Int,
30             );
31 1     1   26 sub _build_priority { 50; }
32              
33              
34             requires qw(handle_token);
35              
36             around 'handle_token' => sub {
37             my $orig = shift;
38             my $self = shift;
39             my $refs = $orig->($self,@_);
40             if( defined $refs ) {
41             if( is_arrayref($refs) ) {
42             foreach my $doc (@{ $refs }) {
43             $doc->{_by} = $self->name;
44             }
45             }
46             elsif( is_hashref($refs) ) {
47             $refs->{_by} = $self->name;
48             }
49             return $refs;
50             }
51             else {
52             return;
53             }
54             };
55              
56              
57             # Handle Build Args
58              
59              
60             sub BUILDARGS {
61 7     7 0 22231 my($class,%in) = @_;
62              
63 7         49 my @search = map { $_ => lc $_ } ($class,(split /::/, $class)[-1]);
  14         52  
64 7 50       30 my $options = exists $in{options} ? delete $in{options} : {};
65              
66 7         16 my @options = ();
67 7         17 foreach my $s (@search) {
68 28 50       66 if( exists $options->{$s} ) {
69 0         0 push @options, $options->{$s};
70 0         0 last;
71             }
72             }
73 7 50       22 push @options, \%in if keys %in;
74              
75 7 50       148 return scalar(@options) ? clone_merge(@options) : {};
76             }
77              
78             1;
79              
80             __END__
81              
82             =pod
83              
84             =head1 NAME
85              
86             App::ElasticSearch::Utilities::QueryString::Plugin - Moo::Role for implementing QueryString Plugins
87              
88             =head1 VERSION
89              
90             version 8.6
91              
92             =head1 ATTRIBUTES
93              
94             =head2 name
95              
96             Name of the plugin, used in debug reporting.
97              
98             =head2 priority
99              
100             Priority is an integer which determmines the order tokens are parsed in
101             low->high order.
102              
103             =head1 INTERFACE
104              
105             =head2 handle_token()
106              
107             The handle_token() routine receives a single token from the command line, often a single word
108             and returns a hash reference specifying
109              
110             The token expansion plugins can return undefined, which is basically a noop on the token.
111             The plugin can return a hash reference, which marks that token as handled and no other plugins
112             receive that token. The hash reference may contain:
113              
114             =head2 query_string
115              
116             This is the rewritten bits that will be reassembled in to the final query string.
117              
118             =head2 condition
119              
120             This is usually a hash reference representing the condition going into the bool query. For instance:
121              
122             { terms => { field => [qw(alice bob charlie)] } }
123              
124             Or
125              
126             { prefix => { user_agent => 'Go ' } }
127              
128             These conditions will wind up in the B<must> or B<must_not> section of the B<bool> query depending on the
129             state of the the invert flag.
130              
131             =head2 invert
132              
133             This is used by the bareword "not" to track whether the token invoked a flip from the B<must> to the B<must_not>
134             state. After each token is processed, if it didn't set this flag, the flag is reset.
135              
136             =head2 dangles
137              
138             This is used for bare words like "not", "or", and "and" to denote that these terms cannot dangle from the
139             beginning or end of the query_string. This allows the final pass of the query_string builder to strip these
140             words to prevent syntax errors.
141              
142             =for Pod::Coverage BUILDARGS
143              
144             =head1 AUTHOR
145              
146             Brad Lhotsky <brad@divisionbyzero.net>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is Copyright (c) 2023 by Brad Lhotsky.
151              
152             This is free software, licensed under:
153              
154             The (three-clause) BSD License
155              
156             =cut