| 1 | package EntityModel::Hash; | ||||||||||||||||||||||||||||
| 2 | 5 | $EntityModel::Hash::VERSION = '0.016'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use EntityModel::Log ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | EntityModel::Hash - wrapper object for dealing with hashrefs | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.016 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Primarily intended as an abstract interface for use with L<EntityModel> backend storage. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 METHODS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use overload | ||||||||||||||||||||||||||||
| 25 | 5 | ▶ | '%{}' => sub { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 27 | 1 | return $self->hashref; | |||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | 5 | fallback => 1; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head2 new | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Instantiates with the given hashref. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 3 | my ($class, $data) = @_; | |||||||||||||||||||||||||||
| 39 | 3 | ▶ | bless { data => ($data // { }) }, $class; | ||||||||||||||||||||||||||
|
Condition100%2/2
$data // {}
MC/DC100%1/1
$data // {}
$data
Truth table100%2/2
A
A= $data
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 count | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Returns the number of items in the hashref (i.e. keys). | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | 4 | return scalar keys %{$self->hashref}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 list | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Returns all values from the hashref. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | return unless $self->hashref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | return values %{$self->hashref}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head2 set | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Sets an entry (identified by key) to the given value. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 5 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 5 | my ($k, $v) = @_; | |||||||||||||||||||||||||||
| 74 | 5 | ▶ | unless(defined $k) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | # logStack("No k?"); | ||||||||||||||||||||||||||||
| 76 | 0 | return $self; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 5 | ▶ | if(ref($k) && ref($k) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $k and ref $k eq "HASH"
MC/DC50%1/2
ref $k and ref $k eq "HASH"
ref $k
ref $k eq "HASH"
Truth table66%2/3
A and B
A= ref $kB= ref $k eq "HASH"
| |||||||||||||||||||||||||||||
| 79 | 1 | $self->hashref->{$_} = $k->{$_} foreach keys %$k; | |||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 4 | $self->hashref->{$k} = $v; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 5 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 erase | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Deletes the given key from the hashref. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub erase { | ||||||||||||||||||||||||||
|
Subroutine
erase: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | 1 | my ($k) = @_; | |||||||||||||||||||||||||||
| 95 | 1 | ▶ | $k //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$k //= ""
MC/DC0%0/1
$k //= ""
$k
Truth table50%1/2
A
A= $k
| |||||||||||||||||||||||||||||
| 96 | 1 | delete $self->hashref->{$k}; | |||||||||||||||||||||||||||
| 97 | 1 | return $self; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 get | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Retrieves the value for the given key. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 7 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 7 | my ($self, $k) = @_; | |||||||||||||||||||||||||||
| 108 | 7 | ▶ | $k //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$k //= ""
MC/DC0%0/1
$k //= ""
$k
Truth table50%1/2
A
A= $k
| |||||||||||||||||||||||||||||
| 109 | 7 | return $self->hashref->{$k}; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 hashref | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Returns the contained hashref. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 26 | ▶ | sub hashref { | ||||||||||||||||||||||||||
|
Subroutine
hashref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 26 | my $class = ref $self; | |||||||||||||||||||||||||||
| 121 | 26 | bless $self, 'overload::dummy'; | |||||||||||||||||||||||||||
| 122 | 26 | my $out = $self->{data}; | |||||||||||||||||||||||||||
| 123 | 26 | bless $self, $class; | |||||||||||||||||||||||||||
| 124 | 26 | return $out; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 exists | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Returns true if the given key exists in the hashref. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | sub exists : method { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 2 | my ($self, $k) = @_; | |||||||||||||||||||||||||||
| 135 | 2 | ▶ | $k //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$k //= ""
MC/DC0%0/1
$k //= ""
$k
Truth table50%1/2
A
A= $k
| |||||||||||||||||||||||||||||
| 136 | 2 | return exists($self->hashref->{$k}); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 keys | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Returns a list of all keys. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | ▶ | sub keys : method { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | 1 | return keys %{$self->hashref}; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 clear | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Clears the hashref. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | sub clear : method { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 158 | 1 | my $class = ref $self; | |||||||||||||||||||||||||||
| 159 | 1 | bless $self, 'overload::dummy'; | |||||||||||||||||||||||||||
| 160 | 1 | $self->{data} = { }; | |||||||||||||||||||||||||||
| 161 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 162 | 1 | return $self; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 is_empty | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Returns true if there's nothing in the hashref. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub is_empty { | ||||||||||||||||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 0 | return !$self->keys; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | __END__ | ||||||||||||||||||||||||||||