summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/#Tags.pm#
blob: 29cf102eb58807f832d8b28793e65351aa384125 (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
package Data::MultiValued::Tags;
use Moose;
use MooseX::Params::Validate;
use Moose::Util::TypeConstraints;
use MooseX::Types::Moose qw(Num Str Undef Any);
use Data::MultiValued::Exceptions;
use Data::MultiValued::TagContainer;
 
# ABSTRACT: Handle values with tags
 
=head1 SYNOPSIS
 
  use Data::MultiValued::Tags;
 
  my $obj = Data::MultiValued::Tags->new();
  $obj->set({
    tag => 'tag1',
    value => 'a string',
  });
  say $obj->get({tag=>'tag1'}); # prints 'a string'
  say $obj->get({tag=>'tag2'}); # dies
 
=head1 METHODS
 
=cut
 
has _storage => (
    is => 'rw',
    isa => class_type('Data::MultiValued::TagContainer'),
    init_arg => undef,
    lazy_build => 1,
);
 
sub _build__storage {
    Data::MultiValued::TagContainer->new();
}
 
sub _rebless_storage {
    my ($self) = @_;
 
    bless $self->{_storage},'Data::MultiValued::TagContainer';
}
 
sub _as_hash {
    my ($self) = @_;
 
    my %ret = %{$self->_storage};
    return {_storage=>\%ret};
}
 
=head2 C<set>
 
  $obj->set({ tag => $the_tag, value => $the_value });
 
Stores the given value for the given tag. Replaces existing
values. Does not throw exceptions.
 
No cloning is done: if you pass in a reference, the reference is
just stored.
 
=cut
 
sub set {
    my ($self,%args) = validated_hash(
        \@_,
        tag => { isa => Str, optional => 1, },
        value => { isa => Any, },
    );
 
    $self->_storage->get_or_create(\%args)
         ->{value} = $args{value};
}
 
=head2 C<get>
 
  my $value = $obj->get({ tag => $the_tag });
 
Retrieves the value for the given tag. Throws a
L<Data::MultiValued::Exceptions::TagNotFound> exception if the tag
does not exists in this object.
 
No cloning is done: if a reference was stored, you get it back
untouched.
 
=cut
 
sub get {
    my ($self,%args) = validated_hash(
        \@_,
        tag => { isa => Str, optional => 1, },
    );
 
    $self->_storage->get(\%args)
         ->{value};
}
 
=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.
 
=cut
 
sub clear {
    my ($self,%args) = validated_hash(
        \@_,
        tag => { isa => Str, optional => 1, },
    );
 
    $self->_storage->clear(\%args);
}
 
=head1 SEE ALSO
 
L<Data::MultiValued::TagContainer>, L<Data::MultiValued::Exceptions>
 
=cut
 
1;