File Coverage

blib/lib/Data/Persist.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             package Data::Persist;
2             {
3             $Data::Persist::VERSION = '0.12';
4             }
5             BEGIN {
6 1     1   38584 $Data::Persist::AUTHORITY = 'cpan:TEX';
7             }
8             # ABSTRACT: an easy-to-use data-to-disk dumper
9              
10 1     1   31 use 5.010_000;
  1         4  
  1         37  
11 1     1   3926 use mro 'c3';
  1         988  
  1         7  
12 1     1   42 use feature ':5.10';
  1         2  
  1         108  
13              
14 1     1   545 use Moose;
  0            
  0            
15             use namespace::autoclean;
16              
17             # use IO::Handle;
18             # use autodie;
19             # use MooseX::Params::Validate;
20              
21             use Try::Tiny;
22             use Data::Serializer;
23             use File::Blarf;
24              
25             has 'filename' => (
26             'is' => 'rw',
27             'isa' => 'Str',
28             'required' => 1,
29             );
30              
31             has '_serializer' => (
32             'is' => 'ro',
33             'isa' => 'Data::Serializer',
34             'lazy' => 1,
35             'builder' => '_init_serializer',
36             );
37              
38             with qw(Log::Tree::RequiredLogger);
39              
40             sub _init_serializer {
41             my $self = shift;
42              
43             my $ser = Data::Serializer::->new( compress => 1, );
44             return $ser;
45             }
46              
47             sub BUILD {
48             my $self = shift;
49              
50             # make sure we can either write to the exisiting file or, if it does not exist, write
51             # to the parent directory
52             if ( $self->filename() && -e $self->filename() && !-w $self->filename() ) {
53             die( 'Can not write cache file at ' . $self->filename() );
54             }
55             elsif ( $self->filename() && !-e $self->filename() ) {
56             my @path = split /\//, $self->filename();
57             my $file = pop @path;
58             my $dir = join q{/}, @path;
59             if ( !-w $dir ) {
60             die( 'File ' . $self->filename() . ' does not exist and parent directory '.$dir.' is not writeable.' );
61             }
62             }
63              
64             return 1;
65             }
66             ## no critic (ProhibitBuiltinHomonyms)
67             sub write {
68             ## use critic
69             my $self = shift;
70             my $hash_ref = shift;
71             my $filename = shift || $self->filename();
72              
73             my $success = try {
74             my $text = $self->_serializer()->freeze($hash_ref);
75             File::Blarf::blarf( $filename, $text, { Flock => 1, } );
76             1;
77             } catch {
78             $self->logger()->log( message => 'Failed to serialize cache: '.$_, level => 'warning' );
79             };
80             if ( !$success ) {
81             return;
82             }
83             $self->logger()->log( message => 'Serialized cache to ' . $filename, level => 'debug' );
84             return 1;
85             }
86             ## no critic (ProhibitBuiltinHomonyms)
87             sub read {
88             ## use critic
89             my $self = shift;
90             my $filename = shift || $self->filename();
91              
92             if ( !-e $filename ) {
93             $self->logger()->log( message => 'No cache file found at ' . $filename, level => 'notice' );
94             return;
95             }
96              
97             my $text = File::Blarf::slurp( $filename, { Flock => 1, } );
98             my $unser;
99             my $success = try {
100             $unser = $self->_serializer()->thaw($text);
101             1;
102             } catch {
103             $self->logger()->log( message => 'Failed to unserialize cache: '.$_, level => 'warning' );
104             };
105             if ( !$success || !$unser ) {
106             return;
107             }
108             $self->logger()->log( message => 'Unserialized cache from ' . $filename, level => 'debug' );
109             return $unser;
110             }
111              
112             no Moose;
113             __PACKAGE__->meta->make_immutable;
114              
115             1;
116              
117             __END__
118              
119             =pod
120              
121             =encoding utf-8
122              
123             =head1 NAME
124              
125             Data::Persist - an easy-to-use data-to-disk dumper
126              
127             =head1 METHODS
128              
129             =head2 write
130              
131             Takes two arguments: an data structure and an filename.
132              
133             Serializes that data structure and writes to the given filename.
134              
135             =head2 read
136              
137             Takes one argument: an filename to read from.
138              
139             Unserializes this data structure and returns it.
140              
141             =head2 BUILD
142              
143             Make sure given filename is accessible.
144              
145             =head1 NAME
146              
147             Data::Persist - an easy-to-use data-to-disk dumper
148              
149             =head1 AUTHOR
150              
151             Dominik Schulz <dominik.schulz@gauner.org>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2012 by Dominik Schulz.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut