Giddy/Role/DocumentMatcher.pm

filter: Statementstmt 14.9% 24 / 161 Branchbran 0.0% 0 / 202 Conditioncond 0.0% 0 / 121 MC/DCmcdc 0.0% 0 / 74 Subroutinesub 35.2% 6 / 17 Podpod n/a totaltotal 5.2% 30 / 575 CC 154 SCAR 89.2
CC
154
Cov
5%
_attribute_matches
11772.0
_document_matches
210.0
_match_by_name
156.0
CRAP 7515.8
↑ Summary Giddy/Role/DocumentUpdater.pm »
1 package Giddy::Role::DocumentMatcher;
2  
3 2 ▶use Any::Moose 'Role';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DateTime::Format::W3CDTF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "0.013_001";
12 2 $VERSION = eval $VERSION;
13  
14 2 requires 'db';
15 2 requires '_documents';
16 2 requires '_load_document';
17  
18 =head1 NAME
19  
20 Giddy::Role::DocumentMatcher - Provides query parsing and document matching for Giddy::Collection
21  
22 =head1 VERSION
23  
24 version 0.013_001
25  
26 =head1 SYNOPSIS
27  
28 # used internally
29  
30 =head1 DESCRIPTION
31  
32 This role provides document matching capabilities to L<Giddy::Collection> and L<Giddy::Collection::InMemory>.
33  
34 Requires the 'db', '_documents' and '_load_document' attributes/methods to be implemented by consuming classes.
35  
36 =head1 METHODS
37  
38 =head2 _match_by_name( $name, \%options )
39  
40 =cut
41  
42 0 ▶sub _match_by_name {
Subroutine _match_by_name: not called CC=12 SCAR=50.5
43 0 my ($self, $name, $opts) = @_;
44  
45 0 ▶ $name ||= '';
Condition0%0/2
$name ||= ""
l!l
00
MC/DC0%0/1
$name ||= ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
46 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
47  
48 # return all documents if we don't really have a query
49 0 ▶ $name eq '' && return $self->_documents;
Branch: 1 branch
BranchTrueFalseTotal
$name eq "" and return $self->_documents 0 0 0
50  
51 0 my $docs = Tie::IxHash->new;
52  
53 # if query is for equality then only one document can be matched
54 # and since we want this to be fast, let's search for it a little
55 # differently
56 0 ▶ if (!ref $name || (ref $name eq 'HASH' && scalar keys %$name == 1 && $name->{'$eq'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $name or ref $name eq 'HASH' and scalar keys %$name == 1 and $name->{'$eq'}) { } 0 0 0
Condition0%0/9
not ref $name or ref $name eq "HASH" and scalar keys %$name == 1 and $name->{"\$eq"}
l!l&&r!l&&!r
000
ref $name eq "HASH" and scalar keys %$name == 1 and $name->{"\$eq"}
!ll&&!rl&&r
000
ref $name eq "HASH" and scalar keys %$name == 1
!ll&&!rl&&r
000
MC/DC0%0/4
not ref $name or ref $name eq "HASH" and scalar keys %$name == 1 and $name->{"\$eq"}
not ref $name ref $name eq "HASH" scalar keys %$name == 1 $name->{"\$eq"}
Truth table0%0/5
not(A) or B and C and D
A= not ref $nameB= ref $name eq "HASH"C= scalar keys %$name == 1D= $name->{"\$eq"}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
57 # if the collection is in-memory, then we already have a
58 # hash-ref of all documents in it and we can just find out
59 # if it has the document we're searching for
60 0 ▶ my $n = ref $name ? $name->{'$eq'} : $name;
Branch: 1 branch
BranchTrueFalseTotal
ref $name ? : 0 0 0
61 0 ▶ if (ref $self eq 'Giddy::Collection::InMemory') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Giddy::Collection::InMemory') { } 0 0 0
62 0 my $doc = $self->_documents->FETCH($n);
63 0 ▶ $docs->STORE($n => $doc)
Branch: 1 branch
BranchTrueFalseTotal
if $doc 0 0 0
64 if $doc;
65 } else {
66 # we're gonna use Git's cat-file command and see
67 # if we have such a document. Since this command
68 # croaks when the document doesn't exist, we're
69 # gonna eval it
70 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
71 0 my $type = $self->db->_repo->run('cat-file', '-t', 'HEAD:'.$self->_path_to($n));
72 0 ▶ if ($type eq 'blob') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'blob') { } 0 0 0
elsif ($type eq 'tree') { } 0 0 0
73 0 $docs->STORE($n => 'file');
74 } elsif ($type eq 'tree') {
75 0 $docs->STORE($n => 'dir');
76 }
77 0 };
78 }
79 } else {
80 0 foreach (grep { $self->_attribute_matches({ _name => $_ }, '_name', $name) } $self->_documents->Keys) {
81 0 $docs->STORE($_ => $self->_documents->FETCH($_));
82 }
83  
84 0 $docs->SortByKey;
85 }
86  
87 0 return $docs;
88 }
89  
90 =head2 _match_by_query( [ \%query, \%options ] )
91  
92 =cut
93  
94 0 ▶sub _match_by_query {
Subroutine _match_by_query: not called CC=6 SCAR=37.4
95 0 my ($self, $query, $opts) = @_;
96  
97 0 ▶ $query ||= {};
Condition0%0/2
$query ||= {}
l!l
00
MC/DC0%0/1
$query ||= {}
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
98 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
99  
100 # return all documents if we don't really have a query
101 0 ▶ scalar keys %$query == 0 && return $self->_documents;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$query == 0 and return $self->_documents 0 0 0
102  
103 0 my $docs = Tie::IxHash->new;
104 0 my $loaded = {};
105 0 my $i = 0;
106 0 foreach ($self->_documents->Keys) {
107 0 my $doc = $self->_load_document($i++);
108 0 ▶ if ($self->_document_matches($doc, $query)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_document_matches($doc, $query)) 0 0 0
109 0 $docs->STORE($_ => $self->_documents->FETCH($_));
110 0 $loaded->{$_} = $doc;
111 }
112 }
113  
114 0 return ($docs, $loaded);
115 }
116  
117 =head2 _document_matches( \%doc, \%query )
118  
119 =cut
120  
121 0 ▶sub _document_matches {
Subroutine _document_matches: not called CC=14 SCAR=53.5
122 0 my ($self, $doc, $query) = @_;
123  
124 0 ▶ croak "You must provide a document hash-ref."
Branch: 1 branch
BranchTrueFalseTotal
unless $doc and ref $doc eq "HASH" 0 0 0
Condition0%0/3
$doc and ref $doc eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$doc and ref $doc eq "HASH"
$doc ref $doc eq "HASH"
Truth table0%0/3
A and B
A= $docB= ref $doc eq "HASH"
ABresult
0X0
100
111
125 unless $doc && ref $doc eq 'HASH';
126  
127 0 ▶ $query ||= {}; # allow empty queries (document will match)
Condition0%0/2
$query ||= {}
l!l
00
MC/DC0%0/1
$query ||= {}
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
128  
129 # go over each key of the query
130 0 foreach my $key (keys %$query) {
131 0 my $value = $query->{$key};
132 0 ▶ if ($key eq '$or' && ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '$or' and ref $value eq 'ARRAY') { } 0 0 0
Condition0%0/3
$key eq "\$or" and ref $value eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "\$or" and ref $value eq "ARRAY"
$key eq "\$or" ref $value eq "ARRAY"
Truth table0%0/3
A and B
A= $key eq "\$or"B= ref $value eq "ARRAY"
ABresult
0X0
100
111
133 0 my $found;
134 0 foreach (@$value) {
135 0 ▶ next unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 0 0
136 0 my $ok = 1;
137 0 ▶ while (my ($k, $v) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 0 0 0
138 0 ▶ unless ($self->_attribute_matches($doc, $k, $v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_attribute_matches($doc, $k, $v)) 0 0 0
139 0 undef $ok;
140 0 last;
141 }
142 }
143 0 ▶ if ($ok) { # document matches this criteria
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
144 0 $found = 1;
145 0 last;
146 }
147 }
148 0 ▶ return unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
149 } else {
150 0 ▶ return unless $self->_attribute_matches($doc, $key, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_attribute_matches($doc, $key, $value) 0 0 0
151 }
152 }
153  
154 # if we've reached here, the document matches, so return true
155 0 return 1;
156 }
157  
158 =head2 _attributes_matches( \%doc, $key, $value )
159  
160 =cut
161  
162 0 ▶sub _attribute_matches {
Subroutine _attribute_matches: not called CC=108 SCAR=93.7
163 0 my ($self, $doc, $key, $value) = @_;
164  
165 0 ▶ if (!ref $value) { # if value is a scalar, we need to check for equality
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $value) { } 0 0 0
elsif (ref $value eq 'Regexp') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
166 # (or, if the attribute is an array in the document,
167 # we need to check the value exists in it)
168 0 ▶ return unless $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} 0 0 0
169 0 ▶ if (ref $doc->{$key} eq 'ARRAY') { # check the array has the requested value
Branch: 2 branches
BranchTrueFalseTotal
if (ref $doc->{$key} eq 'ARRAY') { } 0 0 0
elsif (not ref $doc->{$key}) { } 0 0 0
170 0 ▶ return unless $self->_array_has_eq($value, $doc->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_array_has_eq($value, $doc->{$key}) 0 0 0
171 } elsif (!ref $doc->{$key}) { # check the values are equal
172 0 ▶ return unless $doc->{$key} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} eq $value 0 0 0
173 } else { # we can't compare a non-scalar to a scalar, so return false
174 0 return;
175 }
176 } elsif (ref $value eq 'Regexp') { # if the value is a regex, we need to check
177 # for a match (or, if the attribute is an array
178 # in the document, we need to check at least one
179 # value in it matches it)
180 0 ▶ return unless $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} 0 0 0
181 0 ▶ if (ref $doc->{$key} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $doc->{$key} eq 'ARRAY') { } 0 0 0
elsif (not ref $doc->{$key}) { } 0 0 0
182 0 ▶ return unless $self->_array_has_re($value, $doc->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_array_has_re($value, $doc->{$key}) 0 0 0
183 } elsif (!ref $doc->{$key}) { # check the values match
184 0 ▶ return unless $doc->{$key} =~ $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} =~ /$value/ 0 0 0
185 } else { # we can't compare a non-scalar to a scalar, so return false
186 0 return;
187 }
188 } elsif (ref $value eq 'HASH') { # if the value is a hash, than it either contains
189 # advanced queries, or it's just a hash that we
190 # want the document to have as-is
191 0 ▶ unless ($self->_has_adv_que($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_adv_que($value)) { } 0 0 0
192 # value hash-ref doesn't have any advanced
193 # queries, we need to check our document
194 # has an attributes with exactly the same hash-ref
195 # (and name of course)
196 0 ▶ return unless Compare($value, $doc->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless Compare($value, $doc->{$key}) 0 0 0
197 } else {
198 # value contains advanced queries,
199 # we need to make sure our document has an
200 # attribute with the same name that matches
201 # all these queries
202 0 foreach my $q (keys %$value) {
203 0 my $term = $value->{$q};
204 0 ▶ if ($q eq '$gt') {
Branch: 13 branches
BranchTrueFalseTotal
if ($q eq '$gt') { } 0 0 0
elsif ($q eq '$gte') { } 0 0 0
elsif ($q eq '$lt') { } 0 0 0
elsif ($q eq '$gt') { } 0 0 0
elsif ($q eq '$eq') { } 0 0 0
elsif ($q eq '$ne') { } 0 0 0
elsif ($q eq '$exists') { } 0 0 0
elsif ($q eq '$mod' and ref $term eq 'ARRAY' and scalar @$term == 2) { } 0 0 0
elsif ($q eq '$in' and ref $term eq 'ARRAY') { } 0 0 0
elsif ($q eq '$nin' and ref $term eq 'ARRAY') { } 0 0 0
elsif ($q eq '$size' and $term =~ /^\d+$/) { } 0 0 0
elsif ($q eq '$all' and ref $term eq 'ARRAY') { } 0 0 0
elsif ($q eq '$type' and not ref $term) { } 0 0 0
205 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
206 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
207 0 ▶ return unless $doc->{$key} > $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} > $term 0 0 0
208 } else {
209 0 ▶ return unless $doc->{$key} gt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} gt $term 0 0 0
210 }
211 } elsif ($q eq '$gte') {
212 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
213 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
214 0 ▶ return unless $doc->{$key} >= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} >= $term 0 0 0
215 } else {
216 0 ▶ return unless $doc->{$key} ge $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} ge $term 0 0 0
217 }
218 } elsif ($q eq '$lt') {
219 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
220 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
221 0 ▶ return unless $doc->{$key} < $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} < $term 0 0 0
222 } else {
223 0 ▶ return unless $doc->{$key} lt $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} lt $term 0 0 0
224 }
225 } elsif ($q eq '$gt') {
226 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
227 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
228 0 ▶ return unless $doc->{$key} <= $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} <= $term 0 0 0
229 } else {
230 0 ▶ return unless $doc->{$key} le $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} le $term 0 0 0
231 }
232 } elsif ($q eq '$eq') {
233 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
234 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
235 0 ▶ return unless $doc->{$key} == $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} == $term 0 0 0
236 } else {
237 0 ▶ return unless $doc->{$key} eq $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} eq $term 0 0 0
238 }
239 } elsif ($q eq '$ne') {
240 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
241 0 ▶ if ($doc->{$key} =~ m/^\d+(\.\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->{$key} =~ /^\d+(\.\d+)?$/) { } 0 0 0
242 0 ▶ return unless $doc->{$key} != $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} != $term 0 0 0
243 } else {
244 0 ▶ return unless $doc->{$key} ne $term;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc->{$key} ne $term 0 0 0
245 }
246 } elsif ($q eq '$exists') {
247 0 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) { } 0 0 0
248 0 ▶ return unless exists $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $doc->{$key} 0 0 0
249 } else {
250 0 ▶ return if exists $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$key} 0 0 0
251 }
252 ▶ } elsif ($q eq '$mod' && ref $term eq 'ARRAY' && scalar @$term == 2) {
Condition0%0/6
$q eq "\$mod" and ref $term eq "ARRAY" and scalar @$term == 2
!ll&&!rl&&r
000
$q eq "\$mod" and ref $term eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
$q eq "\$mod" and ref $term eq "ARRAY" and scalar @$term == 2
$q eq "\$mod" ref $term eq "ARRAY" scalar @$term == 2
Truth table0%0/4
A and B and C
A= $q eq "\$mod"B= ref $term eq "ARRAY"C= scalar @$term == 2
ABCresult
0XX0
10X0
1100
1111
253 0 ▶ return unless defined $doc->{$key} && $doc->{$key} =~ m/^\d+(\.\d+)?$/ && $doc->{$key} % $term->[0] == $term->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and $doc->{$key} =~ /^\d+(\.\d+)?$/ and $doc->{$key} % $term->[0] == $term->[1] 0 0 0
Condition0%0/6
defined $doc->{$key} and $doc->{$key} =~ /^\d+(\.\d+)?$/ and $doc->{$key} % $term->[0] == $term->[1]
!ll&&!rl&&r
000
defined $doc->{$key} and $doc->{$key} =~ /^\d+(\.\d+)?$/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $doc->{$key} and $doc->{$key} =~ /^\d+(\.\d+)?$/ and $doc->{$key} % $term->[0] == $term->[1]
defined $doc->{$key} $doc->{$key} =~ /^\d+(\.\d+)?$/ $doc->{$key} % $term->[0] == $term->[1]
Truth table0%0/4
A and B and C
A= defined $doc->{$key}B= $doc->{$key} =~ /^\d+(\.\d+)?$/C= $doc->{$key} % $term->[0] == $term->[1]
ABCresult
0XX0
10X0
1100
1111
254 ▶ } elsif ($q eq '$in' && ref $term eq 'ARRAY') {
Condition0%0/3
$q eq "\$in" and ref $term eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$q eq "\$in" and ref $term eq "ARRAY"
$q eq "\$in" ref $term eq "ARRAY"
Truth table0%0/3
A and B
A= $q eq "\$in"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
255 0 ▶ return unless defined $doc->{$key} && $self->_value_in($doc->{$key}, $term);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and $self->_value_in($doc->{$key}, $term) 0 0 0
Condition0%0/3
defined $doc->{$key} and $self->_value_in($doc->{$key}, $term)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and $self->_value_in($doc->{$key}, $term)
defined $doc->{$key} $self->_value_in($doc->{$key}, $term)
Truth table0%0/3
A and B
A= defined $doc->{$key}B= $self->_value_in($doc->{$key}, $term)
ABresult
0X0
100
111
256 ▶ } elsif ($q eq '$nin' && ref $term eq 'ARRAY') {
Condition0%0/3
$q eq "\$nin" and ref $term eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$q eq "\$nin" and ref $term eq "ARRAY"
$q eq "\$nin" ref $term eq "ARRAY"
Truth table0%0/3
A and B
A= $q eq "\$nin"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
257 0 ▶ return unless defined $doc->{$key} && !$self->_value_in($doc->{$key}, $term);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not $self->_value_in($doc->{$key}, $term) 0 0 0
Condition0%0/3
defined $doc->{$key} and not $self->_value_in($doc->{$key}, $term)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not $self->_value_in($doc->{$key}, $term)
defined $doc->{$key} not $self->_value_in($doc->{$key}, $term)
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not $self->_value_in($doc->{$key}, $term)
ABresult
0X0
100
111
258 ▶ } elsif ($q eq '$size' && $term =~ m/^\d+$/) {
Condition0%0/3
$q eq "\$size" and $term =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$q eq "\$size" and $term =~ /^\d+$/
$q eq "\$size" $term =~ /^\d+$/
Truth table0%0/3
A and B
A= $q eq "\$size"B= $term =~ /^\d+$/
ABresult
0X0
100
111
259 0 ▶ return unless defined $doc->{$key} && ref $doc->{$key} eq 'ARRAY' && scalar @{$doc->{$key}} == $term;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and ref $doc->{$key} eq "ARRAY" and scalar @{$doc->{$key};} == $term 0 0 0
Condition0%0/6
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY" and scalar @{$doc->{$key};} == $term
!ll&&!rl&&r
000
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY" and scalar @{$doc->{$key};} == $term
defined $doc->{$key} ref $doc->{$key} eq "ARRAY" scalar @{$doc->{$key};} == $term
Truth table0%0/4
A and B and C
A= defined $doc->{$key}B= ref $doc->{$key} eq "ARRAY"C= scalar @{$doc->{$key};} == $term
ABCresult
0XX0
10X0
1100
1111
260 ▶ } elsif ($q eq '$all' && ref $term eq 'ARRAY') {
Condition0%0/3
$q eq "\$all" and ref $term eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$q eq "\$all" and ref $term eq "ARRAY"
$q eq "\$all" ref $term eq "ARRAY"
Truth table0%0/3
A and B
A= $q eq "\$all"B= ref $term eq "ARRAY"
ABresult
0X0
100
111
261 0 ▶ return unless defined $doc->{$key} && ref $doc->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and ref $doc->{$key} eq "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY"
defined $doc->{$key} ref $doc->{$key} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$key}B= ref $doc->{$key} eq "ARRAY"
ABresult
0X0
100
111
262 0 foreach (@$term) {
263 0 ▶ return unless $self->_value_in($_, $doc->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_value_in($_, $doc->{$key}) 0 0 0
264 }
265 ▶ } elsif ($q eq '$type' && !ref $term) {
Condition0%0/3
$q eq "\$type" and not ref $term
!ll&&!rl&&r
000
MC/DC0%0/2
$q eq "\$type" and not ref $term
$q eq "\$type" not ref $term
Truth table0%0/3
A and not(B)
A= $q eq "\$type"B= not ref $term
ABresult
0X0
100
111
266 0 ▶ if ($term eq 'int') {
Branch: 8 branches
BranchTrueFalseTotal
if ($term eq 'int') { } 0 0 0
elsif ($term eq 'double') { } 0 0 0
elsif ($term eq 'string') { } 0 0 0
elsif ($term eq 'array') { } 0 0 0
elsif ($term eq 'bool') { } 0 0 0
elsif ($term eq 'date') { } 0 0 0
elsif ($term eq 'null') { } 0 0 0
elsif ($term eq 'regex') { } 0 0 0
267 0 ▶ return unless defined $doc->{$key} && $doc->{$key} =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and $doc->{$key} =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $doc->{$key} and $doc->{$key} =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and $doc->{$key} =~ /^\d+$/
defined $doc->{$key} $doc->{$key} =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $doc->{$key}B= $doc->{$key} =~ /^\d+$/
ABresult
0X0
100
111
268 } elsif ($term eq 'double') {
269 0 ▶ return unless defined $doc->{$key} && $doc->{$key} =~ m/^\d+\.[1-9]\d*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and $doc->{$key} =~ /^\d+\.[1-9]\d*$/ 0 0 0
Condition0%0/3
defined $doc->{$key} and $doc->{$key} =~ /^\d+\.[1-9]\d*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and $doc->{$key} =~ /^\d+\.[1-9]\d*$/
defined $doc->{$key} $doc->{$key} =~ /^\d+\.[1-9]\d*$/
Truth table0%0/3
A and B
A= defined $doc->{$key}B= $doc->{$key} =~ /^\d+\.[1-9]\d*$/
ABresult
0X0
100
111
270 } elsif ($term eq 'string') {
271 0 ▶ return unless defined $doc->{$key} && $doc->{$key} =~ m/^[[:alnum:]]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and $doc->{$key} =~ /^[[:alnum:]]+$/ 0 0 0
Condition0%0/3
defined $doc->{$key} and $doc->{$key} =~ /^[[:alnum:]]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and $doc->{$key} =~ /^[[:alnum:]]+$/
defined $doc->{$key} $doc->{$key} =~ /^[[:alnum:]]+$/
Truth table0%0/3
A and B
A= defined $doc->{$key}B= $doc->{$key} =~ /^[[:alnum:]]+$/
ABresult
0X0
100
111
272 } elsif ($term eq 'array') {
273 0 ▶ return unless defined $doc->{$key} && ref $doc->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and ref $doc->{$key} eq "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and ref $doc->{$key} eq "ARRAY"
defined $doc->{$key} ref $doc->{$key} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$key}B= ref $doc->{$key} eq "ARRAY"
ABresult
0X0
100
111
274 } elsif ($term eq 'bool') {
275 # boolean - not really supported, will always return true since everything in Perl is a boolean
276 } elsif ($term eq 'date') {
277 0 ▶ return unless defined $doc->{$key} && !ref $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and not ref $doc->{$key} 0 0 0
Condition0%0/3
defined $doc->{$key} and not ref $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and not ref $doc->{$key}
defined $doc->{$key} not ref $doc->{$key}
Truth table0%0/3
A and not(B)
A= defined $doc->{$key}B= not ref $doc->{$key}
ABresult
0X0
100
111
278 0 ▶ my $date = try { DateTime::Format::W3CDTF->parse_datetime($doc->{$key}) } catch { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 0 ▶ return unless blessed $date && $date->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($date) and $date->isa("DateTime") 0 0 0
Condition0%0/3
builtin::blessed($date) and $date->isa("DateTime")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($date) and $date->isa("DateTime")
builtin::blessed($date) $date->isa("DateTime")
Truth table0%0/3
A and B
A= builtin::blessed($date)B= $date->isa("DateTime")
ABresult
0X0
100
111
280 } elsif ($term eq 'null') {
281 0 ▶ return unless exists $doc->{$key} && !defined $doc->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $doc->{$key} and not defined $doc->{$key} 0 0 0
Condition0%0/3
exists $doc->{$key} and not defined $doc->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $doc->{$key} and not defined $doc->{$key}
exists $doc->{$key} not defined $doc->{$key}
Truth table0%0/3
A and not(B)
A= exists $doc->{$key}B= not defined $doc->{$key}
ABresult
0X0
100
111
282 } elsif ($term eq 'regex') {
283 0 ▶ return unless defined $doc->{$key} && ref $doc->{$key} eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc->{$key} and ref $doc->{$key} eq "Regexp" 0 0 0
Condition0%0/3
defined $doc->{$key} and ref $doc->{$key} eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$key} and ref $doc->{$key} eq "Regexp"
defined $doc->{$key} ref $doc->{$key} eq "Regexp"
Truth table0%0/3
A and B
A= defined $doc->{$key}B= ref $doc->{$key} eq "Regexp"
ABresult
0X0
100
111
284 }
285 }
286 }
287 }
288 }
289  
290 0 return 1;
291 }
292  
293 =head2 _array_has_eq( $value, \@array )
294  
295 =cut
296  
297 0 ▶sub _array_has_eq {
Subroutine _array_has_eq: not called CC=3 SCAR=24.8
298 0 my ($self, $value, $array) = @_;
299  
300 0 foreach (@$array) {
301 0 ▶ return 1 if $_ eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $value 0 0 0
302 }
303  
304 0 return;
305 }
306  
307 =head2 _array_has_re( $regex, \@array )
308  
309 =cut
310  
311 0 ▶sub _array_has_re {
Subroutine _array_has_re: not called CC=3 SCAR=24.8
312 0 my ($self, $re, $array) = @_;
313  
314 0 foreach (@$array) {
315 0 ▶ return 1 if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 0 0 0
316 }
317  
318 0 return;
319 }
320  
321 =head2 _has_adv_que( \%hash )
322  
323 =cut
324  
325 0 ▶sub _has_adv_que {
Subroutine _has_adv_que: not called CC=3 SCAR=24.8
326 0 my ($self, $hash) = @_;
327  
328 0 foreach ('$gt', '$gte', '$lt', '$lte', '$all', '$exists', '$mod', '$ne', '$in', '$nin', '$size', '$type') {
329 0 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 0 0 0
330 }
331  
332 0 return;
333 }
334  
335 =head2 _value_in( $value, \@array )
336  
337 =cut
338  
339 0 ▶sub _value_in {
Subroutine _value_in: not called CC=10 SCAR=47.0
340 0 my ($self, $value, $array) = @_;
341  
342 0 foreach (@$array) {
343 0 ▶ next if m/^\d+(\.\d+)?$/ && $value !~ m/^\d+(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/ 0 0 0
Condition0%0/3
/^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
/^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/
/^\d+(\.\d+)?$/ not $value =~ /^\d+(\.\d+)?$/
Truth table0%0/3
A and not(B)
A= /^\d+(\.\d+)?$/B= not $value =~ /^\d+(\.\d+)?$/
ABresult
0X0
100
111
344 0 ▶ next if !m/^\d+(\.\d+)?$/ && $value =~ m/^\d+$(\.\d+)?/;
Branch: 1 branch
BranchTrueFalseTotal
if not /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/ 0 0 0
Condition0%0/3
not /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/
!ll&&!rl&&r
000
MC/DC0%0/2
not /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/
not /^\d+(\.\d+)?$/ $value =~ /^\d+$(\.\d+)?/
Truth table0%0/3
not(A) and B
A= not /^\d+(\.\d+)?$/B= $value =~ /^\d+$(\.\d+)?/
ABresult
0X0
100
111
345 0 ▶ return 1 if m/^\d+(\.\d+)?$/ && $value == $_;
Branch: 1 branch
BranchTrueFalseTotal
if /^\d+(\.\d+)?$/ and $value == $_ 0 0 0
Condition0%0/3
/^\d+(\.\d+)?$/ and $value == $_
!ll&&!rl&&r
000
MC/DC0%0/2
/^\d+(\.\d+)?$/ and $value == $_
/^\d+(\.\d+)?$/ $value == $_
Truth table0%0/3
A and B
A= /^\d+(\.\d+)?$/B= $value == $_
ABresult
0X0
100
111
346 0 ▶ return 1 if !m/^\d+(\.\d+)?$/ && $value eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if not /^\d+(\.\d+)?$/ and $value eq $_ 0 0 0
Condition0%0/3
not /^\d+(\.\d+)?$/ and $value eq $_
!ll&&!rl&&r
000
MC/DC0%0/2
not /^\d+(\.\d+)?$/ and $value eq $_
not /^\d+(\.\d+)?$/ $value eq $_
Truth table0%0/3
not(A) and B
A= not /^\d+(\.\d+)?$/B= $value eq $_
ABresult
0X0
100
111
347 }
348  
349 0 return;
350 }
351  
352 =head1 AUTHOR
353  
354 Ido Perlmuter, C<< <ido at ido50.net> >>
355  
356 =head1 BUGS
357  
358 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
359 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
360 automatically be notified of progress on your bug as I make changes.
361  
362 =head1 SUPPORT
363  
364 You can find documentation for this module with the perldoc command.
365  
366 perldoc Giddy::Role::DocumentMatcher
367  
368 You can also look for information at:
369  
370 =over 4
371  
372 =item * RT: CPAN's request tracker
373  
374 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
375  
376 =item * AnnoCPAN: Annotated CPAN documentation
377  
378 L<http://annocpan.org/dist/Giddy>
379  
380 =item * CPAN Ratings
381  
382 L<http://cpanratings.perl.org/d/Giddy>
383  
384 =item * Search CPAN
385  
386 L<http://search.cpan.org/dist/Giddy/>
387  
388 =back
389  
390 =head1 LICENSE AND COPYRIGHT
391  
392 Copyright 2011 Ido Perlmuter.
393  
394 This program is free software; you can redistribute it and/or modify it
395 under the terms of either: the GNU General Public License as published
396 by the Free Software Foundation; or the Artistic License.
397  
398 See http://dev.perl.org/licenses/ for more information.
399  
400 =cut
401  
402 2 1;
↑ Summary Giddy/Role/DocumentUpdater.pm »