DBIx/Class/Valiant/ResultSet.pm

filter: Statementstmt 80.4% 66 / 82 Branchbran 71.8% 23 / 32 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.5% 11 / 14 Podpod 66.6% 4 / 6 totaltotal 75.0% 105 / 140 CC 22 SCAR 20.7
CC
22
Cov
76%
contains
20.0
new_result
12.1
build
6.0
CRAP 7.9
« Valiant/Util.pm ↑ Summary Valiant/Validator/Length.pm »
1 package DBIx::Class::Valiant::ResultSet;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use namespace::autoclean -also => ['debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use DBIx::Class::Valiant::Util::Exception::TooManyRows;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub skip_validation {
11 142 ▶ my ($self, $arg) = @_;
Subroutine skip_validation: called CC=3 SCAR=11.0
Pod documented
12 142 ▶ if(defined($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 141 142
13 1 ▶ $self->{_skip_validation} = $arg ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$arg ? : 1 0 1
14 }
15 142 return $self->{_skip_validation};
16 }
17
18 sub skip_validate {
19 1 ▶ my ($self) = @_;
Subroutine skip_validate: called CC=1 SCAR=0.0
Pod documented
20 1 $self->skip_validation(1);
21 1 return $self;
22 }
23 sub do_validate {
24 0 ▶ my ($self) = @_;
Subroutine do_validate: not called CC=1 SCAR=6.9
Pod documented
25 0 $self->skip_validation(0);
26 0 return $self;
27 }
28  
29 sub _nested_info_for_related {
30 48 ▶ my ($self, $related) = @_;
Subroutine _nested_info_for_related: called CC=2 SCAR=7.0
31 48 my %nested = $self->result_class->accept_nested_for;
32 48 ▶ my %info = %{ $nested{$related}||+{} };
Branch: 1 branch
BranchTrueFalseTotal
$nested{$related} or {} 0 48 48
33 48 return %info;
34 }
35  
36 sub _related_limit {
37 48 ▶ my ($self, $related) = @_;
Subroutine _related_limit: called CC=4 SCAR=14.0
38 48 my %info = $self->_nested_info_for_related($related);
39 48 ▶ if(my $limit_proto = $info{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit_proto = $info{"limit"}) 6 42 48
40 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
41 6 return 1, $limit;
42 }
43 42 return 0, undef;
44 }
45  
46 # This is just an alias so that we can decouple the Valiant API from DBIC
47 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
48  
49 sub new_result {
50 141 ▶ my ($self, $fields, @args) = @_;
Subroutine new_result: called CC=12 SCAR=25.0
Pod undocumented
51 141 my $context = delete $fields->{__context};
52  
53 141 my %related = ();
54 141 my %nested = $self->result_class->accept_nested_for;
55
56 141 foreach my $associated (keys %nested) {
57 $related{$associated} = delete($fields->{$associated})
58 108 ▶ if exists($fields->{$associated});
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields->{$associated} 49 59 108
59 }
60  
61 # Remove any relationed keys we didn't find with the allows nested
62 141 my @rel_names = $self->result_source->relationships();
63 141 ▶ my @m2m_names = keys %{ $self->result_class->_m2m_metadata ||+{} };
Branch: 1 branch
BranchTrueFalseTotal
$self->result_class->_m2m_metadata or {} 0 141 141
64  
65 141 my %found = map { $_ => delete($fields->{$_}) } @rel_names, @m2m_names;
66  
67 141 ▶ if(grep { defined $_ } values %found) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_;} values %found) 0 141 141
68 0 my $related = join(', ', grep { $found{$_} } keys %found);
69 0 die "You are trying to create a relationship ($related) without setting 'accept_nested_for'";
70 }
71  
72 141 my $result = $self->next::method($fields, @args);
73 141 ▶ $result->{__VALIANT_CREATE_ARGS}{context} = $context if $context; # Need this for ->insert
Branch: 1 branch
BranchTrueFalseTotal
if $context 12 129 141
74 141 ▶ $result->skip_validation(1) if $self->skip_validation;
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_validation 1 140 141
75  
76 141 debug 2, "made new_result @{[ $result ]}";
77 141 RELATED: foreach my $related(keys %related) {
78  
79 49 ▶ if(my $cb = $nested{$related}->{reject_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $nested{$related}{"reject_if"}) 3 46 49
80 3 my $response = $cb->($result, $related{$related});
81 3 ▶ next RELATED if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 1 2 3
82 }
83  
84 48 my ($has_limit, $limit) = $self->_related_limit($related);
85 48 ▶ if($has_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_limit) 6 42 48
86 6 my $num = scalar @{$related{$related}};
87 6 ▶ DBIx::Class::Valiant::Util::Exception::TooManyRows
Branch: 1 branch
BranchTrueFalseTotal
if $num > $limit 1 5 6
88 ->throw(
89 limit=>$limit,
90 attempted=>$num,
91 related=>$related,
92 me=>$self->result_source->name,
93 ) if $num > $limit;
94 }
95  
96 47 $result->set_related_from_params($related, $related{$related});
97 }
98  
99 138 return $result;
100 }
101  
102 # Utility methods
103  
104 # this should cache results betters
105 sub contains {
106 0 ▶ my ($self, $row) = @_;
Subroutine contains: not called CC=4 SCAR=30.0
Pod undocumented
107 0 my %pk = map { $_ => $row->$_ }
108 $self->result_source->primary_columns;
109 0 foreach my $item ($self->all) {
110 0 ▶ next if $item->is_removed;
Branch: 1 branch
BranchTrueFalseTotal
if $item->is_removed 0 0 0
111 my @matches = grep {
112 0 $item->get_column($_) eq $pk{$_}
113 } keys %pk;
114 0 ▶ return 1 if scalar(@matches) == keys %pk;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matches == keys %pk 0 0 0
115 }
116 0 return 0;
117 }
118  
119 7 1;
120  
121 =head1 NAME
122  
123 DBIx::Class::Valiant::ResultSet - Validation support for resultsets
124  
125 =head1 SYNOPSIS
126  
127 package Example::Schema::ResultSet::Person;
128  
129 use base 'DBIx::Class::ResultSet';
130  
131 __PACKAGE__->load_components('Valiant::ResultSet');
132  
133 See <example> directory in the distribution for a more complete example
134 setup and application.
135  
136 =head1 DESCRIPTION
137  
138 A component that needs to be used on any result classes for which you want to add
139 L<Valiant> validations on. Its best to add this to your base and default resultset
140 classes if you plan to use L<DBIx::Class::Valiant> across all your result classes.
141  
142 =head1 METHODS
143  
144 This component adds the following methods to your resultset classes.
145  
146 =head2 skip_validation (1|0)
147  
148 $schema->resultset('User')->skip_validation(1)->create(...
149  
150 Turns off automatic validation on any creates / updates / etc going forward
151 in this chain if arg is true. You may still manually run validations in the
152 normal way as described in L<Valiant> (via ->validate for example).
153  
154 =head2 skip_validate
155  
156 =head2 do_validate
157  
158 Skip validations or reenable validations. This is just a wrapper on L</skip_validation>
159 which presets the enable or disable value.
160  
161 $schema->resultset('User')
162 ->skip_validate
163 ->create(\%user_args);
164  
165 =head2 build
166  
167 This is just a shortcut for "->new_result(+{})" and exists mostly to provide expected API
168 for L<Valiant::HTML::FormBuilder>.
169  
170 =head1 AUTHOR
171
172 John Napiorkowski L<email:jjnapiork@cpan.org>
173
174 =head1 SEE ALSO
175
176 L<Valiant>, L<DBIx::Class>, L<DBIx::Class::Valiant>
177  
178 =head1 AUTHOR
179  
180 See L<Valiant>.
181  
182 =head1 COPYRIGHT & LICENSE
183  
184 See L<Valiant>.
185  
186 =cut
187  
« Valiant/Util.pm ↑ Summary Valiant/Validator/Length.pm »