DBIx/Class/Valiant/ResultSet.pm

filter: Statementstmt 78.2% 126 / 161 Branchbran 65.2% 47 / 72 Conditioncond 52.1% 12 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 73.9% 17 / 23 Podpod 40.0% 4 / 10 totaltotal 69.6% 211 / 303 CC 52 SCAR 24.6
CC
52
Cov
72%
set_recursively
24.8
contains
20.0
_dump_row
13.2
CRAP 11.7
« Valiant/HTML/Util/TagBuilder.pm ↑ Summary Valiant/Error.pm »
1 package DBIx::Class::Valiant::ResultSet;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use namespace::autoclean -also => ['debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use DBIx::Class::Valiant::Util::Exception::TooManyRows;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 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 sub namespace {
19 0 ▶ my $self = shift;
Subroutine namespace: not called CC=2 SCAR=17.9
Pod undocumented
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 sub i18n_lookup {
28 0 ▶ my ($class_or_self, $arg) = @_;
Subroutine i18n_lookup: not called CC=3 SCAR=20.0
Pod undocumented
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 9 ▶ 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 sub skip_validation {
37 160 ▶ my ($self, $arg) = @_;
Subroutine skip_validation: called CC=3 SCAR=11.0
Pod documented
38 160 ▶ if(defined($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 159 160
39 1 ▶ $self->{_skip_validation} = $arg ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 1 0 1
40 }
41 160 return $self->{_skip_validation};
42 }
43
44 sub skip_validate {
45 1 ▶ my ($self) = @_;
Subroutine skip_validate: called CC=1 SCAR=0.0
Pod documented
46 1 $self->skip_validation(1);
47 1 return $self;
48 }
49 sub do_validate {
50 0 ▶ my ($self) = @_;
Subroutine do_validate: not called CC=1 SCAR=6.9
Pod documented
51 0 $self->skip_validation(0);
52 0 return $self;
53 }
54  
55 sub _nested_info_for_related {
56 47 ▶ my ($self, $related) = @_;
Subroutine _nested_info_for_related: called CC=2 SCAR=7.0
57 47 my %nested = $self->result_class->accept_nested_for;
58 47 ▶ my %info = %{ $nested{$related}||+{} };
Branch: 1 branch
BranchTrueFalseTotal
$nested{$related} or {} 0 47 47
59 47 return %info;
60 }
61  
62 sub _related_limit {
63 47 ▶ my ($self, $related) = @_;
Subroutine _related_limit: called CC=4 SCAR=14.0
64 47 my %info = $self->_nested_info_for_related($related);
65 47 ▶ if(my $limit_proto = $info{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit_proto = $info{"limit"}) 6 41 47
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 41 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 sub new_result {
76 159 ▶ my ($self, $fields, @args) = @_;
Subroutine new_result: called CC=12 SCAR=25.0
Pod undocumented
77 159 my $context = delete $fields->{__context};
78  
79 159 my %related = ();
80 159 my %nested = $self->result_class->accept_nested_for;
81
82 159 foreach my $associated (keys %nested) {
83 $related{$associated} = delete($fields->{$associated})
84 125 ▶ if exists($fields->{$associated});
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields->{$associated} 50 75 125
85 }
86  
87 # Remove any relationed keys we didn't find with the allows nested
88 159 my @rel_names = $self->result_source->relationships();
89 159 ▶ my @m2m_names = keys %{ $self->result_class->_m2m_metadata ||+{} };
Branch: 1 branch
BranchTrueFalseTotal
$self->result_class->_m2m_metadata or {} 0 159 159
90  
91 159 my %found = map { $_ => delete($fields->{$_}) } @rel_names, @m2m_names;
92  
93 159 ▶ if(grep { defined $_ } values %found) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_;} values %found) 0 159 159
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 159 my $result = $self->next::method($fields, @args);
99 159 ▶ $result->{__VALIANT_CREATE_ARGS}{context} = $context if $context; # Need this for ->insert
Branch: 1 branch
BranchTrueFalseTotal
if $context 12 147 159
100 159 ▶ $result->skip_validation(1) if $self->skip_validation;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_validation 1 158 159
101  
102 159 debug 2, "made new_result @{[ $result ]}";
103 159 RELATED: foreach my $related(keys %related) {
104  
105 48 ▶ if(my $cb = $nested{$related}->{reject_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $nested{$related}{"reject_if"}) 3 45 48
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 47 my ($has_limit, $limit) = $self->_related_limit($related);
111 47 ▶ if($has_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_limit) 6 41 47
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 46 $result->set_related_from_params($related, $related{$related});
123 }
124  
125 156 return $result;
126 }
127  
128 # Utility methods
129  
130 # this should cache results betters
131 sub contains {
132 0 ▶ my ($self, $row) = @_;
Subroutine contains: not called CC=4 SCAR=30.0
Pod undocumented
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 sub set_recursively {
150 2 ▶ my ($self, $data, $opts) = @_;
Subroutine set_recursively: called CC=15 SCAR=32.1
Pod undocumented
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 sub _dump_resultset {
183 2 ▶ my ($resultset, $seen) = @_;
Subroutine _dump_resultset: called CC=2 SCAR=6.9
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 sub _dump_row {
199 16 ▶ my ($row, $seen) = @_;
Subroutine _dump_row: called CC=13 SCAR=25.8
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 9 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 <example> directory in the distribution for a more complete example
248 setup and application.
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 =head1 AUTHOR
285
286 John Napiorkowski L<email:jjnapiork@cpan.org>
287
288 =head1 SEE ALSO
289
290 L<Valiant>, L<DBIx::Class>, L<DBIx::Class::Valiant>
291  
292 =head1 AUTHOR
293  
294 See L<Valiant>.
295  
296 =head1 COPYRIGHT & LICENSE
297  
298 See L<Valiant>.
299  
300 =cut
301  
« Valiant/HTML/Util/TagBuilder.pm ↑ Summary Valiant/Error.pm »