summaryrefslogtreecommitdiff
path: root/lib/Data/MultiValued/Ranges.pm
blob: 1168960c19c24e69a7ee1b143f98de1f0a03494d (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package Data::MultiValued::Ranges; 
{
  $Data::MultiValued::Ranges::VERSION = '0.0.3';
}
{
  $Data::MultiValued::Ranges::DIST = 'Data-MultiValued';
}
use Moose;
use namespace::autoclean;
use Moose::Util::TypeConstraints;
use MooseX::Params::Validate;
use Data::MultiValued::Exceptions;
use Data::MultiValued::RangeContainer;
 
# ABSTRACT: Handle values with validity ranges 
 
 
has _storage => (
    is => 'rw',
    isa => class_type('Data::MultiValued::RangeContainer'),
    init_arg => undef,
    lazy_build => 1,
);
 
sub _build__storage {
    Data::MultiValued::RangeContainer->new();
}
 
 
sub set {
    my ($self,%args) = validated_hash(
        \@_,
        from => { isa => 'Num|Undef'optional => 1, },
        to => { isa => 'Num|Undef'optional => 1, },
        value => { isa => 'Any', },
    );
 
    $self->_storage->get_or_create(\%args)
         ->{value} = $args{value};
}
 
 
sub get {
    my ($self,%args) = validated_hash(
        \@_,
        at => { isa => 'Num|Undef'optional => 1, },
    );
 
    $self->_storage->get(\%args)
         ->{value};
}
 
 
sub clear {
    my ($self,%args) = validated_hash(
        \@_,
        from => { isa => 'Num|Undef'optional => 1, },
        to => { isa => 'Num|Undef'optional => 1, },
    );
 
    $self->_storage->clear(\%args);
}
 
 
sub _rebless_storage {
    my ($self) = @_;
 
    bless $self->{_storage},'Data::MultiValued::RangeContainer';
}
 
 
 
sub _as_hash {
    my ($self) = @_;
 
    my %ret = %{$self->_storage};
    return {_storage=>\%ret};
}
 
 
__PACKAGE__->meta->make_immutable();
 
1;
 
__END__
=pod
 
=encoding utf-8
 
=head1 NAME
 
Data::MultiValued::Ranges - Handle values with validity ranges
 
=head1 VERSION
 
version 0.0.3
 
=head1 SYNOPSIS
 
  use Data::MultiValued::Ranges;
 
  my $obj = Data::MultiValued::Ranges->new();
  $obj->set({
    from => 10,
    to => 20,
    value => 'foo',
  });
  say $obj->get({at => 15}); # prints 'foo'
  say $obj->get({at => 35}); # dies
 
=head1 METHODS
 
=head2 C<set>
 
  $obj->set({ from => $min, to => $max, value => $the_value });
 
Stores the given value for the given range. Throws
L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
if C<< $min > $max >>.
 
The range is defined as C<< Num $x : $min <= $x < $max >>. A C<< from
=> undef >> means "from -Inf", and a C<< to => undef >> means "to
+Inf". Not passing in C<from> or C<to> is equivalent to passing
C<undef>.
 
If the given range intersects existing ranges, these are spliced to
avoid overlaps. In other words:
 
  $obj->set({
    from => 10,
    to => 20,
    value => 'foo',
  });
  $obj->set({
    from => 15,
    to => 25,
    value => 'bar',
  });
  say $obj->get({at => 12}); # prints 'foo'
  say $obj->get({at => 15}); # prints 'bar'
  say $obj->get({at => 25}); # dies
 
No cloning is done: if you pass in a reference, the reference is
just stored.
 
=head2 C<get>
 
  my $value = $obj->get({ at => $point });
 
Retrieves the value for the given 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
(remember that a range does not include its C<to> point).
 
A C<< at => undef >> means "at -Inf". Not passing in C<at> is
equivalent to passing C<undef>.
 
No cloning is done: if a reference was stored, you get it back
untouched.
 
=head2 C<clear>
 
  $obj->clear({ from => $min, to => $max });
 
Deletes all values for the given range. Throws
L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
if C<< $min > $max >>.
 
A C<< from => undef >> means "from -Inf", and a C<< to => undef >>
means "to +Inf". Not passing in C<from> or C<to> is equivalent to
passing C<undef>. Thus, C<< $obj->clear() >> clears everything.
 
If the given range intersects existing ranges, these are spliced. In
other words:
 
  $obj->set({
    from => 10,
    to => 20,
    value => 'foo',
  });
  $obj->clear({
    from => 15,
    to => 25,
  });
  say $obj->get({at => 12}); # prints 'foo'
  say $obj->get({at => 15}); # dies
 
=head1 Serialisation helpers
 
These are used through
L<Data::MultiValued::UglySerializationHelperRole>.
 
=head2 C<_rebless_storage>
 
Blesses the storage into L<Data::MultiValued::RangeContainer>.
 
=head2 C<_as_hash>
 
Returns the internal representation with no blessed hashes, with as
few copies as possible.
 
=head1 SEE ALSO
 
L<Data::MultiValued::RangeContainer>, L<Data::MultiValued::Exceptions>
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2011 by Net-a-Porter.com.
 
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
 
=cut