Giddy/Role/DocumentUpdater.pm

filter: Statementstmt 11.8% 12 / 101 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 50.0% 3 / 6 Podpod n/a totaltotal 5.5% 15 / 272 CC 76 SCAR 81.5
CC
76
Cov
5%
_update_document
4290.0
_index_of
110.0
_has_adv_upd
12.0
CRAP 3456.7
« Giddy/Role/DocumentMatcher.pm ↑ Summary Giddy/Collection.pm »
1 package Giddy::Role::DocumentUpdater;
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  
8 2 our $VERSION = "0.013_001";
9 2 $VERSION = eval $VERSION;
10  
11 =head1 NAME
12  
13 Giddy::Role::DocumentUpdater - Provides document updating for Giddy::Collection
14  
15 =head1 VERSION
16  
17 version 0.013_001
18  
19 =head1 SYNOPSIS
20  
21 # used internally
22  
23 =head1 DESCRIPTION
24  
25 This role provides document updating capabilities to L<Giddy::Collection> and L<Giddy::Collection::InMemory>.
26  
27 =head1 METHODS
28  
29 =head2 _update_document( \%obj, \%doc )
30  
31 =cut
32  
33 0 ▶sub _update_document {
Subroutine _update_document: not called CC=65 SCAR=83.6
34 0 my ($self, $obj, $doc) = @_;
35  
36 0 ▶ croak "You must provide an updates hash-ref to update according to."
Branch: 1 branch
BranchTrueFalseTotal
unless $obj and ref $obj eq "HASH" 0 0 0
Condition0%0/3
$obj and ref $obj eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and ref $obj eq "HASH"
$obj ref $obj eq "HASH"
Truth table0%0/3
A and B
A= $objB= ref $obj eq "HASH"
ABresult
0X0
100
111
37 unless $obj && ref $obj eq 'HASH';
38 0 ▶ croak "You must provide a document hash-ref to update (can be empty)."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc and ref $doc eq "HASH" 0 0 0
Condition0%0/3
defined $doc and ref $doc eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc and ref $doc eq "HASH"
defined $doc ref $doc eq "HASH"
Truth table0%0/3
A and B
A= defined $docB= ref $doc eq "HASH"
ABresult
0X0
100
111
39 unless defined $doc && ref $doc eq 'HASH';
40  
41 # we only need to do something if the $obj hash-ref has any advanced
42 # update operations, otherwise $obj is meant to be the new $doc
43  
44 0 ▶ if ($self->_has_adv_upd($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_adv_upd($obj)) { } 0 0 0
45 0 foreach my $op (keys %$obj) {
46 0 ▶ next if $_ eq '_name';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_name" 0 0 0
47 0 ▶ if ($op eq '$inc') {
Branch: 10 branches
BranchTrueFalseTotal
if ($op eq '$inc') { } 0 0 0
elsif ($op eq '$set') { } 0 0 0
elsif ($op eq '$unset') { } 0 0 0
elsif ($op eq '$push') { } 0 0 0
elsif ($op eq '$pushAll') { } 0 0 0
elsif ($op eq '$addToSet') { } 0 0 0
elsif ($op eq '$pop') { } 0 0 0
elsif ($op eq '$rename') { } 0 0 0
elsif ($op eq '$pull') { } 0 0 0
elsif ($op eq '$pullAll') { } 0 0 0
48 # increase numerically
49 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
50 0 foreach my $field (keys %{$obj->{$op}}) {
51 0 ▶ $doc->{$field} ||= 0;
Condition0%0/2
$doc->{$field} ||= 0
l!l
00
MC/DC0%0/1
$doc->{$field} ||= 0
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
52 0 $doc->{$field} += $obj->{$op}->{$field};
53 }
54 } elsif ($op eq '$set') {
55 # set key-value pairs
56 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
57 0 foreach my $field (keys %{$obj->{$op}}) {
58 0 $doc->{$field} = $obj->{$op}->{$field};
59 }
60 } elsif ($op eq '$unset') {
61 # remove key-value pairs
62 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
63 0 foreach my $field (keys %{$obj->{$op}}) {
64 0 ▶ delete $doc->{$field} if $obj->{$op}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$op}{$field} 0 0 0
65 }
66 } elsif ($op eq '$push') {
67 # push values to end of arrays
68 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
69 0 foreach my $field (keys %{$obj->{$op}}) {
70 croak "The $field attribute is not an array in $doc->{_name}."
71 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
72 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
73 0 push(@{$doc->{$field}}, $obj->{$op}->{$field});
74 }
75 } elsif ($op eq '$pushAll') {
76 # push a list of values to end of arrays
77 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
78 0 foreach my $field (keys %{$obj->{$op}}) {
79 croak "The $field attribute is not an array in $doc->{_name}."
80 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
81 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
82 0 push(@{$doc->{$field}}, @{$obj->{$op}->{$field}});
83 }
84 } elsif ($op eq '$addToSet') {
85 # push values to arrays only if they're not already there
86 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
87 0 foreach my $field (keys %{$obj->{$op}}) {
88 croak "The $field attribute is not an array in $doc->{_name}."
89 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
90 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
91 0 push(@{$doc->{$field}}, $obj->{$op}->{$field})
92 0 ▶ unless defined $self->_index_of($obj->{$op}->{$field}, $doc->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_index_of($obj->{$op}{$field}, $doc->{$field}) 0 0 0
93 }
94 } elsif ($op eq '$pop') {
95 # pop values from arrays
96 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
97 0 foreach my $field (keys %{$obj->{$op}}) {
98 croak "The $field attribute is not an array in $doc->{_name}."
99 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
100 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
101 0 splice(@{$doc->{$field}}, $obj->{$op}->{$field}, 1);
102 }
103 } elsif ($op eq '$rename') {
104 # rename attributes
105 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
106 0 foreach my $field (keys %{$obj->{$op}}) {
107 $doc->{$obj->{$op}->{$field}} = delete $doc->{$field}
108 0 ▶ if exists $doc->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $doc->{$field} 0 0 0
109 }
110 } elsif ($op eq '$pull') {
111 # remove values from arrays
112 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
113 0 foreach my $field (keys %{$obj->{$op}}) {
114 croak "The $field attribute is not an array in $doc->{_name}."
115 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
116 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
117 0 my $i = $self->_index_of($obj->{$op}->{$field}, $doc->{$field});
118 0 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 0 0 0
119 0 splice(@{$doc->{$field}}, $i, 1);
120 0 $i = $self->_index_of($obj->{$op}->{$field}, $doc->{$field});
121 }
122 }
123 } elsif ($op eq '$pullAll') {
124 # remove a list of values from arrays
125 0 ▶ next unless ref $obj->{$op} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj->{$op} eq "HASH" 0 0 0
126 0 foreach my $field (keys %{$obj->{$op}}) {
127 croak "The $field attribute is not an array in $doc->{_name}."
128 0 ▶ if defined $doc->{$field} && ref $doc->{$field} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc->{$field} and ref $doc->{$field} ne "ARRAY" 0 0 0
Condition0%0/3
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $doc->{$field} and ref $doc->{$field} ne "ARRAY"
defined $doc->{$field} ref $doc->{$field} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $doc->{$field}B= ref $doc->{$field} ne "ARRAY"
ABresult
0X0
100
111
129 0 ▶ $doc->{$field} ||= [];
Condition0%0/2
$doc->{$field} ||= []
l!l
00
MC/DC0%0/1
$doc->{$field} ||= []
$doc->{$field}
Truth table0%0/2
A
A= $doc->{$field}
Aresult
11
00
130 0 foreach my $value (@{$obj->{$op}->{$field}}) {
131 0 my $i = $self->_index_of($value, $doc->{$field});
132 0 ▶ while (defined $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i) 0 0 0
133 0 splice(@{$doc->{$field}}, $i, 1);
134 0 $i = $self->_index_of($value, $doc->{$field});
135 }
136 }
137 }
138 }
139 }
140 } else {
141 # $obj is actually the new $doc
142 0 foreach (keys %$obj) {
143 0 ▶ next if $_ eq '_name';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_name" 0 0 0
144 0 $doc->{$_} = $obj->{$_};
145 }
146 }
147 }
148  
149 =head2 _has_adv_upd( \%hash )
150  
151 =cut
152  
153 0 ▶sub _has_adv_upd {
Subroutine _has_adv_upd: not called CC=3 SCAR=24.8
154 0 my ($self, $hash) = @_;
155  
156 0 foreach ('$inc', '$set', '$unset', '$push', '$pushAll', '$addToSet', '$pop', '$pull', '$pullAll', '$rename', '$bit') {
157 0 ▶ return 1 if exists $hash->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$_} 0 0 0
158 }
159  
160 0 return;
161 }
162  
163 =head2 _index_of( $value, \@array )
164  
165 =cut
166  
167 0 ▶sub _index_of {
Subroutine _index_of: not called CC=10 SCAR=47.0
168 0 my ($self, $value, $array) = @_;
169  
170 0 ▶ for (my $i = 0; $i < scalar @$array; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$array) 0 0 0
171 0 ▶ next if $array->[$i] =~ m/^\d+(\.\d+)?$/ && $value !~ m/^\d+(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] =~ /^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/ 0 0 0
Condition0%0/3
$array->[$i] =~ /^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
$array->[$i] =~ /^\d+(\.\d+)?$/ and not $value =~ /^\d+(\.\d+)?$/
$array->[$i] =~ /^\d+(\.\d+)?$/ not $value =~ /^\d+(\.\d+)?$/
Truth table0%0/3
A and not(B)
A= $array->[$i] =~ /^\d+(\.\d+)?$/B= not $value =~ /^\d+(\.\d+)?$/
ABresult
0X0
100
111
172 0 ▶ next if $array->[$i] !~ m/^\d+(\.\d+)?$/ && $value =~ m/^\d+$(\.\d+)?/;
Branch: 1 branch
BranchTrueFalseTotal
if not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/ 0 0 0
Condition0%0/3
not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/
!ll&&!rl&&r
000
MC/DC0%0/2
not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value =~ /^\d+$(\.\d+)?/
not $array->[$i] =~ /^\d+(\.\d+)?$/ $value =~ /^\d+$(\.\d+)?/
Truth table0%0/3
not(A) and B
A= not $array->[$i] =~ /^\d+(\.\d+)?$/B= $value =~ /^\d+$(\.\d+)?/
ABresult
0X0
100
111
173 0 ▶ return $i if $array->[$i] =~ m/^\d+(\.\d+)?$/ && $value == $array->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $array->[$i] =~ /^\d+(\.\d+)?$/ and $value == $array->[$i] 0 0 0
Condition0%0/3
$array->[$i] =~ /^\d+(\.\d+)?$/ and $value == $array->[$i]
!ll&&!rl&&r
000
MC/DC0%0/2
$array->[$i] =~ /^\d+(\.\d+)?$/ and $value == $array->[$i]
$array->[$i] =~ /^\d+(\.\d+)?$/ $value == $array->[$i]
Truth table0%0/3
A and B
A= $array->[$i] =~ /^\d+(\.\d+)?$/B= $value == $array->[$i]
ABresult
0X0
100
111
174 0 ▶ return $i if $array->[$i] !~ m/^\d+(\.\d+)?$/ && $value eq $array->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value eq $array->[$i] 0 0 0
Condition0%0/3
not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value eq $array->[$i]
!ll&&!rl&&r
000
MC/DC0%0/2
not $array->[$i] =~ /^\d+(\.\d+)?$/ and $value eq $array->[$i]
not $array->[$i] =~ /^\d+(\.\d+)?$/ $value eq $array->[$i]
Truth table0%0/3
not(A) and B
A= not $array->[$i] =~ /^\d+(\.\d+)?$/B= $value eq $array->[$i]
ABresult
0X0
100
111
175 }
176  
177 0 return;
178 }
179  
180 =head1 AUTHOR
181  
182 Ido Perlmuter, C<< <ido at ido50.net> >>
183  
184 =head1 BUGS
185  
186 Please report any bugs or feature requests to C<bug-giddy at rt.cpan.org>, or through
187 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Giddy>. I will be notified, and then you'll
188 automatically be notified of progress on your bug as I make changes.
189  
190 =head1 SUPPORT
191  
192 You can find documentation for this module with the perldoc command.
193  
194 perldoc Giddy::Role::DocumentUpdater
195  
196 You can also look for information at:
197  
198 =over 4
199  
200 =item * RT: CPAN's request tracker
201  
202 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Giddy>
203  
204 =item * AnnoCPAN: Annotated CPAN documentation
205  
206 L<http://annocpan.org/dist/Giddy>
207  
208 =item * CPAN Ratings
209  
210 L<http://cpanratings.perl.org/d/Giddy>
211  
212 =item * Search CPAN
213  
214 L<http://search.cpan.org/dist/Giddy/>
215  
216 =back
217  
218 =head1 LICENSE AND COPYRIGHT
219  
220 Copyright 2011 Ido Perlmuter.
221  
222 This program is free software; you can redistribute it and/or modify it
223 under the terms of either: the GNU General Public License as published
224 by the Free Software Foundation; or the Artistic License.
225  
226 See http://dev.perl.org/licenses/ for more information.
227  
228 =cut
229  
230 2 1;
« Giddy/Role/DocumentMatcher.pm ↑ Summary Giddy/Collection.pm »