File Coverage

blib/lib/Data/MultiValued/Tags.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::Tags;
2             {
3             $Data::MultiValued::Tags::VERSION = '0.0.1_4';
4             }
5             {
6             $Data::MultiValued::Tags::DIST = 'Data-MultiValued';
7             }
8 4     4   151222 use Moose;
  4         573205  
  4         38  
9 4     4   34300 use namespace::autoclean;
  4         1600  
  4         42  
10 4     4   4029 use MooseX::Params::Validate;
  4         49466  
  4         41  
11 4     4   2111 use Moose::Util::TypeConstraints;
  4         9  
  4         47  
12 4     4   13086 use MooseX::Types::Moose qw(Num Str Undef Any);
  4         336160  
  4         53  
13 4     4   37004 use Data::MultiValued::Exceptions;
  4         15  
  4         199  
14 4     4   2755 use Data::MultiValued::TagContainer;
  4         18  
  4         2065  
15              
16             # ABSTRACT: Handle values with tags
17              
18              
19             has _storage => (
20             is => 'rw',
21             isa => class_type('Data::MultiValued::TagContainer'),
22             init_arg => undef,
23             lazy_build => 1,
24             );
25              
26             sub _build__storage {
27 9     9   413 Data::MultiValued::TagContainer->new();
28             }
29              
30              
31             sub set {
32 11     11 1 1630 my ($self,%args) = validated_hash(
33             \@_,
34             tag => { isa => Str, optional => 1, },
35             value => { isa => Any, },
36             );
37              
38 11         22979 $self->_storage->get_or_create(\%args)
39             ->{value} = $args{value};
40             }
41              
42              
43             sub get {
44 29     29 1 1252 my ($self,%args) = validated_hash(
45             \@_,
46             tag => { isa => Str, optional => 1, },
47             );
48              
49 29         26358 $self->_storage->get(\%args)
50             ->{value};
51             }
52              
53              
54             sub clear {
55 3     3 1 49 my ($self,%args) = validated_hash(
56             \@_,
57             tag => { isa => Str, optional => 1, },
58             );
59              
60 3         5196 $self->_storage->clear(\%args);
61             }
62              
63              
64             sub _rebless_storage {
65 1     1   3 my ($self) = @_;
66              
67 1         9 bless $self->{_storage},'Data::MultiValued::TagContainer';
68             }
69              
70              
71             sub _as_hash {
72 1     1   4 my ($self) = @_;
73              
74 1         2 my %ret = %{$self->_storage};
  1         45  
75 1         6 return {_storage=>\%ret};
76             }
77              
78              
79             __PACKAGE__->meta->make_immutable();
80              
81             1;
82              
83             __END__
84             =pod
85              
86             =encoding utf-8
87              
88             =head1 NAME
89              
90             Data::MultiValued::Tags - Handle values with tags
91              
92             =head1 VERSION
93              
94             version 0.0.1_4
95              
96             =head1 SYNOPSIS
97              
98             use Data::MultiValued::Tags;
99              
100             my $obj = Data::MultiValued::Tags->new();
101             $obj->set({
102             tag => 'tag1',
103             value => 'a string',
104             });
105             say $obj->get({tag=>'tag1'}); # prints 'a string'
106             say $obj->get({tag=>'tag2'}); # dies
107              
108             =head1 METHODS
109              
110             =head2 C<set>
111              
112             $obj->set({ tag => $the_tag, value => $the_value });
113              
114             Stores the given value for the given tag. Replaces existing
115             values. Does not throw exceptions.
116              
117             Not passing in a C<tag> is equivalent to passing in C<< tag => undef
118             >>.
119              
120             No cloning is done: if you pass in a reference, the reference is
121             just stored.
122              
123             =head2 C<get>
124              
125             my $value = $obj->get({ tag => $the_tag });
126              
127             Retrieves the value for the given tag. Throws a
128             L<Data::MultiValued::Exceptions::TagNotFound|Data::MultiValued::Exceptions/Data::MultiValued::Exceptions::TagNotFound>
129             exception if the tag does not exists in this object.
130              
131             Not passing in a C<tag> is equivalent to passing in C<< tag => undef
132             >>.
133              
134             No cloning is done: if a reference was stored, you get it back
135             untouched.
136              
137             =head2 C<clear>
138              
139             $obj->clear({ tag => $the_tag });
140              
141             Deletes the given tag and all data associated with it. Does not throw
142             exceptions: if the tag does not exist, nothing happens.
143              
144             Not passing in a C<tag> clears everything. Yes, this means that there
145             is no way to just clear the value for the C<undef> tag.
146              
147             =head1 Serialisation helpers
148              
149             These are used through
150             L<Data::MultiValued::UglySerializationHelperRole>.
151              
152             =head2 C<_rebless_storage>
153              
154             Blesses the storage into L<Data::MultiValued::TagContainer>.
155              
156             =head2 C<_as_hash>
157              
158             Returns the internal representation with no blessed hashes, with as
159             few copies as possible.
160              
161             =head1 SEE ALSO
162              
163             L<Data::MultiValued::TagContainer>, L<Data::MultiValued::Exceptions>
164              
165             =head1 AUTHOR
166              
167             Gianni Ceccarelli <dakkar@thenautilus.net>
168              
169             =head1 COPYRIGHT AND LICENSE
170              
171             This software is copyright (c) 2011 by Net-a-Porter.com.
172              
173             This is free software; you can redistribute it and/or modify it under
174             the same terms as the Perl 5 programming language system itself.
175              
176             =cut
177