/[Frey]/branches/zimbardo/lib/Frey/Storage.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /branches/zimbardo/lib/Frey/Storage.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1189 - (show annotations)
Mon Sep 28 20:23:45 2009 UTC (14 years, 7 months ago) by dpavlin
File size: 1660 byte(s)
use Storage to store files with exstension .st(orgage)
1 package Frey::Storage;
2 use Moose::Role;
3
4 with 'Frey::Path';
5
6 #use YAML::Syck; # XXX doesn't play nice with Continuity
7 use YAML qw//;
8 use File::Slurp qw//;
9 use Storable qw//;
10 use Carp qw/croak carp/;
11
12 =head2 store
13
14 $o->store( 'filename.yml', $data );
15
16 =cut
17
18 has 'storage_timestamp' => (
19 is => 'rw',
20 isa => 'HashRef[Int]',
21 default => sub { {} },
22 );
23
24 sub store {
25 my $self = shift;
26 my ( $filename, $data ) = @_;
27 $self->mkbasepath( $filename );
28 if ( $filename =~ m{\.ya?ml$}i ) {
29 YAML::DumpFile( $filename, $data );
30 } elsif ( $filename =~ m{\.sto?r?a?b?l?e?$} ) {
31 Storable::store $data => $filename;
32 } else {
33 croak "without extension we can save only scalar data" if ref($data);
34 File::Slurp::write_file( $filename, $data );
35 }
36 carp "created $filename ", -s $filename;
37 }
38
39 =head2 load
40
41 $data = $o->load( 'filename.yml' );
42
43 =cut
44
45 sub load {
46 my ( $self, $filename ) = @_;
47 return if ! -e $filename;
48 my $data;
49 if ( $filename =~ m{\.ya?ml}i ) {
50 $data = YAML::LoadFile( $filename );
51 } elsif ( $filename =~ m{\.stor?a?b?l?e?$} ) {
52 $data = Storable::retrieve $filename;
53 } else {
54 $data = File::Slurp::read_file( $filename );
55 }
56 $self->storage_timestamp->{ $filename } = _path_time( $filename );
57 return $data;
58 }
59
60 =head2 reload
61
62 Reload file if on-disk copy have changed
63
64 my $new_data = $o->reload( 'filename.yml' );
65
66 =cut
67
68 sub _path_time {
69 (stat( $_[0] ))[9]; # mtime
70 }
71
72 sub reload {
73 my ( $self, $filename ) = @_;
74 return if defined $self->storage_timestamp->{ $filename } && $self->storage_timestamp->{ $filename } == _path_time( $filename );
75 warn "# reload $filename" if $self->debug;
76 $self->load( $filename );
77 }
78
79 no Moose::Role;
80
81 1;

  ViewVC Help
Powered by ViewVC 1.1.26