| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$prefix eq "EO::" or $prefix eq "EO"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 41 | 0 | $hash = { @_ }; | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 2 | $hash = shift; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if (!$hash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | throw EO::Error::InvalidParameters | |||||||||||||||||||||||||||
| 47 | text => 'no hash provided'; | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | if (!ref($hash)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | throw EO::Error::InvalidParameters | |||||||||||||||||||||||||||
| 51 | text => 'not a reference'; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if (ref($hash) && ref($hash) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $hash and ref $hash ne "HASH"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 0 | throw EO::Error::InvalidParameters | |||||||||||||||||||||||||||
| 67 | text => 'no key specified for at'; | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 19 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$code and ref $code eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$code and ref $code eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$pair and $pair->isa("EO::Pair")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||