| 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
Condition33%1/3
defined $accessor and defined $hash
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
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | die 'accessor can\'t be named like hash' if $accessor eq $hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | my %h = %{$self->$hash || {}}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->$hash || {}
MC/DC100%1/1
$self->$hash || {}
$self->$hash
Truth table100%2/2
A
A= $self->$hash
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | if (scalar @args == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 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
| |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | if (scalar @args == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | return ref $h{$key} eq 'HASH' ? $h{$key}->{$args[0]} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } elsif (scalar @args == 2) { | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | $h{$key} = {} unless exists $h{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | return undef unless ref $h{$key} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 52 | 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
| |||||||||||||||||||||||||||||
| 53 | 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
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | return undef unless ref $h{$key} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | die $accessor.'_push function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 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
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | die $accessor.'_shift function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 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
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | return unless scalar @array; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | 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
| |||||||||||||||||||||||||||||
| 92 | 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
| |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | for (@array) { | |||||||||||||||||||||||||||
| 95 | 3 | ▶ | return 1 if $val eq $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 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
| |||||||||||||||||||||||||||||
| 104 | 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
| |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my @array = defined $h{$key} ? ( @{$h{$key}} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | die $accessor.'_delete function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 128 | 4 | ▶ | die $accessor.'_exists function requires 1 arg' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | 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
| |||||||||||||||||||||||||||||
| 130 | 4 | return exists $h{$key}; | |||||||||||||||||||||||||||
| 131 | 1 | }); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||