| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Keys::E::Store::Dir; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Data::Keys::E::Store::Dir - folder storage |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $dk = Data::Keys->new( |
|
10
|
|
|
|
|
|
|
'base_dir' => '/some/folder', |
|
11
|
|
|
|
|
|
|
'extend_with' => 'Store::Dir', |
|
12
|
|
|
|
|
|
|
); |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Store values into a folder. Keys are the file names. |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 METHODS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
|
21
|
|
|
|
|
|
|
|
|
22
|
11
|
|
|
11
|
|
1437
|
use warnings; |
|
|
11
|
|
|
|
|
18
|
|
|
|
11
|
|
|
|
|
427
|
|
|
23
|
11
|
|
|
11
|
|
44
|
use strict; |
|
|
11
|
|
|
|
|
15
|
|
|
|
11
|
|
|
|
|
468
|
|
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
26
|
|
|
|
|
|
|
|
|
27
|
11
|
|
|
11
|
|
566
|
use Moose::Role; |
|
|
11
|
|
|
|
|
4392
|
|
|
|
11
|
|
|
|
|
108
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'base_dir' => ( isa => 'Str', is => 'rw',); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
after 'init' => sub { |
|
32
|
|
|
|
|
|
|
my $self = shift; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
confess 'base_dir is a mandatory argument' |
|
35
|
|
|
|
|
|
|
if not $self->base_dir; |
|
36
|
|
|
|
|
|
|
confess $self->base_dir.' is not a writable folder' |
|
37
|
|
|
|
|
|
|
if (not -d $self->base_dir) or (not -w $self->base_dir); |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
return; |
|
40
|
|
|
|
|
|
|
}; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 get($filename) |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Reads C<$filename> and returns its content. |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub get { |
|
49
|
20
|
|
|
20
|
1
|
2950
|
my $self = shift; |
|
50
|
20
|
|
|
|
|
38
|
my $key = shift; |
|
51
|
20
|
50
|
|
|
|
74
|
confess 'too many arguments ' if @_; |
|
52
|
|
|
|
|
|
|
|
|
53
|
20
|
|
|
|
|
123
|
my $filename = $self->_make_filename($key); |
|
54
|
20
|
|
|
|
|
37
|
return eval { IO::Any->slurp([$filename]) }; |
|
|
20
|
|
|
|
|
176
|
|
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 set($filename, $content) |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Writes C<$content> into the C<$filename>. Returns C<$filename>. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub set { |
|
64
|
22
|
|
|
22
|
1
|
2387
|
my $self = shift; |
|
65
|
22
|
|
|
|
|
100
|
my $key = shift; |
|
66
|
22
|
|
|
|
|
33
|
my $value = shift; |
|
67
|
22
|
50
|
|
|
|
75
|
confess 'too many arguments ' if @_; |
|
68
|
|
|
|
|
|
|
|
|
69
|
22
|
|
|
|
|
71
|
my ($new_key, $filename) = $self->_make_filename($key); |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# if value is undef, remove the file |
|
72
|
22
|
100
|
|
|
|
78
|
if (not defined $value) { |
|
73
|
2
|
50
|
66
|
|
|
104
|
unlink($filename) || (not -f $filename) || warn 'failed to remove "'.$filename.'"'; |
|
74
|
2
|
|
|
|
|
16
|
return $new_key; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
20
|
|
|
|
|
94
|
eval { IO::Any->spew([$filename], $value, { 'atomic' => 1 }); }; |
|
|
20
|
|
|
|
|
321
|
|
|
78
|
20
|
100
|
|
|
|
10905
|
confess 'failed to store "'.$key.'" - '.$@ |
|
79
|
|
|
|
|
|
|
if $@; |
|
80
|
|
|
|
|
|
|
|
|
81
|
18
|
|
|
|
|
74
|
return $new_key; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _make_filename { |
|
85
|
59
|
|
|
59
|
|
86
|
my $self = shift; |
|
86
|
59
|
|
|
|
|
83
|
my $key = shift; |
|
87
|
59
|
50
|
33
|
|
|
383
|
confess 'need key (with length > 0) as argument' |
|
88
|
|
|
|
|
|
|
if ((not defined $key) or (length($key) == 0)); |
|
89
|
59
|
50
|
|
|
|
149
|
confess 'too many arguments ' if @_; |
|
90
|
|
|
|
|
|
|
|
|
91
|
59
|
|
|
|
|
2430
|
my $filename = File::Spec->catfile( |
|
92
|
|
|
|
|
|
|
$self->base_dir, |
|
93
|
|
|
|
|
|
|
$key |
|
94
|
|
|
|
|
|
|
); |
|
95
|
|
|
|
|
|
|
|
|
96
|
59
|
100
|
|
|
|
258
|
return ($key, $filename) |
|
97
|
|
|
|
|
|
|
if wantarray; |
|
98
|
20
|
|
|
|
|
47
|
return $filename; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 AUTHOR |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Jozef Kutej |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |