blib/lib/DBIx/Class/HashAccessor.pm

filter: Statementstmt 96.6% 116 / 120 Branchbran 56.0% 37 / 66 Conditioncond 46.6% 14 / 30 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 73.3% 182 / 248 CC 49 SCAR 18.8
CC
49
Cov
77%
__ANON__
9.2
__ANON__
9.1
__ANON__
8.8
CRAP 6.6
↑ Summary
1 package DBIx::Class::HashAccessor;
2 2 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: Helper functions to install accessors for serialized hash columns
4 2 $DBIx::Class::HashAccessor::VERSION = '0.001';
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub add_hash_accessor {
Subroutine add_hash_accessor: called CC=4 SCAR=14.4
Pod undocumented
12 1 my ( $class, $accessor, $hash ) = @_;
13 1 ▶ die 'require accessor and hash name' unless defined $accessor and defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accessor and defined $hash 0 1 1
Condition33%1/3
defined $accessor and defined $hash
!ll&&!rl&&r
001
MC/DC0%0/2
defined $accessor and defined $hash
defined $accessor defined $hash
Truth table33%1/3
A and B
A= defined $accessorB= defined $hash
ABresult
0X0
100
111
14 1 ▶ die 'accessor can\'t be named like hash' if $accessor eq $hash;
Branch: 1 branch
BranchTrueFalseTotal
if $accessor eq $hash 0 1 1
15 1 my $st = Package::Stash->new($class);
16  
17 5 ▶ $st->add_symbol('&'.$accessor,sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
18 5 my ( $self, @args ) = @_;
19 5 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 5 5
20 5 ▶ my %h = %{$self->$hash || {}};
Condition100%2/2
$self->$hash || {}
l!l
41
MC/DC100%1/1
$self->$hash || {}
$self->$hash
Truth table100%2/2
A
A= $self->$hash
Aresult
11
00
21 5 ▶ if (scalar @args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @args == 1) { } 4 1 5
elsif (scalar @args == 2) { } 1 0 1
22 4 return $h{$args[0]};
23 } elsif (scalar @args == 2) {
24 1 $h{$args[0]} = $args[1];
25 1 $self->$hash({ %h });
26 1 return $args[1];
27 } else {
28 0 die $accessor.' function must get 1 or 2 args';
29 }
30 1 });
31  
32 3 ▶ $st->add_symbol('&'.$accessor.'_hash',sub {
Subroutine __ANON__: called CC=8 SCAR=22.2
33 3 my ( $self, $key, @args ) = @_;
34 3 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 3 3
35 3 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
30
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
36 3 ▶ if (scalar @args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @args == 1) { } 2 1 3
elsif (scalar @args == 2) { } 1 0 1
37 2 ▶ return ref $h{$key} eq 'HASH' ? $h{$key}->{$args[0]} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $h{$key} eq 'HASH' ? : 2 0 2
38 } elsif (scalar @args == 2) {
39 1 ▶ $h{$key} = {} unless exists $h{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h{$key} 1 0 1
40 1 ▶ return undef unless ref $h{$key} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h{$key} eq "HASH" 0 1 1
41 1 $h{$key}->{$args[0]} = $args[1];
42 1 $self->$hash({ %h });
43 1 return $args[1];
44 } else {
45 0 die $accessor.'_hash function must get 2 or 3 args';
46 }
47 1 });
48  
49 1 ▶ $st->add_symbol('&'.$accessor.'_hash_delete',sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
50 1 my ( $self, $key, $hash_key ) = @_;
51 1 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 1 1
52 1 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
10
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
53 1 ▶ if ($key && $hash_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and $hash_key) { } 1 0 1
Condition33%1/3
$key and $hash_key
!ll&&!rl&&r
001
MC/DC0%0/2
$key and $hash_key
$key $hash_key
Truth table33%1/3
A and B
A= $keyB= $hash_key
ABresult
0X0
100
111
54 1 ▶ return undef unless ref $h{$key} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h{$key} eq "HASH" 0 1 1
55 1 my $old_value = delete $h{$key}->{$hash_key};
56 1 $self->$hash({ %h });
57 1 return $old_value;
58 } else {
59 0 die $accessor.'_hash_delete function must get 2 args';
60 }
61 1 });
62  
63 1 ▶ $st->add_symbol('&'.$accessor.'_push',sub {
Subroutine __ANON__: called CC=5 SCAR=17.0
64 1 my ( $self, $key, @elements ) = @_;
65 1 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 1 1
66 1 ▶ die $accessor.'_push function requires 1 arg' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 1 1
67 1 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
10
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
68 0 ▶ my @array = defined $h{$key} ? ( @{$h{$key}} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $h{$key} ? : 0 1 1
69 1 push @array, @elements;
70 1 $h{$key} = [ @array ];
71 1 $self->$hash({ %h });
72 1 return @elements;
73 1 });
74  
75 1 ▶ $st->add_symbol('&'.$accessor.'_shift',sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
76 1 my ( $self, $key ) = @_;
77 1 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 1 1
78 1 ▶ die $accessor.'_shift function requires 1 arg' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 1 1
79 1 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
10
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
80 1 ▶ my @array = defined $h{$key} ? ( @{$h{$key}} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $h{$key} ? : 1 0 1
81 1 ▶ return unless scalar @array;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @array 0 1 1
82 1 my $return = shift @array;
83 1 $h{$key} = [ @array ];
84 1 $self->$hash({ %h });
85 1 return $return;
86 1 });
87  
88 2 ▶ $st->add_symbol('&'.$accessor.'_in',sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
89 2 my ( $self, $key, $val ) = @_;
90 2 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 2 2
91 2 ▶ die $accessor.'_in function requires 2 args' unless defined $val && defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and defined $key 0 2 2
Condition33%1/3
defined $val and defined $key
!ll&&!rl&&r
002
MC/DC0%0/2
defined $val and defined $key
defined $val defined $key
Truth table33%1/3
A and B
A= defined $valB= defined $key
ABresult
0X0
100
111
92 2 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
20
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
93 2 ▶ my @array = defined $h{$key} ? ( @{$h{$key}} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $h{$key} ? : 2 0 2
94 2 for (@array) {
95 3 ▶ return 1 if $val eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $_ 1 2 3
96 }
97 1 return 0;
98 1 });
99  
100 1 ▶ $st->add_symbol('&'.$accessor.'_in_delete',sub {
Subroutine __ANON__: called CC=8 SCAR=21.7
101 1 my ( $self, $key, $val ) = @_;
102 1 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 1 1
103 1 ▶ die $accessor.'_in_delete function requires 2 args' unless defined $val && defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and defined $key 0 1 1
Condition33%1/3
defined $val and defined $key
!ll&&!rl&&r
001
MC/DC0%0/2
defined $val and defined $key
defined $val defined $key
Truth table33%1/3
A and B
A= defined $valB= defined $key
ABresult
0X0
100
111
104 1 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
10
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
105 1 ▶ my @array = defined $h{$key} ? ( @{$h{$key}} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $h{$key} ? : 1 0 1
106 1 my @new_array;
107 1 for my $old_val (@array) {
108 2 ▶ push @new_array, $old_val unless $val eq $old_val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq $old_val 1 1 2
109 }
110 1 $h{$key} = [ @new_array ];
111 1 $self->$hash({ %h });
112 1 return;
113 1 });
114  
115 1 ▶ $st->add_symbol('&'.$accessor.'_delete',sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
116 1 my ( $self, $key ) = @_;
117 1 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 1 1
118 1 ▶ die $accessor.'_delete function requires 1 arg' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 1 1
119 1 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
10
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
120 1 my $return = delete $h{$key};
121 1 $self->$hash({ %h });
122 1 return $return;
123 1 });
124  
125 4 ▶ $st->add_symbol('&'.$accessor.'_exists',sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
126 4 my ( $self, $key ) = @_;
127 4 ▶ die((ref $self).' does not support '.$hash) unless $self->can($hash);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($hash) 0 4 4
128 4 ▶ die $accessor.'_exists function requires 1 arg' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
129 4 ▶ my %h = %{$self->$hash || {}};
Condition50%1/2
$self->$hash || {}
l!l
40
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
Aresult
11
00
130 4 return exists $h{$key};
131 1 });
132  
133 }
134  
135 2 1;
136  
137 __END__
↑ Summary