| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Mixin::ExtraFields::Driver::HashGuts 0.140004; | ||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use parent qw(Mixin::ExtraFields::Driver); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: store extras in a hashy object's guts | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||||||||||
| 10 | #pod package Your::HashBased::Class; | ||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||
| 12 | #pod use Mixin::ExtraFields -fields => { driver => 'HashGuts' }; | ||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||
| 14 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||
| 16 | #pod This driver class implements an extremely simple storage mechanism: extras are | ||||||||||||||||||||||||||||
| 17 | #pod stored on the object on which the mixed-in methods are called. By default, | ||||||||||||||||||||||||||||
| 18 | #pod they are stored under the key returned by the C<L</default_has_key>> method, | ||||||||||||||||||||||||||||
| 19 | #pod but this can be changed by providing a C<hash_key> argument to the driver | ||||||||||||||||||||||||||||
| 20 | #pod configuration, like so: | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod use Mixin::ExtraFields -fields => { | ||||||||||||||||||||||||||||
| 23 | #pod driver => { class => 'HashGuts', hash_key => "\0Something\0Wicked\0" } | ||||||||||||||||||||||||||||
| 24 | #pod }; | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | #pod | ||||||||||||||||||||||||||||
| 28 | #pod In addition to the methods required by Mixin::ExtraFields::Driver, the | ||||||||||||||||||||||||||||
| 29 | #pod following methods are provided: | ||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||
| 31 | #pod =head2 hash_key | ||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||
| 33 | #pod my $key = $driver->hash_key; | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod This method returns the key where the driver will store its extras. | ||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||
| 37 | #pod =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 47 | ▶ | sub hash_key { | ||||||||||||||||||||||||||
|
Subroutine
hash_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 47 | my ($self) = @_; | |||||||||||||||||||||||||||
| 41 | 47 | return $self->{hash_key}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #pod =head2 default_hash_key | ||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||
| 46 | #pod If no C<hash_key> argument is given for the driver, this method is called | ||||||||||||||||||||||||||||
| 47 | #pod during driver initialization. It will return a unique string to be used as the | ||||||||||||||||||||||||||||
| 48 | #pod hash key. | ||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||
| 50 | #pod =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | my $i = 0; | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub default_hash_key { | ||||||||||||||||||||||||||
|
Subroutine
default_hash_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | 2 | return "$self" . '@' . $i++; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | #pod =head2 storage | ||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||
| 60 | #pod This method returns the hashref of storage used for extras. Individual objects | ||||||||||||||||||||||||||||
| 61 | #pod get weak references to their id within this hashref. | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 42 | ▶ | sub storage { $_[0]->{storage} } | ||||||||||||||||||||||||||
|
Subroutine
storage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #pod =head2 storage_for | ||||||||||||||||||||||||||||
| 68 | #pod | ||||||||||||||||||||||||||||
| 69 | #pod my $stash = $driver->storage_for($object, $id); | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod This method returns the hashref to use to store extras for the given object and | ||||||||||||||||||||||||||||
| 72 | #pod id. This hashref is stored on both the hash-based object (in its C<hash_key> | ||||||||||||||||||||||||||||
| 73 | #pod entry) and on the driver (in the entry for C<$id> in its C<storage> hash). | ||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||
| 75 | #pod All objects with the same id should end up with the same hash in their | ||||||||||||||||||||||||||||
| 76 | #pod C<hash_key> field. B<None> of these references are weakened, which means two | ||||||||||||||||||||||||||||
| 77 | #pod things: first, even if all objects with a given id go out of scope, future | ||||||||||||||||||||||||||||
| 78 | #pod objects with that id will retain the original extras; secondly, memory used to | ||||||||||||||||||||||||||||
| 79 | #pod store extras is never reclaimed. If this is a problem, use a more | ||||||||||||||||||||||||||||
| 80 | #pod sophisticated driver. | ||||||||||||||||||||||||||||
| 81 | #pod | ||||||||||||||||||||||||||||
| 82 | #pod =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 42 | ▶ | sub storage_for { | ||||||||||||||||||||||||||
|
Subroutine
storage_for: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 42 | my ($self, $object, $id) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 42 | ▶ | my $store = $self->storage->{ $id } ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->storage->{$id} ||= {}
MC/DC100%1/1
$self->storage->{$id} ||= {}
$self->storage->{$id}
Truth table100%2/2
A
A= $self->storage->{$id}
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 42 | ▶ | unless ($object->{ $self->hash_key }||0 == $store) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$$object{$self->hash_key} or 0 == $store
MC/DC50%1/2
$$object{$self->hash_key} or 0 == $store
$$object{$self->hash_key}
0 == $store
Truth table66%2/3
A or B
A= $$object{$self->hash_key}B= 0 == $store
| |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | $object->{ $self->hash_key } ||= $store; | ||||||||||||||||||||||||||
|
Condition33%1/3
$$object{$self->hash_key} ||= $store
MC/DC0%0/2
$$object{$self->hash_key} ||= $store
$$object{$self->hash_key}
$store
Truth table33%1/3
A ||= B
A= $$object{$self->hash_key}B= $store
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 42 | return $store | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 5 | ▶ | sub from_args { | ||||||||||||||||||||||||||
|
Subroutine
from_args: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 5 | my ($class, $arg) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | my $self = bless { storage => {} } => $class; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | $self->{hash_key} = $arg->{hash_key} || $self->default_hash_key; | ||||||||||||||||||||||||||
|
Condition66%2/3
$arg->{'hash_key'} || $self->default_hash_key
MC/DC0%0/2
$arg->{'hash_key'} || $self->default_hash_key
$arg->{'hash_key'}
$self->default_hash_key
Truth table66%2/3
A || B
A= $arg->{'hash_key'}B= $self->default_hash_key
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 5 | return $self; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 20 | ▶ | sub exists_extra { | ||||||||||||||||||||||||||
|
Subroutine
exists_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 20 | my ($self, $object, $id, $name) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 20 | return exists $self->storage_for($object, $id)->{$name}; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 8 | ▶ | sub get_extra { | ||||||||||||||||||||||||||
|
Subroutine
get_extra: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 8 | my ($self, $object, $id, $name) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # avoid autovivifying entries on get. | ||||||||||||||||||||||||||||
| 116 | 8 | ▶ | return unless $self->exists_extra($object, $id, $name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 4 | return $self->storage_for($object, $id)->{$name}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | sub get_detailed_extra { | ||||||||||||||||||||||||||
|
Subroutine
get_detailed_extra: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 3 | my ($self, $object, $id, $name) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # avoid autovivifying entries on get. | ||||||||||||||||||||||||||||
| 124 | 3 | ▶ | return unless $self->exists_extra($object, $id, $name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 1 | return { value => $self->storage_for($object, $id)->{$name} }; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | ▶ | sub get_all_detailed_extra { | ||||||||||||||||||||||||||
|
Subroutine
get_all_detailed_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 7 | my ($self, $object, $id) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 7 | my $stash = $self->storage_for($object, $id); | |||||||||||||||||||||||||||
| 132 | 7 | my @all_detailed = map { $_ => { value => $stash->{$_} } } keys %$stash; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub get_all_extra { | ||||||||||||||||||||||||||
|
Subroutine
get_all_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 4 | my ($self, $object, $id) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4 | return %{ $self->storage_for($object, $id) }; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 4 | ▶ | sub set_extra { | ||||||||||||||||||||||||||
|
Subroutine
set_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 4 | my ($self, $object, $id, $name, $value) = @_; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 4 | $self->storage_for($object, $id)->{$name} = $value; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub delete_extra { | ||||||||||||||||||||||||||
|
Subroutine
delete_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 1 | my ($self, $object, $id, $name) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | delete $self->storage_for($object, $id)->{$name}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub delete_all_extra { | ||||||||||||||||||||||||||
|
Subroutine
delete_all_extra: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 1 | my ($self, $object, $id) = @_; | |||||||||||||||||||||||||||
| 155 | 1 | %{ $self->storage_for($object, $id) } = (); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | 1; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||