diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/MultiValued/#Tags.pm# | 121 | ||||
-rw-r--r-- | lib/Data/MultiValued/Ranges.pm | 85 | ||||
-rw-r--r-- | lib/Data/MultiValued/Tags.pm | 4 |
3 files changed, 205 insertions, 5 deletions
diff --git a/lib/Data/MultiValued/#Tags.pm# b/lib/Data/MultiValued/#Tags.pm# new file mode 100644 index 0000000..29cf102 --- /dev/null +++ b/lib/Data/MultiValued/#Tags.pm# @@ -0,0 +1,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; diff --git a/lib/Data/MultiValued/Ranges.pm b/lib/Data/MultiValued/Ranges.pm index 57bda5d..ef55b4c 100644 --- a/lib/Data/MultiValued/Ranges.pm +++ b/lib/Data/MultiValued/Ranges.pm @@ -12,7 +12,8 @@ use MooseX::Types::Moose qw(Num Str Undef Any); use Data::MultiValued::Exceptions; use Data::MultiValued::RangeContainer; -# ABSTRACT: Handle values with tags and validity ranges +# ABSTRACT: Handle values with validity ranges + has _storage => ( is => 'rw', @@ -38,6 +39,7 @@ sub _as_hash { return {_storage=>\%ret}; } + sub set { my ($self,%args) = validated_hash( \@_, @@ -50,6 +52,7 @@ sub set { ->{value} = $args{value}; } + sub get { my ($self,%args) = validated_hash( \@_, @@ -60,6 +63,7 @@ sub get { ->{value}; } + sub clear { my ($self,%args) = validated_hash( \@_, @@ -70,7 +74,6 @@ sub clear { $self->_storage->clear(\%args); } - 1; __END__ @@ -78,12 +81,88 @@ __END__ =head1 NAME -Data::MultiValued::Ranges - Handle values with tags and validity ranges +Data::MultiValued::Ranges - Handle values with validity ranges =head1 VERSION version 0.0.1 +=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. Does not throw exceptions. + +The range is defined as C<< Num $x : $min <= $x < $max >>. + +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> exception if no ranges +exist in this object that include the point (remember that a range +does not include its C<to> point). + +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. Does not throw exceptions. + +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 AUTHOR Gianni Ceccarelli <dakkar@thenautilus.net> diff --git a/lib/Data/MultiValued/Tags.pm b/lib/Data/MultiValued/Tags.pm index 7295db8..d0d7b36 100644 --- a/lib/Data/MultiValued/Tags.pm +++ b/lib/Data/MultiValued/Tags.pm @@ -12,7 +12,7 @@ use MooseX::Types::Moose qw(Num Str Undef Any); use Data::MultiValued::Exceptions; use Data::MultiValued::TagContainer; -# ABSTRACT: Handle values with tags and validity ranges +# ABSTRACT: Handle values with tags has _storage => ( @@ -80,7 +80,7 @@ __END__ =head1 NAME -Data::MultiValued::Tags - Handle values with tags and validity ranges +Data::MultiValued::Tags - Handle values with tags =head1 VERSION |