/[Frey]/trunk/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 /trunk/lib/Frey/Storage.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 294 - (show annotations)
Wed Nov 5 08:21:05 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 1205 byte(s)
 r3136@llin (orig r260):  dpavlin | 2008-11-04 23:12:07 +0100
 don't attach extension if allready there, report files with size

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/LoadFile DumpFile/;
8
9 =head2 store
10
11 $o->store( 'filename.yml', $data );
12
13 =cut
14
15 has 'storage_timestamp' => (
16 is => 'rw',
17 isa => 'HashRef[Int]',
18 default => sub { {} },
19 );
20
21 sub store {
22 my $self = shift;
23 my ( $filename, $data ) = @_;
24 $filename .= '.yml' unless $filename =~ m{\.ya?ml$}i;
25 $self->mkbasepath( $filename );
26 DumpFile( $filename, $data );
27 warn "created $filename ", -s $filename;
28 }
29
30 =head2 load
31
32 $data = $o->load( 'filename.yml' );
33
34 =cut
35
36 sub load {
37 my ( $self, $filename ) = @_;
38 $filename .= '.yml';
39 return if ! -e $filename;
40 $self->storage_timestamp->{ $filename } = _path_time( $filename );
41 LoadFile( $filename );
42 }
43
44 =head2 reload
45
46 Reload file if on-disk copy have changed
47
48 my $new_data = $o->reload( 'filename.yml' );
49
50 =cut
51
52 sub _path_time {
53 (stat( $_[0] ))[9]; # mtime
54 }
55
56 sub reload {
57 my ( $self, $filename ) = @_;
58 my $path = $filename . '.yml';
59 return if defined $self->storage_timestamp->{ $path } && $self->storage_timestamp->{ $path } == _path_time( $path );
60 warn "# reload $filename" if $self->debug;
61 $self->load( $filename );
62 }
63
64 1;

  ViewVC Help
Powered by ViewVC 1.1.26