/[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 153 - (show annotations)
Wed Jul 16 23:21:19 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 1034 byte(s)
label editor which save to yaml on disk and use it
1 package Frey::Storage;
2 use Moose::Role;
3
4 #use YAML::Syck; # XXX doesn't play nice with Continuity
5 use YAML qw/LoadFile DumpFile/;
6
7 =head2 store
8
9 $o->store( 'filename.yml', $data );
10
11 =cut
12
13 has 'storage_timestamp' => (
14 is => 'rw',
15 isa => 'HashRef[Int]',
16 default => sub { {} },
17 );
18
19 sub store {
20 my $self = shift;
21 my ( $filename, $data ) = @_;
22 $filename .= '.yml';
23 DumpFile( $filename, $data );
24 }
25
26 =head2 load
27
28 $data = $o->load( 'filename.yml' );
29
30 =cut
31
32 sub load {
33 my ( $self, $filename ) = @_;
34 $filename .= '.yml';
35 return if ! -e $filename;
36 $self->storage_timestamp->{ $filename } = _path_time( $filename );
37 LoadFile( $filename );
38 }
39
40 =head2 reload
41
42 Reload file if on-disk copy have changed
43
44 my $new_data = $o->reload( 'filename.yml' );
45
46 =cut
47
48 sub _path_time {
49 (stat( $_[0] ))[9]; # mtime
50 }
51
52 sub reload {
53 my ( $self, $filename ) = @_;
54 my $path = $filename . '.yml';
55 return if ( $self->storage_timestamp->{ $path } == _path_time( $path ) );
56 warn "# reload $filename" if $self->debug;
57 $self->load( $filename );
58 }
59
60 1;

  ViewVC Help
Powered by ViewVC 1.1.26