summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/TagsAndRanges.pm
blob: 43bff6b63da37b4e704739d66ea23ab772370538 (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
package Data::MultiValued::TagsAndRanges; 
use Moose;
use namespace::autoclean;
use MooseX::Params::Validate;
use Moose::Util::TypeConstraints;
use Data::MultiValued::Exceptions;
use Data::MultiValued::TagContainerForRanges;
 
# ABSTRACT: Handle values with tags and validity ranges 
 
=head1 SYNOPSIS
 
  use Data::MultiValued::TagsAndRanges;
 
  my $obj = Data::MultiValued::TagsAndRanges->new();
  $obj->set({
    tag => 'tag1',
    from => 10,
    to => 20,
    value => 'foo',
  });
  say $obj->get({tag => 'tag1', at => 15}); # prints 'foo'
  say $obj->get({tag => 'tag1', at => 35}); # dies
  say $obj->get({tag => 'tag2', at => 15}); # dies
 
=cut
 
has _storage => (
    is => 'rw',
    isa => class_type('Data::MultiValued::TagContainerForRanges'),
    init_arg => undef,
    lazy_build => 1,
);
 
sub _build__storage {
    Data::MultiValued::TagContainerForRanges->new();
}
 
=method C<set>
 
  $obj->set({ tag => $the_tag, from => $min, to => $max, value => $the_value });
 
Stores the given value for the given tag and range. Does not throw
exceptions.
 
See L<Data::MultiValued::Tags/set> and
L<Data::MultiValued::Ranges/set> for more details.
 
=cut
 
sub set {
    my ($self,%args) = validated_hash(
        \@_,
        from => { isa => 'Num|Undef'optional => 1, },
        to => { isa => 'Num|Undef'optional => 1, },
        tag => { isa => 'Str'optional => 1, },
        value => { isa => 'Any', },
    );
 
    $self->_storage->get_or_create(\%args)
         ->get_or_create(\%args)
         ->{value} = $args{value};
}
 
=method C<get>
 
  my $value = $obj->get({ tag => $the_tag, at => $point });
 
Retrieves the value for the given tag and point. Throws a
L<Data::MultiValued::Exceptions::RangeNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::RangeNotFound>
exception if no ranges exist in this object that include the point,
and
L<Data::MultiValued::Exceptions::TagNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::TagNotFound>
exception if the tag does not exists in this object.
 
See L<Data::MultiValued::Tags/get> and
L<Data::MultiValued::Ranges/get> for more details.
 
=cut
 
sub get {
    my ($self,%args) = validated_hash(
        \@_,
        at => { isa => 'Num|Undef'optional => 1, },
        tag => { isa => 'Str'optional => 1, },
    );
 
    $self->_storage->get(\%args)
         ->get(\%args)
         ->{value};
}
 
=method C<clear>
 
  $obj->clear({ tag => $the_tag, from => $min, to => $max });
 
If a range is specified, deletes all values for the given range and
tag. If no range is specified, delete all values for the given tag.
 
Does not throw exceptions.
 
See L<Data::MultiValued::Tags/clear> and
L<Data::MultiValued::Ranges/clear> for more details.
 
=cut
 
sub clear {
    my ($self,%args) = validated_hash(
        \@_,
        from => { isa => 'Num|Undef'optional => 1, },
        to => { isa => 'Num|Undef'optional => 1, },
        tag => { isa => 'Str'optional => 1, },
    );
 
    if (exists $args{from} || exists $args{to}) {
        $self->_storage->get(\%args)
            ->clear(\%args);
    }
    else {
        $self->_storage->clear(\%args);
    }
}
 
=head1 Serialisation helpers
 
These are used through
L<Data::MultiValued::UglySerializationHelperRole>.
 
=head2 C<_rebless_storage>
 
Blesses the storage into L<Data::MultiValued::TagContainerForRanges>,
then calls C<_rebless_storage> on it.
 
=cut
 
sub _rebless_storage {
    my ($self) = @_;
 
    bless $self->{_storage},'Data::MultiValued::TagContainerForRanges';
    $self->_storage->_rebless_storage;
}
 
=head2 C<_as_hash>
 
Returns the internal representation with no blessed hashes, with as
few copies as possible. Depends on
L<Data::MultiValued::TagContainerForRanges/_as_hash>.
 
=cut
 
sub _as_hash {
    my ($self) = @_;
 
    my $ret = $self->_storage->_as_hash;
    return {_storage=>$ret};
}
 
__PACKAGE__->meta->make_immutable();
 
1;