diff options
Diffstat (limited to 'lib/Data/MultiValued/Tags.pm')
-rw-r--r-- | lib/Data/MultiValued/Tags.pm | 168 |
1 files changed, 98 insertions, 70 deletions
diff --git a/lib/Data/MultiValued/Tags.pm b/lib/Data/MultiValued/Tags.pm index 063a778..6d213d4 100644 --- a/lib/Data/MultiValued/Tags.pm +++ b/lib/Data/MultiValued/Tags.pm @@ -1,4 +1,10 @@ package Data::MultiValued::Tags; +{ + $Data::MultiValued::Tags::VERSION = '0.0.1_5'; +} +{ + $Data::MultiValued::Tags::DIST = 'Data-MultiValued'; +} use Moose; use namespace::autoclean; use MooseX::Params::Validate; @@ -8,19 +14,6 @@ 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 - -=cut has _storage => ( is => 'rw', @@ -33,33 +26,100 @@ sub _build__storage { Data::MultiValued::TagContainer->new(); } -=method C<set> - $obj->set({ tag => $the_tag, value => $the_value }); +sub set { + my ($self,%args) = validated_hash( + \@_, + tag => { isa => 'Str', optional => 1, }, + value => { isa => 'Any', }, + ); -Stores the given value for the given tag. Replaces existing -values. Does not throw exceptions. + $self->_storage->get_or_create(\%args) + ->{value} = $args{value}; +} -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. +sub get { + my ($self,%args) = validated_hash( + \@_, + tag => { isa => 'Str', optional => 1, }, + ); -=cut + $self->_storage->get(\%args) + ->{value}; +} -sub set { + +sub clear { my ($self,%args) = validated_hash( \@_, tag => { isa => 'Str', optional => 1, }, - value => { isa => 'Any', }, ); - $self->_storage->get_or_create(\%args) - ->{value} = $args{value}; + $self->_storage->clear(\%args); +} + + +sub _rebless_storage { + my ($self) = @_; + + bless $self->{_storage},'Data::MultiValued::TagContainer'; } -=method C<get> + +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::Tags - Handle values with tags + +=head1 VERSION + +version 0.0.1_5 + +=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 + +=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. + +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. + +=head2 C<get> my $value = $obj->get({ tag => $the_tag }); @@ -73,19 +133,7 @@ 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. -=cut - -sub get { - my ($self,%args) = validated_hash( - \@_, - tag => { isa => 'Str', optional => 1, }, - ); - - $self->_storage->get(\%args) - ->{value}; -} - -=method C<clear> +=head2 C<clear> $obj->clear({ tag => $the_tag }); @@ -95,17 +143,6 @@ 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. -=cut - -sub clear { - my ($self,%args) = validated_hash( - \@_, - tag => { isa => 'Str', optional => 1, }, - ); - - $self->_storage->clear(\%args); -} - =head1 Serialisation helpers These are used through @@ -115,34 +152,25 @@ L<Data::MultiValued::UglySerializationHelperRole>. Blesses the storage into L<Data::MultiValued::TagContainer>. -=cut - -sub _rebless_storage { - my ($self) = @_; - - bless $self->{_storage},'Data::MultiValued::TagContainer'; -} - =head2 C<_as_hash> Returns the internal representation with no blessed hashes, with as few copies as possible. -=cut +=head1 SEE ALSO -sub _as_hash { - my ($self) = @_; +L<Data::MultiValued::TagContainer>, L<Data::MultiValued::Exceptions> - my %ret = %{$self->_storage}; - return {_storage=>\%ret}; -} +=head1 AUTHOR -=head1 SEE ALSO +Gianni Ceccarelli <dakkar@thenautilus.net> -L<Data::MultiValued::TagContainer>, L<Data::MultiValued::Exceptions> +=head1 COPYRIGHT AND LICENSE -=cut +This software is copyright (c) 2011 by Net-a-Porter.com. -__PACKAGE__->meta->make_immutable(); +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 -1; |