Digest/Object.pm

filter: Statementstmt 55.3% 62 / 112 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.1% 16 / 29 Podpod 92.3% 12 / 13 totaltotal 50.8% 90 / 177 CC 15 SCAR 24.0
CC
15
Cov
47%
as_string
42.0
digest
20.0
BUILD
12.0
CRAP 11.0
« Digest/Driver.pm ↑ Summary Digest.pm »
1 package Store::Digest::Object;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp 'verbose';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use MooseX::Types::Moose qw(Maybe Int CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Store::Digest::Types qw(FiniteHandle DigestHash NonNegativeInt
Subroutine BEGIN: called CC=1 SCAR=0.0
15 ContentType Token DateTimeType MaybeDateTime
16 1 MaybeToken);
17  
18 # flags
19 1 ▶use constant TYPE_CHECKED => 1 << 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant TYPE_VALID => 1 << 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant CHARSET_CHECKED => 1 << 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant CHARSET_VALID => 1 << 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant ENCODING_CHECKED => 1 << 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant ENCODING_VALID => 1 << 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant SYNTAX_CHECKED => 1 << 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant SYNTAX_VALID => 1 << 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =head1 NAME
29  
30 Store::Digest::Object - One distinct Store::Digest data object
31  
32 =head1 VERSION
33  
34 Version 0.01
35  
36 =cut
37  
38 1 our $VERSION = '0.01';
39  
40  
41 =head1 SYNOPSIS
42  
43 my $dataobj = $store->get('sha-256' => $key);
44  
45 my $fh = $dataobj->content;
46  
47  
48 =head1 METHODS
49  
50 This class exists to encapsulate the metadata relevant to a
51 L<Store::Digest> data object. It is not instantiated directly. All
52 methods are read-only accessors.
53  
54 =head2 content
55  
56 Returns a filehandle or equivalent, pointing to the object's content,
57 open in read-only mode. Unless the object has been deleted, then this
58 will be C<undef>.
59  
60 =cut
61  
62 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
63 0 my $self = shift;
64 0 ▶ $self->mtime($self->ctime) unless $self->mtime;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mtime 0 0 0
65 0 ▶ $self->ptime($self->ctime) unless $self->ptime;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ptime 0 0 0
66 }
67  
68 # has content => (
69 # is => 'ro',
70 # required => 0,
71 # isa => Maybe[FiniteHandle],
72 # );
73  
74 1 has _content => (
75 is => 'rw',
76 required => 0,
77 isa => Maybe[CodeRef|FiniteHandle],
78 init_arg => 'content',
79 );
80  
81 0 ▶sub content {
Subroutine content: not called CC=3 SCAR=24.8
Pod documented
82 0 my $self = shift;
83 0 my $content = $self->_content;
84 0 ▶ if ($content and ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content and ref $content eq "CODE") 0 0 0
Condition0%0/3
$content and ref $content eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$content and ref $content eq "CODE"
$content ref $content eq "CODE"
Truth table0%0/3
A and B
A= $contentB= ref $content eq "CODE"
ABresult
0X0
100
111
85 0 $content = $content->();
86 0 $self->_content($content);
87 }
88 0 $content;
89 }
90  
91 =head2 digest
92  
93 my $uri = $dataobj->digest('sha-1');
94  
95 my @algos = $dataobj->digest;
96  
97 Returns a L<URI::di> object for the relevant digest algorithm. Will
98 croak if an invalid digest algorithm is supplied. While valid digest
99 algorithms are specified at creation time, you can retrieve them by
100 calling this method with no arguments.
101  
102 =cut
103  
104 1 has _digests => (
105 is => 'ro',
106 isa => DigestHash,
107 required => 1,
108 init_arg => 'digests',
109 );
110  
111 0 ▶sub digest {
Subroutine digest: not called CC=4 SCAR=30.0
Pod documented
112 0 my ($self, $algo) = @_;
113 0 my $d = $self->_digests;
114 0 ▶ unless (defined $algo) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $algo) 0 0 0
115 0 my @k = sort keys %$d;
116 0 ▶ return wantarray ? @k : \@k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
117 }
118  
119 # lowercase it
120 0 $algo = lc $algo;
121  
122 # hee hee self-reference
123 Carp::croak("No algorithm named $algo, only " . join ' ' , $self->digest)
124 0 ▶ unless defined $d->{$algo};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d->{$algo} 0 0 0
125  
126 # clone the URI so that it can't be messed with
127 0 $d->{$algo}->clone;
128 }
129  
130 =head2 size
131  
132 Returns the byte size of the object. Note that for deleted objects,
133 this will be whatever the size of the object was before it was
134 deleted.
135  
136 =cut
137  
138 1 has size => (
139 is => 'ro',
140 isa => NonNegativeInt,
141 required => 1,
142 );
143  
144 =head2 type
145  
146 Returns the MIME type
147  
148 =cut
149  
150 1 has type => (
151 is => 'ro',
152 isa => Maybe[ContentType],
153 required => 0,
154 );
155  
156 =head2 charset
157  
158 Returns the character set (e.g. C<utf-8>) of the data object if known.
159  
160 =cut
161  
162 1 has charset => (
163 is => 'ro',
164 isa => Maybe[Token],
165 required => 0,
166 );
167  
168 =head2 language
169  
170 Returns the natural language in
171 L<RFC 5646|http://tools.ietf.org/html/rfc5646> format, if it was
172 supplied.
173  
174 =cut
175  
176 1 has language => (
177 is => 'ro',
178 isa => Maybe[Token],
179 required => 0,
180 );
181  
182 =head2 encoding
183  
184 Returns the I<transfer encoding>, of the data object if known,
185 (e.g. C<gzip> or C<deflate>, I<not> the L</charset>).
186  
187 =cut
188  
189 1 has encoding => (
190 is => 'ro',
191 isa => Maybe[Token],
192 required => 0,
193 );
194  
195 =head2 ctime
196  
197 Returns the timestamp at which the object was I<added> to
198 the store, from the point of view of the system.
199  
200 =cut
201  
202 1 has ctime => (
203 is => 'ro',
204 isa => DateTimeType,
205 required => 1,
206 coerce => 1,
207 );
208  
209 =head2 mtime
210  
211 Returns the timestamp that was supplied as the modification time of
212 the object from the point of view of the I<user>, if different from
213 L</ctime>.
214  
215 =cut
216  
217 1 has mtime => (
218 is => 'rw',
219 isa => MaybeDateTime,
220 required => 0,
221 coerce => 1,
222 );
223  
224 =head2 ptime
225  
226 Returns the timestamp of the time the I<metadata properties> of the
227 object were last updated.
228  
229 =cut
230  
231 1 has ptime => (
232 is => 'rw',
233 isa => DateTimeType,
234 required => 0,
235 coerce => 1,
236 );
237  
238 =head2 dtime
239  
240 Returns the system timestamp at which the object was I<deleted>, if
241 applicable.
242  
243 =cut
244  
245 1 has dtime => (
246 is => 'rw',
247 isa => MaybeDateTime,
248 required => 0,
249 coerce => 1,
250 );
251  
252 1 has _flags => (
253 is => 'ro',
254 isa => Int,
255 required => 1,
256 default => 0,
257 init_arg => 'flags',
258 );
259  
260 =head2 type_checked
261  
262 This flag represents that the claimed content-type has been checked.
263  
264 =cut
265  
266 0 ▶sub type_checked {
Subroutine type_checked: not called CC=1 SCAR=6.9
Pod documented
267 0 shift->_flags & TYPE_CHECKED;
268 }
269  
270 =head2 type_valid
271  
272 =cut
273  
274 0 ▶sub type_valid {
Subroutine type_valid: not called CC=1 SCAR=6.9
Pod documented
275 0 shift->_flags & (TYPE_CHECKED|TYPE_VALID);
276 }
277  
278 =head2 charset_checked
279  
280 =cut
281  
282 0 ▶sub charset_checked {
Subroutine charset_checked: not called CC=1 SCAR=6.9
Pod documented
283 0 shift->_flags & CHARSET_CHECKED;
284 }
285  
286 =head2 charset_valid
287  
288 =cut
289  
290 0 ▶sub charset_valid {
Subroutine charset_valid: not called CC=1 SCAR=6.9
Pod documented
291 0 shift->_flags & (CHARSET_CHECKED|CHARSET_VALID);
292 }
293  
294 =head2 encoding_checked
295  
296 =cut
297  
298 0 ▶sub encoding_checked {
Subroutine encoding_checked: not called CC=1 SCAR=6.9
Pod documented
299 0 shift->_flags & ENCODING_CHECKED;
300 }
301  
302 =head2 encoding_valid
303  
304 =cut
305  
306 0 ▶sub encoding_valid {
Subroutine encoding_valid: not called CC=1 SCAR=6.9
Pod documented
307 0 shift->_flags & (ENCODING_CHECKED|ENCODING_VALID);
308 }
309  
310 =head2 syntax_checked
311  
312 This flag represents an additional layer of syntax checking, e.g. XML
313 validation.
314  
315 =cut
316  
317 0 ▶sub syntax_checked {
Subroutine syntax_checked: not called CC=1 SCAR=6.9
Pod documented
318 0 shift->_flags & SYNTAX_CHECKED;
319 }
320  
321 =head2 syntax_valid
322  
323 =cut
324  
325 0 ▶sub syntax_valid {
Subroutine syntax_valid: not called CC=1 SCAR=6.9
Pod documented
326 0 shift->_flags & (SYNTAX_CHECKED|SYNTAX_VALID);
327 }
328  
329 =head2 matches $DIGEST
330  
331 Pass in a L<URI::ni> object or string representing a RFC6920 named
332 identifier, and this method will tell you you whether or not the
333 object possesses a matching digest.
334  
335 =cut
336  
337 0 ▶sub matches {
Subroutine matches: not called CC=3 SCAR=24.8
Pod documented
338 0 my ($self, $digest) = @_;
339 0 $digest = URI->new($digest)->canonical;
340 0 ▶ Carp::croak('Input must be an RFC6920 address')
Branch: 1 branch
BranchTrueFalseTotal
unless $digest->isa("URI::ni") 0 0 0
341 unless $digest->isa('URI::ni');
342 0 ▶ my $d = $self->_digests->{$digest->algorithm} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $self->_digests->{$digest->algorithm} or return 0 0 0
343 0 return $digest->eq($d);
344 }
345  
346 =head2 as_string
347  
348 =cut
349  
350 0 ▶sub as_string {
Subroutine as_string: not called CC=6 SCAR=37.4
Pod documented
351 0 my $self = shift;
352  
353 0 my %labels = (
354 size => 'Size (Bytes)',
355 ctime => 'Added to Store',
356 mtime => 'Last Modified',
357 ptime => 'Properties Modified',
358 dtime => 'Deleted',
359 type => 'Content Type',
360 language => '(Natural) Language',
361 charset => 'Character Set',
362 encoding => 'Content Encoding',
363  
364 );
365 0 my @mandatory = qw(size ctime mtime ptime);
366 0 my @optional = qw(dtime type language charset encoding);
367  
368 0 my $out = sprintf "%s\n Digests:\n", ref $self;
369  
370 0 for my $d ($self->digest) {
371 0 $out .= sprintf(" %s\n", $self->digest($d));
372 }
373  
374 0 for my $m (@mandatory) {
375 0 $out .= " $labels{$m}: " . $self->$m . "\n";
376 }
377 0 for my $o (@optional) {
378 0 my $val = $self->$o;
379 0 ▶ $out .= " $labels{$o}: $val\n" if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
380 }
381  
382 0 my $f = $self->_flags;
383 0 my @a = qw(content-type charset content-encoding syntax);
384 0 my %x = (
385 0 => 'unverified',
386 1 => 'invalid',
387 2 => 'recheck',
388 3 => 'valid',
389 );
390  
391 0 $out .= " Validation:\n";
392 0 for my $i (0..$#a) {
393 0 my $x = ($f >> (3 - $i)) & 3;
394 0 $out .= sprintf(" %-16s: %s\n", $a[$i], $x{$x});
395 }
396  
397 0 $out;
398 }
399  
400 =head1 AUTHOR
401  
402 Dorian Taylor, C<< <dorian at cpan.org> >>
403  
404 =head1 LICENSE AND COPYRIGHT
405  
406 Copyright 2012 Dorian Taylor.
407  
408 Licensed under the Apache License, Version 2.0 (the "License"); you
409 may not use this file except in compliance with the License. You may
410 obtain a copy of the License at
411 L<http://www.apache.org/licenses/LICENSE-2.0>.
412  
413 Unless required by applicable law or agreed to in writing, software
414 distributed under the License is distributed on an "AS IS" BASIS,
415 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
416 implied. See the License for the specific language governing
417 permissions and limitations under the License.
418  
419  
420 =cut
421  
422 1 __PACKAGE__->meta->make_immutable;
423  
424 1 1; # End of Store::Digest::Object
« Digest/Driver.pm ↑ Summary Digest.pm »