File Coverage

blib/lib/Crypt/Keyczar/FileWriter.pm
Criterion Covered Total %
statement 42 42 100.0
branch 7 8 87.5
condition 2 2 100.0
subroutine 10 10 100.0
pod 0 4 0.0
total 61 66 92.4


line stmt bran cond sub pod time code
1             package Crypt::Keyczar::FileWriter;
2 1     1   14954 use base 'Crypt::Keyczar::Writer';
  1         2  
  1         311  
3 1     1   6 use strict;
  1         2  
  1         15  
4 1     1   5 use warnings;
  1         1  
  1         16  
5 1     1   4 use Carp;
  1         2  
  1         36  
6 1     1   5 use File::Path;
  1         2  
  1         282  
7              
8              
9             sub location {
10 233     233 0 366 my $self = shift;
11 233 100       546 if (@_) {
12 58   100     172 my $l = shift || '';
13 58         128 $l =~ s{/$}{};
14 58         140 $self->{location} = $l;
15             }
16 233         3826 return $self->{location};
17             }
18              
19              
20             sub put_metadata {
21 57     57 0 165 my $self = shift;
22 57         100 my $meta = shift;
23              
24 57 100       119 if (!-d $self->location) {
25 6         21 mkpath([$self->location], 0, 0755);
26             }
27 57         182 my $path = sprintf '%s/meta', $self->location;
28 57         199 _put($path, $meta->to_string);
29             }
30              
31              
32             sub put_key {
33 23     23 0 45 my $self = shift;
34 23         60 my ($version, $key) = @_;
35              
36 23 100       61 if (!-d $self->location) {
37 3         13 mkpath([$self->location], 0, 0755);
38             }
39 23         79 my $path = sprintf '%s/%u', $self->location, $version;
40 23         187 _put($path, $key->to_string);
41             }
42              
43              
44             sub delete_key {
45 6     6 0 15 my $self = shift;
46 6         10 my $version = shift;
47 6         19 my $path = sprintf '%s/%u', $self->location, $version;
48 6         520 return unlink($path) == 1;
49             }
50              
51              
52             sub _put {
53 80     80   189 my $path = shift;
54 80         167 my $json = shift;
55              
56 80 50       19500 open my $fh, '>', $path or croak "can't open file: $path: $!";
57 80         740 print $fh $json, "\n";
58 80         3193 close $fh;
59             }
60              
61             1;
62             __END__