File Coverage

blib/lib/Data/MultiValued/Ranges.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod 3 3 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Data::MultiValued::Ranges;
2             {
3             $Data::MultiValued::Ranges::VERSION = '0.0.1_4';
4             }
5             {
6             $Data::MultiValued::Ranges::DIST = 'Data-MultiValued';
7             }
8 6     6   692463 use Moose;
  6         2562734  
  6         100  
9 6     6   57748 use namespace::autoclean;
  6         7162  
  6         48  
10 6     6   5924 use MooseX::Params::Validate;
  6         73192  
  6         62  
11 6     6   3488 use Moose::Util::TypeConstraints;
  6         14  
  6         69  
12 6     6   27022 use MooseX::Types::Moose qw(Num Str Undef Any);
  6         403702  
  6         77  
13 6     6   43387 use Data::MultiValued::Exceptions;
  6         131  
  6         269  
14 6     6   4941 use Data::MultiValued::RangeContainer;
  6         26  
  6         3300  
15              
16             # ABSTRACT: Handle values with validity ranges
17              
18              
19             has _storage => (
20             is => 'rw',
21             isa => class_type('Data::MultiValued::RangeContainer'),
22             init_arg => undef,
23             lazy_build => 1,
24             );
25              
26             sub _build__storage {
27 11     11   795 Data::MultiValued::RangeContainer->new();
28             }
29              
30              
31             sub set {
32 18     18 1 4016 my ($self,%args) = validated_hash(
33             \@_,
34             from => { isa => Num|Undef, optional => 1, },
35             to => { isa => Num|Undef, optional => 1, },
36             value => { isa => Any, },
37             );
38              
39 18         136813 $self->_storage->get_or_create(\%args)
40             ->{value} = $args{value};
41             }
42              
43              
44             sub get {
45 62     62 1 57589 my ($self,%args) = validated_hash(
46             \@_,
47             at => { isa => Num|Undef, optional => 1, },
48             );
49              
50 62         269981 $self->_storage->get(\%args)
51             ->{value};
52             }
53              
54              
55             sub clear {
56 3     3 1 60 my ($self,%args) = validated_hash(
57             \@_,
58             from => { isa => Num|Undef, optional => 1, },
59             to => { isa => Num|Undef, optional => 1, },
60             );
61              
62 3         28081 $self->_storage->clear(\%args);
63             }
64              
65              
66             sub _rebless_storage {
67 1     1   3 my ($self) = @_;
68              
69 1         10 bless $self->{_storage},'Data::MultiValued::RangeContainer';
70             }
71              
72              
73              
74             sub _as_hash {
75 1     1   3 my ($self) = @_;
76              
77 1         3 my %ret = %{$self->_storage};
  1         44  
78 1         6 return {_storage=>\%ret};
79             }
80              
81              
82             __PACKAGE__->meta->make_immutable();
83              
84             1;
85              
86             __END__
87             =pod
88              
89             =encoding utf-8
90              
91             =head1 NAME
92              
93             Data::MultiValued::Ranges - Handle values with validity ranges
94              
95             =head1 VERSION
96              
97             version 0.0.1_4
98              
99             =head1 SYNOPSIS
100              
101             use Data::MultiValued::Ranges;
102              
103             my $obj = Data::MultiValued::Ranges->new();
104             $obj->set({
105             from => 10,
106             to => 20,
107             value => 'foo',
108             });
109             say $obj->get({at => 15}); # prints 'foo'
110             say $obj->get({at => 35}); # dies
111              
112             =head1 METHODS
113              
114             =head2 C<set>
115              
116             $obj->set({ from => $min, to => $max, value => $the_value });
117              
118             Stores the given value for the given range. Throws
119             L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
120             if C<< $min > $max >>.
121              
122             The range is defined as C<< Num $x : $min <= $x < $max >>. A C<< from
123             => undef >> means "from -Inf", and a C<< to => undef >> means "to
124             +Inf". Not passing in C<from> or C<to> is equivalent to passing
125             C<undef>.
126              
127             If the given range intersects existing ranges, these are spliced to
128             avoid overlaps. In other words:
129              
130             $obj->set({
131             from => 10,
132             to => 20,
133             value => 'foo',
134             });
135             $obj->set({
136             from => 15,
137             to => 25,
138             value => 'bar',
139             });
140             say $obj->get({at => 12}); # prints 'foo'
141             say $obj->get({at => 15}); # prints 'bar'
142             say $obj->get({at => 25}); # dies
143              
144             No cloning is done: if you pass in a reference, the reference is
145             just stored.
146              
147             =head2 C<get>
148              
149             my $value = $obj->get({ at => $point });
150              
151             Retrieves the value for the given point. Throws a
152             L<Data::MultiValued::Exceptions::RangeNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::RangeNotFound>
153             exception if no ranges exist in this object that include the point
154             (remember that a range does not include its C<to> point).
155              
156             A C<< at => undef >> means "at -Inf". Not passing in C<at> is
157             equivalent to passing C<undef>.
158              
159             No cloning is done: if a reference was stored, you get it back
160             untouched.
161              
162             =head2 C<clear>
163              
164             $obj->clear({ from => $min, to => $max });
165              
166             Deletes all values for the given range. Throws
167             L<Data::MultiValued::Exceptions::BadRange|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::BadRange>
168             if C<< $min > $max >>.
169              
170             A C<< from => undef >> means "from -Inf", and a C<< to => undef >>
171             means "to +Inf". Not passing in C<from> or C<to> is equivalent to
172             passing C<undef>. Thus, C<< $obj->clear() >> clears everything.
173              
174             If the given range intersects existing ranges, these are spliced. In
175             other words:
176              
177             $obj->set({
178             from => 10,
179             to => 20,
180             value => 'foo',
181             });
182             $obj->clear({
183             from => 15,
184             to => 25,
185             });
186             say $obj->get({at => 12}); # prints 'foo'
187             say $obj->get({at => 15}); # dies
188              
189             =head1 Serialisation helpers
190              
191             These are used through
192             L<Data::MultiValued::UglySerializationHelperRole>.
193              
194             =head2 C<_rebless_storage>
195              
196             Blesses the storage into L<Data::MultiValued::RangeContainer>.
197              
198             =head2 C<_as_hash>
199              
200             Returns the internal representation with no blessed hashes, with as
201             few copies as possible.
202              
203             =head1 SEE ALSO
204              
205             L<Data::MultiValued::RangeContainer>, L<Data::MultiValued::Exceptions>
206              
207             =head1 AUTHOR
208              
209             Gianni Ceccarelli <dakkar@thenautilus.net>
210              
211             =head1 COPYRIGHT AND LICENSE
212              
213             This software is copyright (c) 2011 by Net-a-Porter.com.
214              
215             This is free software; you can redistribute it and/or modify it under
216             the same terms as the Perl 5 programming language system itself.
217              
218             =cut
219