[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Haver-commits] r47 - haver-core/lib/Haver
From: |
dylan |
Subject: |
[Haver-commits] r47 - haver-core/lib/Haver |
Date: |
Tue, 20 Apr 2004 21:48:56 -0400 (EDT) |
Author: dylan
Date: 2004-04-20 21:48:55 -0400 (Tue, 20 Apr 2004)
New Revision: 47
Modified:
haver-core/lib/Haver/Config.pm
haver-core/lib/Haver/Savable.pm
Log:
Config is now a 'Savable' object.
Modified: haver-core/lib/Haver/Config.pm
===================================================================
--- haver-core/lib/Haver/Config.pm 2004-04-18 01:02:53 UTC (rev 46)
+++ haver-core/lib/Haver/Config.pm 2004-04-21 01:48:55 UTC (rev 47)
@@ -18,7 +18,7 @@
package Haver::Config;
use strict;
#use warnings;
-use base 'Haver::Base';
+use base 'Haver::Savable';
use YAML ();
use Fatal qw(:void open close);
use File::stat;
@@ -30,87 +30,57 @@
sub initialize {
my ($me) = @_;
- my $file = delete $me->{'file'};
-
-
- if (exists $me->{auto_save}) {
- $me->{_auto_save} = delete $me->{auto_save};
- } elsif (defined $file) {
- $me->{_auto_save} = 1;
+ $me->SUPER::initialize;
+
+ if (exists $me->{file}) {
+ $me->filename(delete $me->{file});
}
-
- if (defined $file) {
- $me->load($file);
+
+ if (defined $me->filename) {
+ $me->load;
}
}
+sub _load {
+ my ($me, $data) = @_;
+ ASSERT: ref($data) eq 'HASH' or UNIVERSAL::isa($data, 'HASH');
-sub load {
- my ($me, $file) = @_;
- ASSERT: @_ == 2;
- ASSERT: defined $file;
-
- if (-f $file) {
- my $fh;
- open $fh, "<$file";
- local $/ = undef;
- my $conf = YAML::Load(readline($fh));
- close $fh;
- $me->{_mtime} = stat($file)->mtime;
- foreach my $key (grep(!/^_/, keys %$conf)) {
- $me->{$key} = $conf->{$key};
- }
-
+ foreach my $k (keys %$data) {
+ $me->{$k} = $data->{$k};
}
-
- $me->{_file} = $file;
}
-sub auto_save {
- my ($me, $val) = @_;
- $me->{_auto_save} = $val;
-}
-
-sub reload {
+sub _save {
my ($me) = @_;
- ASSERT: defined $me->{_file};
- $me->load($me->{_file});
-}
+ my %data = ();
-sub save {
- my ($me) = @_;
- my $file = $me->{'_file'};
- my $mtime = $me->{'_mtime'};
- my $t = -f $file ? stat($file)->mtime : $mtime;
-
- ASSERT: defined $me->{'_file'};
-
- if ($mtime == $t) {
- my %copy;
- foreach my $key (grep(!/^_/, keys %$me)) {
- $copy{$key} = $me->{$key};
- }
- my $fh;
- open $fh, ">$file";
- print $fh YAML::Dump(\%copy);
- close $fh;
- chmod(0600, $file);
- } else {
- warn "Cowardly refusing to overwrite $file...";
+ foreach my $k (keys %$me) {
+ next if $k =~ /^_/;
+ $data{$k} = $me->{$k};
}
- $me->{_mtime} = stat($file)->mtime;
+ return \%data;
}
+sub load {
+ my ($me, $fn) = @_;
+ $me->filename($fn) if defined $fn;
+ $me->SUPER::load;
+}
-sub finalize {
- my ($me) = @_;
- $me->save if $me->{_auto_save};
+sub filename {
+ my ($me, $file) = @_;
+
+ if (@_ == 2) {
+ return $me->{_filename};
+ } elsif (@_ == 3) {
+ return $me->{_filename} = $file;
+ }
+
}
-
1;
__END__
Modified: haver-core/lib/Haver/Savable.pm
===================================================================
--- haver-core/lib/Haver/Savable.pm 2004-04-18 01:02:53 UTC (rev 46)
+++ haver-core/lib/Haver/Savable.pm 2004-04-21 01:48:55 UTC (rev 47)
@@ -23,64 +23,67 @@
use Fatal qw(:void open close);
use File::stat;
+use IO::File;
use Haver::Preprocessor;
+use Carp;
our $VERSION = '0.02';
our $RELOAD = 1;
sub initialize {
my ($me) = @_;
+
+ $me->{_mtime} = -1;
+ $me->{_auto_save} = 0;
}
-
sub load {
my ($me) = @_;
- ASSERT: @_ == 2;
- ASSERT: defined $file;
+ my $filename = $me->filename;
+ my $fh;
+ local $/ = undef;
- if (-f $file) {
- my $fh;
- open $fh, "<$file";
- local $/ = undef;
- my $data = YAML::Load(readline($fh));
- close $fh;
+ ASSERT: defined $filename;
+ open $fh, $filename;
+ my $raw = readline $fh; # slurp in file
+ my $data = YAML::Load($raw);
+ close $fh;
+ $me->{_mtime} = stat($filename)->mtime;
- $me->{_mtime} = stat($file)->mtime;
- foreach my $key (grep(!/^_/, keys %$conf)) {
- $me->{$key} = $conf->{$key};
- }
-
- }
-
- $me->{_file} = $file;
+ $me->_load($data);
}
-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;
+ my $filename = $me->filename;
+ my $fh;
- ASSERT: defined $file;
-
- if ($mtime == $t) {
- my $data = $me->_save($file);
+ ASSERT: defined $filename;
+ my $mtime = stat($filename)->mtime;
+
+ if ($mtime == $me->{_mtime}) {
my $fh;
- open $fh, ">$file";
- print $fh YAML::Dump($data);
+ my $save = $me->_save or return 0;
+ open $fh, ">$filename";
+ print $fh YAML::Dump($save);
close $fh;
- chmod(0600, $file);
+ $me->{_mtime} = stat($filename)->mtime;
+ return 1;
} else {
- warn "Cowardly refusing to overwrite $file...";
+ carp "Cowardly refusing to overwrite $filename...";
+ return 0;
+ }
+}
+
+sub auto_save {
+ my ($me, $val) = @_;
+ ASSERT: @_ == 2 or @_ == 3;
+
+ if (@_ == 2) {
+ return $me->{_auto_save};
+ } elsif (@_ == 3) {
+ return $me->{_auto_save} = $val;
}
-
- $me->{_mtime} = stat($file)->mtime;
}
@@ -101,9 +104,7 @@
=head1 SYNOPSIS
- use Haver::Config;
- my $config = new Haver::Config(file => 'some-file.yaml');
-
+ FIXME
=head1 DESCRIPTION
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Haver-commits] r47 - haver-core/lib/Haver,
dylan <=