summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/TagContainer.pm
blob: f6e555131f35107eb7b7bd013e0d52dd970e702a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package Data::MultiValued::TagContainer; 
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw(HashRef);
use Data::MultiValued::Exceptions;
 
# ABSTRACT: container for tagged values 
 
=head1 DESCRIPTION
 
Please don't use this module directly, use L<Data::MultiValued::Tags>.
 
This module implements the storage for tagged data. It's almost
exactly a hash, the main difference being that C<undef> is a valid key
and it's distinct from the empty string.
 
Another difference is that you get an exception if you try to access a
tag that's not there.
 
Data is kept in "storage cells", as created by
L</_create_new_inferior> (by default, a hashref).
 
=head1 METHODS
 
=cut
 
has _storage => (
    is => 'rw',
    isa => HashRef,
    init_arg => undef,
    default => sub { { } },
    traits => ['Hash'],
    handles => {
        _has_tag => 'exists',
        _get_tag => 'get',
        _create_tag => 'set',
        _delete_tag => 'delete',
        all_tags => 'keys',
    },
);
 
has _default_tag => (
    is => 'rw',
    init_arg => undef,
    predicate => '_has_default_tag',
    clearer => '_clear_default_tag',
);
 
=head2 C<get>
 
  my $value = $obj->get({ tag => $the_tag });
 
Retrieves the "storage cell" for the given tag. Throws a
L<Data::MultiValued::Exceptions::TagNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::TagNotFound>
exception if the tag does not exists in this object.
 
Not passing in a C<tag> is equivalent to passing in C<< tag => undef
>>.
 
=cut
 
sub get {
    my ($self,$args) = @_;
 
    my $tag = $args->{tag};
 
    if (!defined($tag)) {
        if ($self->_has_default_tag) {
            return $self->_default_tag;
        }
 
        Data::MultiValued::Exceptions::TagNotFound->throw({
            value => $tag,
        });
    }
 
    if (!$self->_has_tag($tag)) {
        Data::MultiValued::Exceptions::TagNotFound->throw({
            value => $tag,
        });
    }
    return $self->_get_tag($tag);
}
 
=head2 C<get_or_create>
 
  $obj->get_or_create({ tag => $the_tag });
 
Retrieves the "storage cell" for the given tag. If the tag does not
exist, creates a new cell (see L</_create_new_inferior>), sets it for
the tag, and returns it.
 
Not passing in a C<tag> is equivalent to passing in C<< tag => undef
>>.
 
=cut
 
sub get_or_create {
    my ($self,$args) = @_;
 
    my $tag = $args->{tag};
 
    if (!defined($tag)) {
        if ($self->_has_default_tag) {
            return $self->_default_tag;
        }
        else {
            return $self->_default_tag(
                $self->_create_new_inferior
            );
        }
    }
 
    if (!$self->_has_tag($tag)) {
        $self->_create_tag($tag,$self->_create_new_inferior);
    }
    return $self->_get_tag($tag);
}
 
sub _clear_storage {
    my ($self) = @_;
 
    $self->_storage({});
}
 
=head2 C<clear>
 
  $obj->clear({ tag => $the_tag });
 
Deletes the given tag and all data associated with it. Does not throw
exceptions: if the tag does not exist, nothing happens.
 
Not passing in a C<tag>, or passing C<< tag => undef >>, clears
everything. If you want to only clear the C<undef> tag, you may call
C<_clear_default_tag> (which is considered a "protected" method).
 
=cut
 
sub clear {
    my ($self,$args) = @_;
 
    my $tag = $args->{tag};
 
    if (!defined($tag)) {
        $self->_clear_default_tag;
        $self->_clear_storage;
    }
    elsif ($self->_has_tag($tag)) {
        $self->_delete_tag($tag);
    }
    return;
}
 
=head2 C<all_tags>
 
  my @tags = $obj->all_tags;
 
Returns all the tags defined in this object. Does not return the
C<undef> tag.
 
=head2 C<_create_new_inferior>
 
Returns a new "storage cell", by default an empty hashref. See
L<Data::MultiValued::TagContainerForRanges> for an example of use.
 
=cut
 
sub _create_new_inferior {
    my ($self) = @_;
    return {};
}
 
1;