diff options
Diffstat (limited to 'lib/Data/MultiValued')
-rw-r--r-- | lib/Data/MultiValued/#Tags.pm# | 121 | ||||
-rw-r--r-- | lib/Data/MultiValued/Ranges.pm | 17 | ||||
-rw-r--r-- | lib/Data/MultiValued/Tags.pm | 11 | ||||
-rw-r--r-- | lib/Data/MultiValued/TagsAndRanges.pm | 56 |
4 files changed, 82 insertions, 123 deletions
diff --git a/lib/Data/MultiValued/#Tags.pm# b/lib/Data/MultiValued/#Tags.pm# deleted file mode 100644 index 29cf102..0000000 --- a/lib/Data/MultiValued/#Tags.pm# +++ /dev/null @@ -1,121 +0,0 @@ -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; diff --git a/lib/Data/MultiValued/Ranges.pm b/lib/Data/MultiValued/Ranges.pm index ef55b4c..296ae76 100644 --- a/lib/Data/MultiValued/Ranges.pm +++ b/lib/Data/MultiValued/Ranges.pm @@ -74,6 +74,7 @@ sub clear { $self->_storage->clear(\%args); } + 1; __END__ @@ -108,7 +109,10 @@ version 0.0.1 Stores the given value for the given range. Does not throw exceptions. -The range is defined as C<< Num $x : $min <= $x < $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: @@ -139,6 +143,9 @@ L<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. @@ -148,6 +155,10 @@ untouched. Deletes all values for the given range. Does not throw exceptions. +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: @@ -163,6 +174,10 @@ other words: say $obj->get({at => 12}); # prints 'foo' say $obj->get({at => 15}); # dies +=head1 SEE ALSO + +L<Data::MultiValued::RangeContainer>, L<Data::MultiValued::Exceptions> + =head1 AUTHOR Gianni Ceccarelli <dakkar@thenautilus.net> diff --git a/lib/Data/MultiValued/Tags.pm b/lib/Data/MultiValued/Tags.pm index d0d7b36..a42caf4 100644 --- a/lib/Data/MultiValued/Tags.pm +++ b/lib/Data/MultiValued/Tags.pm @@ -107,6 +107,9 @@ version 0.0.1 Stores the given value for the given tag. Replaces existing values. Does not throw exceptions. +Not passing in a C<tag> is equivalent to passing in C<< tag => undef +>>. + No cloning is done: if you pass in a reference, the reference is just stored. @@ -115,9 +118,12 @@ just stored. my $value = $obj->get({ tag => $the_tag }); Retrieves the value for the given tag. Throws a -L<Data::MultiValued::Exceptions::TagNotFound> exception it the tag +L<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 +>>. + No cloning is done: if a reference was stored, you get it back untouched. @@ -128,6 +134,9 @@ untouched. 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> clears everything. Yes, this means that there +is no way to just clear the value for the C<undef> tag. + =head1 SEE ALSO L<Data::MultiValued::TagContainer>, L<Data::MultiValued::Exceptions> diff --git a/lib/Data/MultiValued/TagsAndRanges.pm b/lib/Data/MultiValued/TagsAndRanges.pm index cc57b16..0217a69 100644 --- a/lib/Data/MultiValued/TagsAndRanges.pm +++ b/lib/Data/MultiValued/TagsAndRanges.pm @@ -14,6 +14,7 @@ use Data::MultiValued::TagContainerForRanges; # ABSTRACT: Handle values with tags and validity ranges + has _storage => ( is => 'rw', isa => class_type('Data::MultiValued::TagContainerForRanges'), @@ -39,6 +40,7 @@ sub _as_hash { return {_storage=>$ret}; } + sub set { my ($self,%args) = validated_hash( \@_, @@ -53,6 +55,7 @@ sub set { ->{value} = $args{value}; } + sub get { my ($self,%args) = validated_hash( \@_, @@ -65,6 +68,7 @@ sub get { ->{value}; } + sub clear { my ($self,%args) = validated_hash( \@_, @@ -95,6 +99,58 @@ Data::MultiValued::TagsAndRanges - Handle values with tags and validity ranges version 0.0.1 +=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 + +=head1 METHODS + +=head2 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. + +=head2 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> exception if no ranges +exist in this object that include the point, and +L<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. + +=head2 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. + =head1 AUTHOR Gianni Ceccarelli <dakkar@thenautilus.net> |