line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Keys::E::Value::InfDef; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Data::Keys::E::Value::InfDef - inflate/deflate values |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Date::Keys; |
10
|
|
|
|
|
|
|
my $dk = Data::Keys->new( |
11
|
|
|
|
|
|
|
'base_dir' => '/folder/full/of/json/files', |
12
|
|
|
|
|
|
|
'extend_with' => ['Store::Dir', 'Value::InfDef'], |
13
|
|
|
|
|
|
|
'inflate' => sub { JSON::Util->decode($_[0]) }, |
14
|
|
|
|
|
|
|
'deflate' => sub { JSON::Util->encode($_[0]) }, |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my %data = %{$dk->get('abcd.json')}; |
18
|
|
|
|
|
|
|
$dk->set('abcd.json', \%data); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Uses callback to automatically inflate and deflate. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
1751
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
27
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
1
|
|
491
|
use Moose::Role; |
|
1
|
|
|
|
|
4380
|
|
|
1
|
|
|
|
|
6
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 PROPERTIES |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head2 inflate |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Callback executed with C<get> value. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 deflate |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Callback executed with C<set> value. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has 'inflate' => ( isa => 'CodeRef', is => 'rw', ); |
46
|
|
|
|
|
|
|
has 'deflate' => ( isa => 'CodeRef', is => 'rw', ); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
requires('get', 'set'); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
around 'get' => sub { |
51
|
|
|
|
|
|
|
my $get = shift; |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
my $key = shift; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $value = $self->$get($key); |
56
|
|
|
|
|
|
|
return undef if not defined $value; |
57
|
|
|
|
|
|
|
return $self->inflate->($value, $self, $key); |
58
|
|
|
|
|
|
|
}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
around 'set' => sub { |
61
|
|
|
|
|
|
|
my $set = shift; |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
my $key = shift; |
64
|
|
|
|
|
|
|
my $value = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# if value is undef, remove the file |
67
|
|
|
|
|
|
|
return $self->$set($key) |
68
|
|
|
|
|
|
|
if (not defined $value); |
69
|
|
|
|
|
|
|
return $self->$set($key, $self->deflate->($value, $self, $key)); |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 AUTHOR |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Jozef Kutej |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=cut |