[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Haver-commits] r45 - haver-core/lib/Haver
From: |
dylan |
Subject: |
[Haver-commits] r45 - haver-core/lib/Haver |
Date: |
Sat, 17 Apr 2004 21:01:09 -0400 (EDT) |
Author: dylan
Date: 2004-04-17 21:01:08 -0400 (Sat, 17 Apr 2004)
New Revision: 45
Added:
haver-core/lib/Haver/Savable.pm
Log:
Testing...
Added: haver-core/lib/Haver/Savable.pm
===================================================================
--- haver-core/lib/Haver/Savable.pm 2004-04-18 00:58:08 UTC (rev 44)
+++ haver-core/lib/Haver/Savable.pm 2004-04-18 01:01:08 UTC (rev 45)
@@ -0,0 +1,140 @@
+# Haver::Savable - Saver/Loader
+#
+# Copyright (C) 2004 Dylan William Hardison
+#
+# This module is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This module is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this module; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+package Haver::Savable;
+use strict;
+#use warnings;
+use base 'Haver::Base';
+use YAML ();
+use Fatal qw(:void open close);
+use File::stat;
+
+use Haver::Preprocessor;
+
+our $VERSION = '0.02';
+our $RELOAD = 1;
+
+sub initialize {
+ my ($me) = @_;
+}
+
+
+sub load {
+ my ($me) = @_;
+ ASSERT: @_ == 2;
+ ASSERT: defined $file;
+
+ if (-f $file) {
+ my $fh;
+ open $fh, "<$file";
+ local $/ = undef;
+ my $data = YAML::Load(readline($fh));
+ close $fh;
+
+ $me->{_mtime} = stat($file)->mtime;
+ foreach my $key (grep(!/^_/, keys %$conf)) {
+ $me->{$key} = $conf->{$key};
+ }
+
+ }
+
+ $me->{_file} = $file;
+}
+
+sub auto_save {
+ my ($me, $val) = @_;
+ $me->{_auto_save} = $val;
+}
+
+
+sub save {
+ my ($me) = @_;
+ my $file = $me->filename;
+ my $mtime = $me->{'_mtime'};
+ my $t = -f $file ? stat($file)->mtime : $mtime;
+
+ ASSERT: defined $file;
+
+ if ($mtime == $t) {
+ my $data = $me->_save($file);
+ my $fh;
+ open $fh, ">$file";
+ print $fh YAML::Dump($data);
+ close $fh;
+ chmod(0600, $file);
+ } else {
+ warn "Cowardly refusing to overwrite $file...";
+ }
+
+ $me->{_mtime} = stat($file)->mtime;
+}
+
+
+sub finalize {
+ my ($me) = @_;
+
+ $me->save if $me->{_auto_save};
+}
+
+
+1;
+
+__END__
+
+=head1 NAME
+
+Haver::Savable - Save/Load Objects
+
+=head1 SYNOPSIS
+
+ use Haver::Config;
+ my $config = new Haver::Config(file => 'some-file.yaml');
+
+
+=head1 DESCRIPTION
+
+FIXME
+
+=head1 SEE ALSO
+
+L<https://savannah.nongnu.org/projects/haver/>
+
+
+
+=head1 AUTHOR
+
+Dylan William Hardison, E<lt>address@hidden<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2004 by Dylan William Hardison
+
+This library is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this module; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+=cut
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Haver-commits] r45 - haver-core/lib/Haver,
dylan <=