Valiant/ResultSet.pm

filter: Statementstmt 78.2% 126 / 161 Branchbran 66.1% 45 / 68 Conditioncond 51.8% 14 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 73.9% 17 / 23 Podpod 50.0% 5 / 10 totaltotal 69.5% 212 / 305 CC 52 SCAR 24.6
CC
52
Cov
72%
set_recursively
24.8
contains
20.0
_dump_row
13.2
CRAP 11.7
« Valiant/Result/HTML/FormFields.pm ↑ Summary Valiant/Validator/SetSize.pm »
1 package DBIx::Class::Valiant::ResultSet;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use namespace::autoclean -also => ['debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use DBIx::Class::Valiant::Util::Exception::TooManyRows;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 with 'Valiant::Naming';
13  
14 # Gotta jump thru these hoops because of the way the Catalyst
15 # DBIC model messes with the result namespace but not the schema
16 # namespace
17  
18 0 ▶sub namespace {
Subroutine namespace: not called CC=2 SCAR=17.9
Pod undocumented
19 0 my $self = shift;
20 0 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
21 0 my ($ns) = ($class =~m/^(.+)::.+$/);
22 0 return $ns;
23 }
24  
25 0 ▶sub i18n_scope { 'valiant' }
Subroutine i18n_scope: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 0 ▶sub i18n_lookup {
Subroutine i18n_lookup: not called CC=3 SCAR=20.0
Pod undocumented
28 0 my ($class_or_self, $arg) = @_;
29 0 ▶ my $class = ref($class_or_self) ? ref($class_or_self) : $class_or_self;
Branch: 1 branch
BranchTrueFalseTotal
ref $class_or_self ? : 0 0 0
30 12 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 0 my @proposed = @{"${class}::ISA"};
32 0 ▶ push @proposed, $class_or_self->i18n_metadata if $class_or_self->can('i18n_metadata');
Branch: 1 branch
BranchTrueFalseTotal
if $class_or_self->can("i18n_metadata") 0 0 0
33 0 return grep { $_->can('model_name') } ($class, @proposed);
34 }
35  
36 205 ▶sub skip_validation {
Subroutine skip_validation: called CC=3 SCAR=11.0
Pod documented
37 205 my ($self, $arg) = @_;
38 205 ▶ if(defined($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 204 205
39 1 ▶ $self->{_skip_validation} = $arg ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 1 0 1
40 }
41 205 return $self->{_skip_validation};
42 }
43
44 1 ▶sub skip_validate {
Subroutine skip_validate: called CC=1 SCAR=0.0
Pod documented
45 1 my ($self) = @_;
46 1 $self->skip_validation(1);
47 1 return $self;
48 }
49 0 ▶sub do_validate {
Subroutine do_validate: not called CC=1 SCAR=6.9
Pod documented
50 0 my ($self) = @_;
51 0 $self->skip_validation(0);
52 0 return $self;
53 }
54  
55 63 ▶sub _nested_info_for_related {
Subroutine _nested_info_for_related: called CC=2 SCAR=7.0
56 63 my ($self, $related) = @_;
57 63 my %nested = $self->result_class->accept_nested_for;
58 63 ▶ my %info = %{ $nested{$related}||+{} };
Condition50%1/2
$nested{$related} || {}
l!l
630
MC/DC0%0/1
$nested{$related} || {}
$nested{$related}
Truth table50%1/2
A
A= $nested{$related}
Aresult
11
00
59 63 return %info;
60 }
61  
62 63 ▶sub _related_limit {
Subroutine _related_limit: called CC=4 SCAR=14.0
63 63 my ($self, $related) = @_;
64 63 my %info = $self->_nested_info_for_related($related);
65 63 ▶ if(my $limit_proto = $info{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit_proto = $info{"limit"}) 6 57 63
66 6 ▶ my $limit = (ref($limit_proto)||'' eq 'CODE') ? $limit_proto->($self) : $limit_proto;
Branch: 1 branch
BranchTrueFalseTotal
ref $limit_proto || !1 ? : 0 6 6
Condition50%1/2
ref $limit_proto or
l!l
06
MC/DC0%0/1
ref $limit_proto or
ref $limit_proto
Truth table50%1/2
A
A= ref $limit_proto
Aresult
11
00
67 6 return 1, $limit;
68 }
69 57 return 0, undef;
70 }
71  
72 # This is just an alias so that we can decouple the Valiant API from DBIC
73 0 ▶sub build { shift->new_result( shift || +{}) }
Subroutine build: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
74  
75 204 ▶sub new_result {
Subroutine new_result: called CC=12 SCAR=25.0
Pod undocumented
76 204 my ($self, $fields, @args) = @_;
77 204 my $context = delete $fields->{__context};
78  
79 204 my %related = ();
80 204 my %nested = $self->result_class->accept_nested_for;
81
82 204 foreach my $associated (keys %nested) {
83 $related{$associated} = delete($fields->{$associated})
84 141 ▶ if exists($fields->{$associated});
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields->{$associated} 64 77 141
85 }
86  
87 # Remove any relationed keys we didn't find with the allows nested
88 204 my @rel_names = $self->result_source->relationships();
89 204 ▶ my @m2m_names = keys %{ $self->result_class->_m2m_metadata ||+{} };
Condition50%1/2
$self->result_class->_m2m_metadata || {}
l!l
2040
MC/DC0%0/1
$self->result_class->_m2m_metadata || {}
$self->result_class->_m2m_metadata
Truth table50%1/2
A
A= $self->result_class->_m2m_metadata
Aresult
11
00
90  
91 204 my %found = map { $_ => delete($fields->{$_}) } @rel_names, @m2m_names;
92  
93 204 ▶ if(grep { defined $_ } values %found) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_;} values %found) 0 204 204
94 0 my $related = join(', ', grep { $found{$_} } keys %found);
95 0 die "You are trying to create a relationship ($related) on @{[$self->result_source->name ]} without setting 'accept_nested_for'";
96 }
97  
98 204 my $result = $self->next::method($fields, @args);
99 204 ▶ $result->{__VALIANT_CREATE_ARGS}{context} = $context if $context; # Need this for ->insert
Branch: 1 branch
BranchTrueFalseTotal
if $context 12 192 204
100 204 ▶ $result->skip_validation(1) if $self->skip_validation;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_validation 1 203 204
101  
102 204 debug 2, "made new_result @{[ $result ]}";
103 204 RELATED: foreach my $related(keys %related) {
104  
105 64 ▶ if(my $cb = $nested{$related}->{reject_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $nested{$related}{"reject_if"}) 3 61 64
106 3 my $response = $cb->($result, $related{$related});
107 3 ▶ next RELATED if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 1 2 3
108 }
109  
110 63 my ($has_limit, $limit) = $self->_related_limit($related);
111 63 ▶ if($has_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_limit) 6 57 63
112 6 my $num = scalar @{$related{$related}};
113 6 ▶ DBIx::Class::Valiant::Util::Exception::TooManyRows
Branch: 1 branch
BranchTrueFalseTotal
if $num > $limit 1 5 6
114 ->throw(
115 limit=>$limit,
116 attempted=>$num,
117 related=>$related,
118 me=>$self->result_source->name,
119 ) if $num > $limit;
120 }
121  
122 62 $result->set_related_from_params($related, $related{$related});
123 }
124  
125 201 return $result;
126 }
127  
128 # Utility methods
129  
130 # this should cache results betters
131 0 ▶sub contains {
Subroutine contains: not called CC=4 SCAR=30.0
Pod documented
132 0 my ($self, $row) = @_;
133 0 my %pk = map { $_ => $row->$_ }
134 $self->result_source->primary_columns;
135 0 foreach my $item ($self->all) {
136 0 ▶ next if $item->is_removed;
Branch: 1 branch
BranchTrueFalseTotal
if $item->is_removed 0 0 0
137 my @matches = grep {
138 0 $item->get_column($_) eq $pk{$_}
139 } keys %pk;
140 0 ▶ return 1 if scalar(@matches) == keys %pk;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matches == keys %pk 0 0 0
141 }
142 0 return 0;
143 }
144  
145 # This is a work in progress, this needs to really use code from ::Result
146 # so that is working against an already loaded resultset, so that we properly
147 # handle deletes and so forth.
148 # $self, $data, { rollback_on_invalid=>0||1 }
149 2 ▶sub set_recursively {
Subroutine set_recursively: called CC=15 SCAR=32.1
Pod undocumented
150 2 my ($self, $data, $opts) = @_;
151 2 ▶ my $rollback_on_invalid = delete($opts->{rollback_on_invalid}) || 0;
Condition100%2/2
delete $opts->{'rollback_on_invalid'} || 0
l!l
11
MC/DC100%1/1
delete $opts->{'rollback_on_invalid'} || 0
delete $opts->{'rollback_on_invalid'}
Truth table100%2/2
A
A= delete $opts->{'rollback_on_invalid'}
Aresult
11
00
152 2 my @results = ();
153  
154 2 ▶ $self->result_source->schema->txn_begin if $rollback_on_invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $rollback_on_invalid 1 1 2
155  
156 2 my $err = 0;
157 2 foreach my $row_data (@$data) {
158 6 ▶ if(ref($row_data) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $row_data eq 'HASH') { } 6 0 6
elsif (ref $row_data and $row_data->isa('DBIx::Class::Row')) { } 0 0 0
159 6 my $new_result = $self->new_result(+{});
160 6 $new_result->set_columns_recursively($row_data);
161 6 $new_result->insert_or_update;
162 6 ▶ $err ||= $new_result->errors->count ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$new_result->errors->count ? : 2 2 4
Condition100%3/3
$err ||= $new_result->errors->count ? 1 : 0
l!l&&r!l&&!r
222
MC/DC100%2/2
$err ||= $new_result->errors->count ? 1 : 0
$err $new_result->errors->count ? 1 : 0
Truth table100%3/3
A ||= B
A= $errB= $new_result->errors->count ? 1 : 0
ABresult
1X1
011
000
163 6 push @results, $new_result;
164 ▶ } elsif (ref($row_data) && $row_data->isa('DBIx::Class::Row')) {
Condition0%0/3
ref $row_data and $row_data->isa("DBIx::Class::Row")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $row_data and $row_data->isa("DBIx::Class::Row")
ref $row_data $row_data->isa("DBIx::Class::Row")
Truth table0%0/3
A and B
A= ref $row_dataB= $row_data->isa("DBIx::Class::Row")
ABresult
0X0
100
111
165 0 $row_data->insert_or_update;
166 0 ▶ $err ||= $row_data->errors->count ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$row_data->errors->count ? : 0 0 0
Condition0%0/3
$err ||= $row_data->errors->count ? 1 : 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$err ||= $row_data->errors->count ? 1 : 0
$err $row_data->errors->count ? 1 : 0
Truth table0%0/3
A ||= B
A= $errB= $row_data->errors->count ? 1 : 0
ABresult
1X1
011
000
167 0 push @results, $row_data;
168 } else {
169 0 die "Don't know how to handle row data of type @{[ ref($row_data) ]}";
170 }
171 }
172  
173 2 ▶ $self->result_source->schema->txn_rollback if $rollback_on_invalid && $err;
Branch: 1 branch
BranchTrueFalseTotal
if $rollback_on_invalid and $err 1 1 2
Condition66%2/3
$rollback_on_invalid and $err
!ll&&!rl&&r
101
MC/DC50%1/2
$rollback_on_invalid and $err
$rollback_on_invalid $err
Truth table66%2/3
A and B
A= $rollback_on_invalidB= $err
ABresult
0X0
100
111
174 2 ▶ $self->result_source->schema->txn_commit if $rollback_on_invalid && !$err;
Branch: 1 branch
BranchTrueFalseTotal
if $rollback_on_invalid and not $err 0 2 2
Condition66%2/3
$rollback_on_invalid and not $err
!ll&&!rl&&r
110
MC/DC0%0/2
$rollback_on_invalid and not $err
$rollback_on_invalid not $err
Truth table66%2/3
A and not(B)
A= $rollback_on_invalidB= not $err
ABresult
0X0
100
111
175  
176 2 $self->set_cache(\@results);
177 2 my @rs_with_errs = grep { $_->errors->count } @results;
178 2 return $self, @rs_with_errs;
179 }
180  
181 # $self, ${ cached_only => 0||1, depth => $num, _seen => +{}, _current_depth => 0, allow_undef => 0||1 }
182 2 ▶sub _dump_resultset {
Subroutine _dump_resultset: called CC=2 SCAR=6.9
183 2 my ($resultset, $seen) = @_;
184 2 my $name = $resultset->result_source->name;
185 2 my @data;
186  
187 2 $resultset->reset;
188 2 ▶ while (my $row = $resultset->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $resultset->next) 6 2 8
189 6 my $data = _dump_row($row, $seen);
190 6 push @data, $data;
191 }
192 2 $resultset->reset;
193  
194 2 return +{ $name => \@data};
195 }
196  
197 # Recursive function to dump a row and its relationships
198 16 ▶sub _dump_row {
Subroutine _dump_row: called CC=13 SCAR=25.8
199 16 my ($row, $seen) = @_;
200 16 ▶ $seen ||= +{};
Condition100%2/2
$seen ||= {}
l!l
106
MC/DC100%1/1
$seen ||= {}
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
201  
202 # Prevent infinite recursion in case of circular relationships
203 16 my $row_id = Scalar::Util::refaddr($row);
204 16 ▶ return if $seen->{$row_id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$row_id}++ 4 12 16
205  
206 12 my %data = $row->get_columns;
207 12 my %errors = ();
208 12 foreach my $attr (keys %data) {
209 20 my @errors = $row->errors->full_messages_for($attr);
210 20 ▶ $errors{$attr} = \@errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 6 14 20
211 }
212 12 my @model_errors = $row->errors->model_messages;
213 12 ▶ $errors{"*"} = \@model_errors if @model_errors;
Branch: 1 branch
BranchTrueFalseTotal
if @model_errors 0 12 12
214  
215 12 my @relationships = $row->result_source->relationships;
216 12 foreach my $rel_name (@relationships) {
217 18 ▶ next unless exists $row->{_relationship_data}{$rel_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{"_relationship_data"}{$rel_name} 8 10 18
218 10 my $related = $row->{_relationship_data}{$rel_name};
219 10 ▶ if (defined $related) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $related) 10 0 10
220 10 ▶ if ($related->isa('DBIx::Class::ResultSet')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($related->isa('DBIx::Class::ResultSet')) { } 0 10 10
elsif ($related->isa('DBIx::Class::Row')) { } 10 0 10
221 0 $data{$rel_name} = $related->_dump_resultset($seen);
222 } elsif ($related->isa('DBIx::Class::Row')) {
223 10 my @rel_errors = $row->errors->full_messages_for($rel_name);
224 10 ▶ $errors{$rel_name} = \@rel_errors if @rel_errors;
Branch: 1 branch
BranchTrueFalseTotal
if @rel_errors 2 8 10
225 10 my $recursed = _dump_row($related, $seen);
226 10 ▶ next unless $recursed;
Branch: 1 branch
BranchTrueFalseTotal
unless $recursed 4 6 10
227 6 $data{$rel_name} = $recursed;
228 }
229 }
230 }
231 12 return +{ data => \%data, errors => \%errors };
232 }
233 12 1;
234  
235 =head1 NAME
236  
237 DBIx::Class::Valiant::ResultSet - Validation support for resultsets
238  
239 =head1 SYNOPSIS
240  
241 package Example::Schema::ResultSet::Person;
242  
243 use base 'DBIx::Class::ResultSet';
244  
245 __PACKAGE__->load_components('Valiant::ResultSet');
246  
247 See the C<t/dbic> tests in the distribution for more complete example
248 setups.
249  
250 =head1 DESCRIPTION
251  
252 A component that needs to be used on any result classes for which you want to add
253 L<Valiant> validations on. Its best to add this to your base and default resultset
254 classes if you plan to use L<DBIx::Class::Valiant> across all your result classes.
255  
256 =head1 METHODS
257  
258 This component adds the following methods to your resultset classes.
259  
260 =head2 skip_validation (1|0)
261  
262 $schema->resultset('User')->skip_validation(1)->create(...
263  
264 Turns off automatic validation on any creates / updates / etc going forward
265 in this chain if arg is true. You may still manually run validations in the
266 normal way as described in L<Valiant> (via ->validate for example).
267  
268 =head2 skip_validate
269  
270 =head2 do_validate
271  
272 Skip validations or reenable validations. This is just a wrapper on L</skip_validation>
273 which presets the enable or disable value.
274  
275 $schema->resultset('User')
276 ->skip_validate
277 ->create(\%user_args);
278  
279 =head2 build
280  
281 This is just a shortcut for "->new_result(+{})" and exists mostly to provide expected API
282 for L<Valiant::HTML::FormBuilder>.
283  
284 =head2 contains ($row)
285  
286 Returns true if the resultset contains a row with the same primary key values as the
287 given row (rows marked as removed are not considered). Used by the nested validation
288 code and handy in tests.
289  
290 =head1 SEE ALSO
291  
292 L<Valiant>, L<DBIx::Class>, L<DBIx::Class::Valiant>
293  
294 =head1 AUTHOR
295  
296 John Napiorkowski L<email:jjnapiork@cpan.org>
297  
298 =head1 COPYRIGHT & LICENSE
299  
300 See L<Valiant>.
301  
302 =cut
303  
« Valiant/Result/HTML/FormFields.pm ↑ Summary Valiant/Validator/SetSize.pm »