| 1 | package DBIO::HashAccessor; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Accessor methods for serialized hash columns | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use parent 'DBIO::Row'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 18 | ▶ | sub _install { | ||||||||||||||||||||||||||
|
Subroutine
_install: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 18 | my ($class, $name, $code) = @_; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 18 | *{"${class}::${name}"} = $code; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub add_hash_accessor { | ||||||||||||||||||||||||||
|
Subroutine
add_hash_accessor: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 4 | my ( $class, $accessor, $hash ) = @_; | |||||||||||||||||||||||||||
| 20 | 4 | ▶ | die 'require accessor and hash name' unless defined $accessor and defined $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $accessor and defined $hash
MC/DC50%1/2
defined $accessor and defined $hash
defined $accessor
defined $hash
Truth table66%2/3
A and B
A= defined $accessorB= defined $hash
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | die 'accessor can\'t be named like hash' if $accessor eq $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | ▶ | $class->_install($accessor, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 24 | 9 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 25 | 9 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | if (scalar @args == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 28 | 4 | return $h{$args[0]}; | |||||||||||||||||||||||||||
| 29 | } elsif (scalar @args == 2) { | ||||||||||||||||||||||||||||
| 30 | 3 | $h{$args[0]} = $args[1]; | |||||||||||||||||||||||||||
| 31 | 3 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 32 | 3 | return $args[1]; | |||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||
| 34 | 2 | die $accessor.' function must get 1 or 2 args'; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 2 | }); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $class->_install($accessor.'_hash', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.7
| |||||||||||||||||||||||||||||
| 39 | 5 | my ( $self, $key, @args ) = @_; | |||||||||||||||||||||||||||
| 40 | 5 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if (scalar @args == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | return ref $h{$key} eq 'HASH' ? $h{$key}->{$args[0]} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } elsif (scalar @args == 2) { | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | $h{$key} = {} unless exists $h{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | return undef unless ref $h{$key} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | $h{$key}->{$args[0]} = $args[1]; | |||||||||||||||||||||||||||
| 48 | 1 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 49 | 1 | return $args[1]; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | die $accessor.'_hash function must get 2 or 3 args'; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 2 | }); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | $class->_install($accessor.'_hash_delete', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 56 | 1 | my ( $self, $key, $hash_key ) = @_; | |||||||||||||||||||||||||||
| 57 | 1 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if ($key && $hash_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key and $hash_key
MC/DC0%0/2
$key and $hash_key
$key
$hash_key
Truth table33%1/3
A and B
A= $keyB= $hash_key
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | return undef unless ref $h{$key} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | my $old_value = delete $h{$key}->{$hash_key}; | |||||||||||||||||||||||||||
| 62 | 1 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 63 | 1 | return $old_value; | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 0 | die $accessor.'_hash_delete function must get 2 args'; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 2 | }); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | $class->_install($accessor.'_push', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 70 | 3 | my ( $self, $key, @elements ) = @_; | |||||||||||||||||||||||||||
| 71 | 3 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | die $accessor.'_push function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | push @array, @elements; | |||||||||||||||||||||||||||
| 76 | 3 | $h{$key} = [ @array ]; | |||||||||||||||||||||||||||
| 77 | 3 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 78 | 3 | return @elements; | |||||||||||||||||||||||||||
| 79 | 2 | }); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | $class->_install($accessor.'_shift', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 82 | 2 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 83 | 2 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | die $accessor.'_shift function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | return unless scalar @array; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | my $return = shift @array; | |||||||||||||||||||||||||||
| 89 | 1 | $h{$key} = [ @array ]; | |||||||||||||||||||||||||||
| 90 | 1 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 91 | 1 | return $return; | |||||||||||||||||||||||||||
| 92 | 2 | }); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | ▶ | $class->_install($accessor.'_in', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=22.1
| |||||||||||||||||||||||||||||
| 95 | 3 | my ( $self, $key, $val ) = @_; | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | die $accessor.'_in function requires 2 args' unless defined $val && defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $val and defined $key
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
| |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 99 | 3 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 3 | for (@array) { | |||||||||||||||||||||||||||
| 101 | 7 | ▶ | return 1 if $val eq $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 2 | return 0; | |||||||||||||||||||||||||||
| 104 | 2 | }); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | $class->_install($accessor.'_in_delete', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=21.7
| |||||||||||||||||||||||||||||
| 107 | 1 | my ( $self, $key, $val ) = @_; | |||||||||||||||||||||||||||
| 108 | 1 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | die $accessor.'_in_delete function requires 2 args' unless defined $val && defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $val and defined $key
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
| |||||||||||||||||||||||||||||
| 110 | 1 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | my @new_array; | |||||||||||||||||||||||||||
| 113 | 1 | for my $old_val (@array) { | |||||||||||||||||||||||||||
| 114 | 3 | ▶ | push @new_array, $old_val unless $val eq $old_val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 1 | $h{$key} = [ @new_array ]; | |||||||||||||||||||||||||||
| 117 | 1 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 118 | 1 | return; | |||||||||||||||||||||||||||
| 119 | 2 | }); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | $class->_install($accessor.'_delete', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 122 | 2 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 123 | 2 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | die $accessor.'_delete function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 126 | 2 | my $return = delete $h{$key}; | |||||||||||||||||||||||||||
| 127 | 2 | $self->$hash({ %h }); | |||||||||||||||||||||||||||
| 128 | 2 | return $return; | |||||||||||||||||||||||||||
| 129 | 2 | }); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 3 | ▶ | $class->_install($accessor.'_exists', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 132 | 3 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 133 | 3 | ▶ | die((ref $self).' does not support '.$hash) unless $self->can($hash); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 3 | ▶ | die $accessor.'_exists function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 3 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$hash || {}
MC/DC0%0/1
$self->$hash || {}
$self->$hash
Truth table50%1/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 136 | 3 | return exists $h{$key}; | |||||||||||||||||||||||||||
| 137 | 2 | }); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||