SolarBeam/Query.pm

filter: Statementstmt 13.9% 17 / 122 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 20.8% 5 / 24 Podpod 100% 2 / 2 totaltotal 13.2% 24 / 181 CC 14 SCAR 27.2
CC
14
Cov
12%
_dispatch_value
56.0
new
20.0
_struct_HASH
12.0
CRAP 15.2
« SolarBeam/Response.pm ↑ Summary SolarBeam.pm »
1 package SolarBeam::Query;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use SolarBeam::Util qw(escape_chars unescape_chars);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use overload q("") => 'stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #has 'query' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
8 0 ▶has query => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9  
10 2 ▶use constant D => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
14 0 my $class = shift;
15 0 my $self;
16  
17 0 ▶ if (@_ == 1 && ref $_[0] && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and ref $_[0] eq "ARRAY") 0 0 0
Condition0%0/6
@_ == 1 and ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and ref $_[0] and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] ref $_[0] eq "ARRAY"
Truth table0%0/4
A and B and C
A= @_ == 1B= ref $_[0]C= ref $_[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
18 0 $self = {query => $_[0]};
19 }
20  
21 0 $self = {query => \@_};
22 0 bless $self, 'SolarBeam::Query';
23 0 return $self;
24 }
25  
26 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
27 0 my $self = shift;
28  
29 0 return $self->_dispatch_struct($self->query);
30 }
31  
32 0 ▶sub _dispatch_struct {
Subroutine _dispatch_struct: not called CC=1 SCAR=6.9
33 0 my ($self, $struct) = @_;
34  
35 0 my $method = '_struct_' . ref $struct;
36  
37 0 D && $self->___log("Dispatching to ->$method " . Dumper $struct );
38  
39 0 my $rv = $self->$method($struct);
40  
41 0 D && $self->___log("Returned: $rv");
42  
43 0 return $rv;
44 }
45  
46 0 ▶sub _struct_HASH {
Subroutine _struct_HASH: not called CC=3 SCAR=24.8
47 0 my ($self, $struct) = @_;
48  
49 0 my @clauses;
50  
51 0 for my $k (keys %$struct) {
52 0 my $v = $struct->{$k};
53  
54 0 D && $self->___log("Key => $k, value => " . Dumper($v));
55  
56 0 ▶ if ($k =~ m{^-(.+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^-(.+)/u) { } 0 0 0
57 0 my $method = "_op_$1";
58  
59 0 D && $self->___log("Dispatch ->$method " . Dumper($v));
60 0 push @clauses, $self->$method($v);
61 }
62 else {
63 0 D && $self->___log("Dispatch ->_dispatch_value $k, " . Dumper($v));
64 0 push @clauses, $self->_dispatch_value($k, $v);
65 }
66 }
67  
68 0 my $rv = join(' AND ', @clauses);
69  
70 0 D && $self->___log("Returning: $rv");
71  
72 0 return $rv;
73 }
74  
75 0 ▶sub _struct_ARRAY {
Subroutine _struct_ARRAY: not called CC=1 SCAR=6.9
76 0 my ($self, $struct) = @_;
77  
78 0 my $rv = '(' . join(" OR ", map { $self->_dispatch_struct($_) } @$struct) . ')';
79  
80 0 D && $self->___log("Returning: $rv");
81  
82 0 return $rv;
83 }
84  
85 0 ▶sub _dispatch_value {
Subroutine _dispatch_value: not called CC=7 SCAR=40.3
86 0 my ($self, $k, $v) = @_;
87  
88 0 my $rv;
89 ### it's an array ref, the first element MAY be an operator!
90 ### it would look something like this:
91 # [ '-and',
92 # { '-require' => 'star' },
93 # { '-require' => 'wars' }
94 # ];
95 0 ▶ if ( ref $v
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v and UNIVERSAL::isa($v, 'ARRAY') and defined $v->[0] and $v->[0] =~ /^ - ( AND|OR ) $/iux) { } 0 0 0
Condition0%0/9
ref $v and UNIVERSAL::isa($v, "ARRAY") and defined $v->[0] and $v->[0] =~ /^ - ( AND|OR ) $/iux
!ll&&!rl&&r
000
ref $v and UNIVERSAL::isa($v, "ARRAY") and defined $v->[0]
!ll&&!rl&&r
000
ref $v and UNIVERSAL::isa($v, "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/4
ref $v and UNIVERSAL::isa($v, "ARRAY") and defined $v->[0] and $v->[0] =~ /^ - ( AND|OR ) $/iux
ref $v UNIVERSAL::isa($v, "ARRAY") defined $v->[0] $v->[0] =~ /^ - ( AND|OR ) $/iux
Truth table0%0/5
A and B and C and D
A= ref $vB= UNIVERSAL::isa($v, "ARRAY")C= defined $v->[0]D= $v->[0] =~ /^ - ( AND|OR ) $/iux
ABCDresult
0XXX0
10XX0
110X0
11100
11111
96 and UNIVERSAL::isa($v, 'ARRAY')
97 and defined $v->[0]
98 and $v->[0] =~ /^ - ( AND|OR ) $/ix)
99 {
100 ### XXX we're assuming that all the next statements MUST
101 ### be hashrefs. is this correct?
102 0 shift @$v;
103 0 my $op = uc $1;
104  
105 0 D && $self->___log("Special operator detected: $op " . Dumper($v));
106  
107 0 my @clauses;
108 0 for my $href (@$v) {
109 0 D && $self->___log("Dispatch ->_dispatch_struct({ $k, " . Dumper($href) . '})');
110  
111 ### the individual directive ($href) pertains to the key,
112 ### so we should send that along.
113 0 my $part = $self->_dispatch_struct({$k => $href});
114  
115 0 D && $self->___log("Returned $part");
116  
117 0 push @clauses, '(' . $part . ')';
118 }
119  
120 0 $rv = '(' . join(" $op ", @clauses) . ')';
121  
122 ### nothing special about this combo, so do a usual dispatch
123 }
124 else {
125 0 ▶ my $method = '_value_' . (ref $v || 'SCALAR');
Condition0%0/2
ref $v || 'SCALAR'
l!l
00
MC/DC0%0/1
ref $v || 'SCALAR'
ref $v
Truth table0%0/2
A
A= ref $v
Aresult
11
00
126  
127 0 D && $self->___log("Dispatch ->$method $k, " . Dumper($v));
128  
129 0 $rv = $self->$method($k, $v);
130 }
131  
132 0 D && $self->___log("Returning: $rv");
133  
134 0 return $rv;
135 }
136  
137 0 ▶sub _value_SCALAR {
Subroutine _value_SCALAR: not called CC=2 SCAR=17.9
138 0 my ($self, $k, $v) = @_;
139 0 ▶ $v = ref $v ? $$v : sprintf '"%s"', escape_chars $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
140  
141 0 my $r = qq($k:$v);
142 0 $r =~ s{^:}{};
143  
144 0 D && $self->___log("Returning: $r");
145  
146 0 return $r;
147 }
148  
149 0 ▶sub _value_HASH {
Subroutine _value_HASH: not called CC=2 SCAR=17.9
150 0 my ($self, $k, $v) = @_;
151  
152 0 my @clauses;
153  
154 0 for my $op (keys %$v) {
155 0 my $struct = $v->{$op};
156 0 $op =~ s{^-(.+)}{_op_$1};
157  
158 0 D && $self->___log("Dispatch ->$op $k, " . Dumper($v));
159  
160 0 push @clauses, $self->$op($k, $struct);
161 }
162  
163 0 my $rv = join(' AND ', @clauses);
164  
165 0 D && $self->___log("Returning: $rv");
166  
167 0 return $rv;
168 }
169  
170 0 ▶sub _value_ARRAY {
Subroutine _value_ARRAY: not called CC=1 SCAR=6.9
171 0 my ($self, $k, $v) = @_;
172  
173 0 my $rv = '(' . join(' OR ', map { $self->_value_SCALAR($k, $_) } @$v) . ')';
174  
175 0 D && $self->___log("Returning: $rv");
176  
177 0 return $rv;
178 }
179  
180 0 ▶sub _op_default {
Subroutine _op_default: not called CC=1 SCAR=6.9
181 0 my ($self, $v) = @_;
182 0 return $self->_dispatch_value('', $v);
183 }
184  
185 0 ▶sub _op_range {
Subroutine _op_range: not called CC=1 SCAR=6.9
186 0 my ($self, $k) = (shift, shift);
187 0 my @v = @{shift()};
188 0 return "$k:[$v[ 0 ] TO $v[ 1 ]]";
189 }
190  
191 2 *_op_range_inc = \&_op_range;
192  
193 0 ▶sub _op_range_exc {
Subroutine _op_range_exc: not called CC=1 SCAR=6.9
194 0 my ($self, $k) = (shift, shift);
195 0 my @v = @{shift()};
196 0 return "$k:{$v[ 0 ] TO $v[ 1 ]}";
197 }
198  
199 0 ▶sub _op_boost {
Subroutine _op_boost: not called CC=1 SCAR=6.9
200 0 my ($self, $k) = (shift, shift);
201 0 my ($v, $boost) = @{shift()};
202 0 return sprintf '%s:%s^%s', $k, escape_chars($v), $boost;
203 }
204  
205 0 ▶sub _op_fuzzy {
Subroutine _op_fuzzy: not called CC=1 SCAR=6.9
206 0 my ($self, $k) = (shift, shift);
207 0 my ($v, $distance) = @{shift()};
208 0 return sprintf '%s:%s~%s', $k, escape_chars($v), $distance;
209 }
210  
211 0 ▶sub _op_proximity {
Subroutine _op_proximity: not called CC=1 SCAR=6.9
212 0 my ($self, $k) = (shift, shift);
213 0 my ($v, $distance) = @{shift()};
214 0 return sprintf '%s:"%s"~%s', $k, escape_chars($v), $distance;
215 }
216  
217 0 ▶sub _op_require {
Subroutine _op_require: not called CC=1 SCAR=6.9
218 0 my ($self, $k, $v) = @_;
219 0 return sprintf '+%s:"%s"', $k, escape_chars($v);
220 }
221  
222 0 ▶sub _op_prohibit {
Subroutine _op_prohibit: not called CC=1 SCAR=6.9
223 0 my ($self, $k, $v) = @_;
224 0 return sprintf '-%s:"%s"', $k, escape_chars($v);
225 }
226  
227 0 ▶sub ___log {
Subroutine ___log: not called CC=1 SCAR=6.9
228 0 my $self = shift;
229 0 my $msg = shift;
230  
231 ### subroutine the log call came from, and line number the log
232 ### call came from. that's 2 different caller frames :(
233 0 my $who = join ':', [caller(1)]->[3], [caller(0)]->[2];
234  
235 ### make sure we prefix every line with a #
236 0 $msg =~ s/\n/\n#/g;
237  
238 0 print "# $who: $msg\n";
239 }
240  
241 __END__
« SolarBeam/Response.pm ↑ Summary SolarBeam.pm »