| 1 | # Data::Hopen::Scope::Hash - a hash-based nested key-value store based | ||||||||||
| 2 | package Data::Hopen::Scope::Hash; | ||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 18 | ▶ | use Data::Hopen::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 18 | our $VERSION = '0.000018'; | |||||||||
| 7 | |||||||||||
| 8 | 18 | ▶ | use Data::Hopen::Scope qw(:default :internal); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 18 | ▶ | use parent 'Data::Hopen::Scope'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | use Class::Tiny { | ||||||||||
| 11 | 155 | ▶ | _content => sub { +{} }, # Our storage | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 18 | ▶ | }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 18 | ▶ | use Data::Hopen qw(getparameters); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | #use Data::Hopen::Util::Data qw(clone); | ||||||||||
| 16 | 18 | ▶ | use Set::Scalar; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | #use Sub::ScopeFinalizer qw(scope_finalizer); | ||||||||||
| 18 | |||||||||||
| 19 | # Docs {{{1 | ||||||||||
| 20 | |||||||||||
| 21 | =head1 NAME | ||||||||||
| 22 | |||||||||||
| 23 | Data::Hopen::Scope::Hash - a hash-based nested key-value store | ||||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | This class implements L<Data::Hopen::Scope> using a single hash table as the | ||||||||||
| 28 | storage. It only supports one set of data (L<Data::Hopen::Scope/$set>), | ||||||||||
| 29 | which is named C<0>. | ||||||||||
| 30 | |||||||||||
| 31 | =head1 ATTRIBUTES | ||||||||||
| 32 | |||||||||||
| 33 | =head2 outer | ||||||||||
| 34 | |||||||||||
| 35 | The fallback C<Scope> for looking up names not found in this C<Scope>. | ||||||||||
| 36 | If non is provided, it is C<undef>, and no fallback will happen. | ||||||||||
| 37 | |||||||||||
| 38 | =head2 name | ||||||||||
| 39 | |||||||||||
| 40 | Not used, but provided so you can use L<Data::Hopen/hnew> to make Scopes. | ||||||||||
| 41 | |||||||||||
| 42 | =head1 METHODS | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | # }}}1 | ||||||||||
| 47 | |||||||||||
| 48 | =head2 put | ||||||||||
| 49 | |||||||||||
| 50 | Add key-value pairs to this scope. See L<Data::Hopen::Scope/put>. In this | ||||||||||
| 51 | particular implementation, the last-added value for a particular key wins. | ||||||||||
| 52 | |||||||||||
| 53 | TODO add $set option once it's added to D::H::Scope::put(). | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | sub put { | ||||||||||
| 58 | 49 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||
|
Subroutine
put: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 59 | 48 | ▶ | croak "Got an odd number of parameters" if @_%2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 47 | ▶ | return $self unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 46 | my %new = @_; | |||||||||
| 62 | 46 | @{$self->_content}{keys %new} = values %new; | |||||||||
| 63 | 46 | return $self; | |||||||||
| 64 | } #add() | ||||||||||
| 65 | |||||||||||
| 66 | =head2 merge | ||||||||||
| 67 | |||||||||||
| 68 | Merge in values. See L<Data::Hopen::Scope/merge>. | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | sub merge { | ||||||||||
| 73 | 45 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||
|
Subroutine
merge: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 74 | 44 | ▶ | croak "Got an odd number of parameters" if @_%2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 43 | ▶ | return unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 39 | my %new = @_; | |||||||||
| 78 | 39 | my $merger = $self->_merger; | |||||||||
| 79 | 39 | $self->_content($merger->merge($self->_content, \%new)); | |||||||||
| 80 | |||||||||||
| 81 | 39 | return $self; | |||||||||
| 82 | } #merge() | ||||||||||
| 83 | |||||||||||
| 84 | =head2 adopt_hash | ||||||||||
| 85 | |||||||||||
| 86 | Takes over the given hash to be the new contents of the Scope::Hash. | ||||||||||
| 87 | Usage example: | ||||||||||
| 88 | |||||||||||
| 89 | $scope->adopt_hash({ foo => 42 }); | ||||||||||
| 90 | |||||||||||
| 91 | The scope uses exactly the hash passed, not a clone of it. If this is not | ||||||||||
| 92 | applicable to a subclass, that subclass should override it as C<...> or an | ||||||||||
| 93 | express C<die>. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | sub adopt_hash { | ||||||||||
| 98 | 5 | ▶ | my $self = shift or croak 'Need an instance'; | ||||||||
|
Subroutine
adopt_hash: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 99 | 4 | ▶ | my $hrNew = shift or croak 'Need a hash to adopt'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 3 | ▶ | croak 'Cannot adopt a non-hash' unless ref $hrNew eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 1 | $self->_content($hrNew); | |||||||||
| 102 | 1 | return $self; | |||||||||
| 103 | } #adopt_hash() | ||||||||||
| 104 | |||||||||||
| 105 | =head2 _names_here | ||||||||||
| 106 | |||||||||||
| 107 | Populates a L<Set::Scalar> with the names of the items stored in this Scope, | ||||||||||
| 108 | but B<not> any outer Scope. Called as: | ||||||||||
| 109 | |||||||||||
| 110 | $scope->_names_here($retval[, $set]); | ||||||||||
| 111 | |||||||||||
| 112 | No return value. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | sub _names_here { | ||||||||||
| 117 | 520 | ▶ | my ($self, %args) = getparameters('self', [qw(retval ; set)], @_); | ||||||||
|
Subroutine
_names_here: called CC=2 SCAR=7.0
| |||||||||||
| 118 | 520 | ▶ | _set0 $args{set} or croak 'I only support set 0'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 520 | $args{retval}->insert(keys %{$self->_content}); | |||||||||
| 120 | } #_names_here() | ||||||||||
| 121 | |||||||||||
| 122 | =head2 _find_here | ||||||||||
| 123 | |||||||||||
| 124 | Looks for a given item in this scope, but B<not> any outer scope. Called as: | ||||||||||
| 125 | |||||||||||
| 126 | $scope->_find_here($name[, $set]) | ||||||||||
| 127 | |||||||||||
| 128 | Returns the value, or C<undef> if not found. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | |||||||||||
| 132 | sub _find_here { | ||||||||||
| 133 | 532 | ▶ | my ($self, %args) = getparameters('self', [qw(name ; set)], @_); | ||||||||
|
Subroutine
_find_here: called CC=4 SCAR=13.9
| |||||||||||
| 134 | 532 | ▶ | _set0 $args{set} or croak 'I only support set 0'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | |||||||||||
| 136 | 532 | my $val = $self->_content->{$args{name}}; | |||||||||
| 137 | 532 | ▶ | return undef unless defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 328 | ▶ | return ($args{set} eq '*') ? { 0 => $val } : $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | } #_find_here() | ||||||||||
| 140 | |||||||||||
| 141 | 18 | 1; | |||||||||
| 142 | __END__ | ||||||||||