EO/Hash.pm

filter: Statementstmt 90.4% 104 / 115 Branchbran 70.0% 21 / 30 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 95.4% 21 / 22 Podpod 85.7% 12 / 14 totaltotal 82.0% 169 / 206 CC 23 SCAR 11.8
CC
23
Cov
83%
new_with_hash
7.2
select
5.3
do
4.3
CRAP 3.3
« EO/WeakArray.pm ↑ Summary EO/Singleton.pm »
1  
2 package EO::Hash;
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use EO::Pair;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use EO::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use EO::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = 0.96;
12 5 our @ISA = qw( EO::Collection );
13  
14 5 ▶use overload '%{}' => 'get_reference',
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 'fallback' => 1;
16  
17 7 ▶sub init {
Subroutine init: called CC=2 SCAR=7.2
Pod undocumented
18 7 my $self = CORE::shift;
19 7 my $elems = CORE::shift;
20 7 ▶ if ($self->SUPER::init( @_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->SUPER::init(@_)) 7 0 7
21 7 $self->element( {} );
22 7 return 1;
23 }
24 0 return 0;
25 }
26  
27 59 ▶sub get_reference {
Subroutine get_reference: called CC=3 SCAR=11.0
Pod undocumented
28 59 my $self = shift;
29 59 my $callpkg = caller();
30 59 my $prefix = substr($callpkg,0,4);
31 59 ▶ if ($prefix eq 'EO::' || $prefix eq 'EO') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix eq "EO::" or $prefix eq "EO") 57 2 59
Condition100%3/3
$prefix eq "EO::" or $prefix eq "EO"
l!l&&r!l&&!r
43142
MC/DC100%2/2
$prefix eq "EO::" or $prefix eq "EO"
$prefix eq "EO::" $prefix eq "EO"
Truth table100%3/3
A or B
A= $prefix eq "EO::"B= $prefix eq "EO"
ABresult
1X1
011
000
32 57 return $self;
33 }
34 2 return $self->element;
35 }
36  
37 2 ▶sub new_with_hash {
Subroutine new_with_hash: called CC=6 SCAR=19.7
Pod documented
38 2 my $class = shift;
39 2 my $hash;
40 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 2 2
41 0 $hash = { @_ };
42 } else {
43 2 $hash = shift;
44 }
45 2 ▶ if (!$hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash) 1 1 2
46 1 throw EO::Error::InvalidParameters
47 text => 'no hash provided';
48 }
49 1 ▶ if (!ref($hash)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash) 0 1 1
50 0 throw EO::Error::InvalidParameters
51 text => 'not a reference';
52 }
53 1 ▶ if (ref($hash) && ref($hash) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash and ref $hash ne "HASH") 0 1 1
Condition33%1/3
ref $hash and ref $hash ne "HASH"
!ll&&!rl&&r
010
MC/DC0%0/2
ref $hash and ref $hash ne "HASH"
ref $hash ref $hash ne "HASH"
Truth table33%1/3
A and B
A= ref $hashB= ref $hash ne "HASH"
ABresult
0X0
100
111
54 0 throw EO::Error::InvalidParameters
55 text => 'not a hash reference';
56 }
57 1 my $self = $class->new();
58 1 $self->element( $hash );
59 1 return $self;
60 }
61  
62 19 ▶sub at {
Subroutine at: called CC=3 SCAR=11.1
Pod documented
63 19 my $self = shift;
64 19 my $key = shift;
65 19 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 19 19
66 0 throw EO::Error::InvalidParameters
67 text => 'no key specified for at';
68 }
69 19 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 9 19
70 10 $self->element->{ $key } = shift;
71 10 return $self;
72 }
73 9 return $self->element->{ $key };
74 }
75  
76 7 ▶sub pair_for {
Subroutine pair_for: called CC=1 SCAR=0.0
Pod documented
77 7 my $self = shift;
78 7 my $key = shift;
79 7 return EO::Pair->new
80 ->key( $key )
81 ->value( $self->at( $key ) );
82 }
83  
84 1 ▶sub do {
Subroutine do: called CC=4 SCAR=14.6
Pod documented
85 1 my $self = shift;
86 1 my $code = shift;
87 1 ▶ unless( $code and ref($code) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code and ref $code eq "CODE") 0 1 1
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
88 0 throw EO::Error::InvalidParameters
89 text => 'must have a code reference as a parameter';
90 }
91 1 my $hash = ref($self)->new();
92 1 foreach my $key (keys %{ $self->element }) {
93 3 my $pair = $self->pair_for( $key );
94 3 $hash->add( $pair->do( $code ) );
95 }
96 1 return $hash;
97 }
98  
99 1 ▶sub select {
Subroutine select: called CC=5 SCAR=16.6
Pod documented
100 1 my $self = shift;
101 1 my $code = shift;
102 1 ▶ unless( $code and ref($code) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code and ref $code eq "CODE") 0 1 1
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
103 0 throw EO::Error::InvalidParameters
104 text => 'must have a code reference as a parameter';
105 }
106 1 my $hash = ref($self)->new();
107 1 foreach my $key (keys %{ $self->element }) {
108 3 my $pair = $self->pair_for( $key );
109 3 ▶ if ( $pair->do( $code ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair->do($code)) 1 2 3
110 1 $hash->add( $pair );
111 }
112 }
113 1 return $hash;
114 }
115  
116 6 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
117 6 my $self = shift;
118 6 my $pair = shift;
119 6 ▶ if (!$pair or !$pair->isa('EO::Pair')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pair and $pair->isa("EO::Pair")) 1 5 6
Condition66%2/3
$pair and $pair->isa("EO::Pair")
!ll&&!rl&&r
105
MC/DC50%1/2
$pair and $pair->isa("EO::Pair")
$pair $pair->isa("EO::Pair")
Truth table66%2/3
A and B
A= $pairB= $pair->isa("EO::Pair")
ABresult
0X0
100
111
120 1 throw EO::Error::InvalidParameters
121 text => 'argument to add must be a pair';
122 }
123 5 $self->at( $pair->key, $pair->value );
124 }
125  
126 0 ▶sub object_at_index : Deprecated {
Subroutine object_at_index: not called CC=1 SCAR=6.9
127 0 my $self = shift;
128 0 $self->at( @_ );
129 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 3 ▶sub delete {
Subroutine delete: called CC=2 SCAR=6.9
Pod documented
132 3 my $self = shift;
133 3 my $key = shift;
134 3 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 1 2 3
135 1 throw EO::Error::InvalidParameters text => 'no key specified for delete';
136 }
137 2 delete $self->element->{ $key };
138 }
139  
140 5 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
141 5 my $self = shift;
142 5 $self->keys->count;
143 }
144  
145 8 ▶sub iterator {
Subroutine iterator: called CC=1 SCAR=0.0
Pod documented
146 8 my $self = shift;
147 8 my %iter = %{$self->element };
148 8 return %iter;
149 }
150  
151 7 ▶sub keys {
Subroutine keys: called CC=2 SCAR=7.2
Pod documented
152 7 my $self = shift;
153 7 my %hash = $self->iterator;
154 7 ▶ if (!wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 7 0 7
155 7 my $array = EO::Array->new()->push( keys %hash );
156 7 return $array;
157 } else {
158 0 return keys %hash;
159 }
160 }
161  
162 1 ▶sub values {
Subroutine values: called CC=2 SCAR=7.2
Pod documented
163 1 my $self = shift;
164 1 my %hash = $self->iterator;
165 1 ▶ if (!wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 1 0 1
166 1 my $array = EO::Array->new()->push( values %hash );
167 1 return $array;
168 } else {
169 0 return values %hash;
170 }
171 }
172  
173 2 ▶sub has {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
174 2 my $self = shift;
175 2 my $key = shift;
176 2 exists $self->element->{ $key }
177 }
178  
179 5 1;
180  
181 __END__
« EO/WeakArray.pm ↑ Summary EO/Singleton.pm »