lib/Reaction/InterfaceModel/Action/DBIC/Role/CheckUniques.pm

filter: Statementstmt 87.7% 50 / 57 Branchbran 45.0% 9 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 74.4% 67 / 90 CC 14 SCAR 24.2
CC
14
Cov
74%
check_all_uniques
17.6
__ANON__
2.1
__ANON__
2.0
CRAP 11.2
« lib/Reaction/UI/ViewPort/Field/.ToDo/TimeRange.pm ↑ Summary lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm »
1 package Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques;
2  
3 5 ▶use Reaction::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use namespace::clean -except => [ qw(meta) ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 # requires qw(target_model
9 # parameter_hashref
10 # parameter_attributes
11 # );
12  
13 has _unique_constraint_results =>
14 (
15 isa => 'HashRef',
16 is => 'rw',
17 required => 1,
18 9 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 5 metaclass => 'Reaction::Meta::Attribute'
20 );
21 2 ▶sub check_all_uniques {
Subroutine check_all_uniques: called CC=12 SCAR=28.7
Pod documented
22 2 my ($self) = @_;
23 2 my $source = $self->target_model->result_source;
24 2 my %uniques = $source->unique_constraints;
25 2 ▶ my $proto = ($self->target_model->isa('DBIx::Class::ResultSet')
Branch: 1 branch
BranchTrueFalseTotal
$self->target_model->isa('DBIx::Class::ResultSet') ? : 0 2 2
26 ? $self->target_model->new_result({})
27 : $self->target_model);
28 2 my $param_hr = $self->parameter_hashref;
29 my %proto_hash = (
30 map {
31 2 my @ret;
32 8 my $attr = $proto->meta->get_attribute($_->name);
33 8 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 8 0 8
34 8 my $reader = $attr->get_read_method;
35 8 ▶ if ($reader) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader) 8 0 8
36 8 my $value = $proto->$reader;
37 8 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 7 1 8
38 7 @ret = ($_->name => $value);
39 }
40 }
41 }
42 8 @ret;
43 } $self->parameter_attributes
44 );
45 my %merged = (
46 %proto_hash,
47 (map {
48 2 ▶ (defined $param_hr->{$_} ? ($_ => $param_hr->{$_}) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $param_hr->{$_} ? : 8 0 8
49 } keys %$param_hr),
50 );
51 2 my %ident = %{$proto->ident_condition};
52 2 my %clashes;
53 2 my $rs = $source->resultset;
54 2 foreach my $unique (keys %uniques) {
55 2 my %pass;
56 2 my @attrs = @{$uniques{$unique}};
57 2 ▶ next if grep { !exists $merged{$_} } @attrs;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $merged{$_};} @attrs 2 0 2
58 # skip PK before insertion if auto-inc etc. etc.
59 0 @pass{@attrs} = @merged{@attrs};
60 0 ▶ if (my $obj = $rs->find(\%pass, { key => $unique })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $rs->find(\%pass, {"key", $unique})) 0 0 0
61 0 my $found_ident = $obj->ident_condition;
62 #warn join(', ', %$found_ident, %ident);
63 0 ▶ if (!$proto->in_storage
Branch: 1 branch
BranchTrueFalseTotal
if (not $proto->in_storage or grep {$found_ident->{$_} ne $ident{$_};} keys %ident) 0 0 0
Condition0%0/3
not $proto->in_storage or grep {$found_ident->{$_} ne $ident{$_};} keys %ident
l!l&&r!l&&!r
000
MC/DC0%0/2
not $proto->in_storage or grep {$found_ident->{$_} ne $ident{$_};} keys %ident
not $proto->in_storage grep {$found_ident->{$_} ne $ident{$_};} keys %ident
Truth table0%0/3
not(A) or B
A= not $proto->in_storageB= grep {$found_ident->{$_} ne $ident{$_};} keys %ident
ABresult
1X1
011
000
64 0 || (grep { $found_ident->{$_} ne $ident{$_} } keys %ident)) {
65 # if in storage and no ident conditions are different the found
66 # obj is *us* :)
67 0 $clashes{$_} = 1 for @attrs;
68 }
69 }
70 }
71 2 $self->_unique_constraint_results(\%clashes);
72 };
73  
74 2 ▶after sync_all => sub { shift->check_all_uniques; };
Subroutine __ANON__: called CC=1 SCAR=0.0
75  
76 8 ▶around error_for_attribute => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
77 8 my $orig = shift;
78 8 my ($self, $attr) = @_;
79 8 ▶ if ($self->_unique_constraint_results->{$attr->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_unique_constraint_results->{$attr->name}) 0 8 8
80 0 return "Already taken, please try an alternative";
81 }
82 8 return $orig->(@_);
83 5 };
84  
85 2 ▶around can_apply => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
86 2 my $orig = shift;
87 2 my ($self) = @_;
88 2 ▶ return 0 if keys %{$self->_unique_constraint_results};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->_unique_constraint_results;} 0 2 2
89 2 return $orig->(@_);
90 5 };
91  
92  
93  
94 5 1;
95  
96 =head1 NAME
97  
98 Reaction::InterfaceModel::Action::DBIC::Role::CheckUniques
99  
100 =head1 DESCRIPTION
101  
102 =head2 check_all_uniques
103  
104 =head2 error_for_attribute
105  
106 =head2 meta
107  
108 =head1 AUTHORS
109  
110 See L<Reaction::Class> for authors.
111  
112 =head1 LICENSE
113  
114 See L<Reaction::Class> for the license.
115  
116 =cut
« lib/Reaction/UI/ViewPort/Field/.ToDo/TimeRange.pm ↑ Summary lib/Reaction/UI/ViewPort/Field/Role/Mutable.pm »