File Coverage

blib/lib/MooX/TaggedAttributes/Cache.pm
Criterion Covered Total %
statement 73 73 100.0
branch 2 2 100.0
condition 16 27 59.2
subroutine 16 16 100.0
pod 6 6 100.0
total 113 124 91.1


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: Extract information from a Tagged Attribute Cache
3              
4             use v5.10.1;
5 5     5   67  
  5         15  
6             use strict;
7 5     5   26 use warnings;
  5         9  
  5         91  
8 5     5   20  
  5         10  
  5         206  
9             our $VERSION = '0.18';
10              
11             use Const::Fast ();
12 5     5   2111 use overload '%{}' => \&tag_hash, fallback => 1;
  5         7358  
  5         155  
13 5     5   68  
  5         10  
  5         45  
14              
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35             my ( $class, $target ) = @_;
36              
37 68     68 1 175 return bless { list => $target->_tag_list }, $class;
38             }
39 68         1920  
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56              
57              
58              
59              
60              
61              
62              
63              
64              
65              
66              
67              
68              
69              
70             my $self = shift;
71              
72             no overloading;
73 54     54 1 25898  
74             return $self->{tag_attr_hash} //= do {
75 5     5   554 my %tags;
  5         9  
  5         741  
76             for my $tuple ( @{ $self->{list} } ) {
77 54   33     235 # my ( $tag, $attrs, $value ) = @$tuple;
78 54         100 my $tag = ( $tags{ $tuple->[0] } //= {} );
79 54         88 $tag->{$_} = $tuple->[2] for @{ $tuple->[1] };
  54         149  
80             }
81 240   100     613 Const::Fast::const my %rtags => %tags;
82 240         293 \%rtags;
  240         771  
83             };
84 54         217 }
85 54         26545 *tag_hash = \&tag_attr_hash;
86              
87              
88              
89              
90              
91              
92              
93              
94              
95              
96              
97              
98              
99              
100              
101              
102              
103              
104              
105              
106              
107              
108              
109              
110              
111             my $self = shift;
112              
113             no overloading;
114              
115 2     2 1 6 return $self->{tag_value_hash} //= do {
116             my %tags;
117 5     5   111 for my $tuple ( @{ $self->{list} } ) {
  5         10  
  5         726  
118             # my ( $tag, $attrs, $value ) = @$tuple;
119 2   33     20 my $tag = ( $tags{ $tuple->[0] } //= {} );
120 2         4 # copy so don't corrupt internal list.
121 2         5 push @{ $tag->{ $tuple->[2] } //= [] }, @{ $tuple->[1] };
  2         9  
122             }
123 16   100     45 Const::Fast::const my %rtags => %tags;
124             \%rtags;
125 16   100     21 };
  16         52  
  16         42  
126             }
127 2         16  
128 2         685  
129              
130              
131              
132              
133              
134              
135              
136              
137              
138              
139             my $self = shift;
140              
141             no overloading;
142              
143             return $self->{attr_hash} //= do {
144 22     22 1 40 my %attrs;
145             for my $tuple ( @{ $self->{list} } ) {
146 5     5   33 # my ( $tag, $attrs, $value ) = @$tuple;
  5         10  
  5         692  
147             ( $attrs{$_} //= {} )->{ $tuple->[0] } = $tuple->[2]
148 22   66     117 for @{ $tuple->[1] };
149 12         21 }
150 12         19 Const::Fast::const my %rattrs => %attrs;
  12         40  
151             \%rattrs;
152             };
153 96   100     119 }
  96         376  
154              
155 12         67  
156 12         3007  
157              
158              
159              
160              
161              
162              
163              
164              
165              
166              
167             my ( $self, $attr ) = @_;
168              
169             no overloading;
170              
171             if ( !defined $attr ) {
172             return $self->{tags} //= do {
173 10     10 1 28 Const::Fast::const my @tags => keys %{ $self->tag_hash };
174             \@tags;
175 5     5   36 }
  5         8  
  5         714  
176             }
177 10 100       30  
178 2   33     16 return ( $self->{attr} //= {} )->{$attr} //= do {
179 2         7 my $attrs = $self->attr_hash;
  2         10  
180 2         104 [ keys %{ $attrs->{$attr} // {} } ];
181             };
182             }
183              
184 8   50     63  
      33        
185 8         27  
186 8   50     15  
  8         95  
187              
188              
189              
190              
191              
192             my ( $self, $attr, $tag ) = @_;
193              
194             no autovivification;
195             return $self->attr_hash->{$attr}{$tag};
196             }
197              
198             #
199 12     12 1 3510 # This file is part of MooX-TaggedAttributes
200             #
201 5     5   2147 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
  5         3560  
  5         20  
202 12         28 #
203             # This is free software, licensed under:
204             #
205             # The GNU General Public License, Version 3, June 2007
206             #
207              
208              
209             =pod
210              
211             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
212              
213             =head1 NAME
214              
215             MooX::TaggedAttributes::Cache - Extract information from a Tagged Attribute Cache
216              
217             =head1 VERSION
218              
219             version 0.18
220              
221             =head1 SYNOPSIS
222              
223             $cache = MooX::TaggedAttributes::Cache->new( $class );
224              
225             $tags = $cache->tags;
226              
227             =head1 DESCRIPTION
228              
229             L<MooX::TaggedAttributes> caches attribute tags as objects of this class.
230             The user typically never instantiates objects of L<MooX::TaggedAttributes::Cache>.
231             Instead, they are returned by the L<_tags|MooX::TaggedAttributes/_tags> method added
232             to tagged classes, e.g.
233              
234             $cache = $class->_tags;
235              
236             =head1 CLASS METHODS
237              
238             =head2 new
239              
240             $cache = MooX::TaggedAttributes::Cache( $class );
241              
242             Create a cache object for the C<$class>, which must have a C<_tag_list> method.
243              
244             =head1 METHODS
245              
246             =head2 tag_attr_hash
247              
248             $tags = $cache->tag_attr_hash;
249              
250             Returns a reference to a read-only hash keyed off of the tags in the
251             cache. The values are hashes which map attribute names to tag values.
252              
253             For example, given:
254              
255             has attr1 => ( ..., tag1 => 'foo' );
256             has attr2 => ( ..., tag1 => 'foo' );
257             has attr3 => ( ..., tag2 => 'bar' );
258             has attr4 => ( ..., tag2 => 'bar' );
259              
260             this will be returned:
261              
262             {
263             tag1 => { attr1 => 'foo', attr2 => 'foo' },
264             tag2 => { attr3 => 'bar', attr4 => 'bar' },
265             }
266              
267             =head2 tag_hash
268              
269             This is a deprecated alias for L</tag_attr_hash>
270              
271             =head2 tag_value_hash
272              
273             $tags = $cache->tag_value_hash;
274              
275             Returns a reference to a hash keyed off of the tags in the cache. The
276             values are hashes which map tag values to attribute names (as an
277             arrayref of names ).
278              
279             For example, given:
280              
281             has attr1 => ( ..., tag1 => 'foo' );
282             has attr2 => ( ..., tag1 => 'foo' );
283             has attr3 => ( ..., tag1 => 'bar' );
284             has attr4 => ( ..., tag1 => 'bar' );
285              
286             this may be returned (the order of the attribute names is arbitrary):
287              
288             { tag1 => { foo => [ 'attr1', 'attr2' ],
289             bar => [ 'attr3', 'attr4' ],
290             },
291              
292             =head2 attr_hash
293              
294             $tags = $cache->tag_hash;
295              
296             Returns a reference to a hash keyed off of the attributes in the
297             cache. The values are hashes which map tag names to tag values.
298              
299             =head2 tags
300              
301             # return all of the tags as an array reference
302             $tags = $cache->tags;
303              
304             # return the tags for the specified attribute as an array reference
305             $tags = $cache->tags( $attr );
306              
307             Returns a reference to an array containing tags.
308              
309             =head2 value
310              
311             $value = $cache->value( $attr, $tag );
312              
313             Return the value of a tag for the given attribute.
314              
315             =head1 OVERLOAD
316              
317             =head2 %{}
318              
319             The object may be treated as a hash reference. It will operate on the
320             reference returned by L</tag_hash>. For example,
321              
322             keys %{ $cache };
323              
324             is equivalent to
325              
326             keys %{ $cache->tag_hash };
327              
328             =head1 SUPPORT
329              
330             =head2 Bugs
331              
332             Please report any bugs or feature requests to bug-moox-taggedattributes@rt.cpan.org or through the web interface at: https://rt.cpan.org/Public/Dist/Display.html?Name=MooX-TaggedAttributes
333              
334             =head2 Source
335              
336             Source is available at
337              
338             https://gitlab.com/djerius/moox-taggedattributes
339              
340             and may be cloned from
341              
342             https://gitlab.com/djerius/moox-taggedattributes.git
343              
344             =head1 SEE ALSO
345              
346             Please see those modules/websites for more information related to this module.
347              
348             =over 4
349              
350             =item *
351              
352             L<MooX::TaggedAttributes|MooX::TaggedAttributes>
353              
354             =back
355              
356             =head1 AUTHOR
357              
358             Diab Jerius <djerius@cpan.org>
359              
360             =head1 COPYRIGHT AND LICENSE
361              
362             This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
363              
364             This is free software, licensed under:
365              
366             The GNU General Public License, Version 3, June 2007
367              
368             =cut