[Bps-public-commit] r16796 - in Prophet/trunk: .

sunnavy at bestpractical.com sunnavy at bestpractical.com
Mon Nov 10 01:37:55 EST 2008


Author: sunnavy
Date: Mon Nov 10 01:37:46 2008
New Revision: 16796

Modified:
   Prophet/trunk/   (props changed)
   Prophet/trunk/lib/Prophet/Config.pm

Log:
 r17676 at sunnavys-mb:  sunnavy | 2008-11-10 14:25:43 +0800
 a basic save method in config, so we can save aliases


Modified: Prophet/trunk/lib/Prophet/Config.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Config.pm	(original)
+++ Prophet/trunk/lib/Prophet/Config.pm	Mon Nov 10 01:37:46 2008
@@ -78,6 +78,7 @@
             $config->{$key} = $val;
         }
     }
+    $config->{_aliases} ||= {}; # default aliases is null.
 }
 
 sub display_name_for_uuid {
@@ -102,6 +103,44 @@
     return (-e $file) ? $file : '';
 }
 
+=head2 save FILENAME
+
+save the current config to file, if the file is not supplied,
+save to $self->app_config_file
+
+=cut
+
+#XXX TODO this won't save comments, which I think we should do.
+#in case of overwriting your file( you will hate me for that ), 
+#I chose to update alias only for now.
+
+sub save {
+    my $self = shift;
+    my $file = shift || $self->app_config_file;
+
+    my @lines;
+    if ( $self->file_if_exists($file) ) {
+        my $file = file($file);
+        @lines = $file->slurp;
+    }
+
+    open my $fh, '>', $file or die "can't save config to $file: $!";
+    for my $line (@lines) {
+
+        # skip old aliases
+        next if $line =~ /^ \s* alias \s+ .+ \s* = \s* .+/x;
+        print $fh $line;
+    }
+
+    if ( $self->aliases ) {
+        for my $alias ( keys %{ $self->aliases } ) {
+            print $fh "alias $alias = " . $self->aliases->{$alias} . "\n";
+        }
+    }
+    close $fh;
+    return 1;
+}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 



More information about the Bps-public-commit mailing list