/[cwmp]/google/trunk/lib/CWMP/Store/YAML.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

Diff of /google/trunk/lib/CWMP/Store/YAML.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 224 by dpavlin, Sat Nov 24 02:17:40 2007 UTC revision 225 by dpavlin, Sun Nov 25 13:34:09 2007 UTC
# Line 4  package CWMP::Store::YAML; Line 4  package CWMP::Store::YAML;
4  use strict;  use strict;
5  use warnings;  use warnings;
6    
7  use Data::Dump qw/dump/;  use CWMP::Store::HASH;
8    use base qw/CWMP::Store::HASH/;
9    
10  use YAML::Syck;  use YAML::Syck;
 use Hash::Merge qw/merge/;  
 use Carp qw/confess/;  
11    
12  =head1 NAME  =head1 NAME
13    
14  CWMP::Store::YAML - use YAML as storage  CWMP::Store::YAML - use YAML as storage
15    
 =head1 METHODS  
   
 =head2 open  
   
   $store->open({  
         path => 'var/',  
         debug => 1,  
         clean => 1,  
   });  
   
 =cut  
   
 my $path;  
   
 my $debug = 0;  
   
 sub open {  
         my $self = shift;  
   
         my $args = shift;  
   
         $debug = $args->{debug};  
         $path = $args->{path} || confess "no path?";  
   
         warn "open ",dump( $args ) if $debug;  
   
         $path = "$path/yaml";  
   
         if ( ! -e $path ) {  
                 mkdir $path || die "can't create $path: $!";  
                 warn "created $path directory\n" if $debug;  
         } elsif ( $args->{clean} ) {  
                 warn "removed old $path\n" if $debug;  
                 foreach my $uid ( $self->all_uids ) {  
                         my $file = "$path/$uid.yml";  
                         unlink $file || die "can't remove $file: $!";  
                 }  
         }  
   
   
 }  
   
 =head2 update_uid_state  
   
   $store->update_uid_state( $uid, $state );  
   
16  =cut  =cut
17    
18  sub update_uid_state {  my $full_path;
         my ( $self, $uid, $state ) = @_;  
   
         my $file = "$path/$uid.yml";  
   
         my $old_state = $self->get_state( $uid );  
   
         my $combined = merge( $state, $old_state );  
   
 #       warn "## ",dump( $old_state, $state, $combined );  
   
         DumpFile( $file, $combined ) || die "can't write $file: $!";  
19    
20    sub full_path {
21            my ( $self, $path ) = @_;
22            $full_path = "$path/yaml";
23            warn "## full_path: $full_path";
24            return $full_path;
25  }  }
26    
27  =head2 get_state  sub file {
   
   $store->get_state( $uid );  
   
 =cut  
   
 sub get_state {  
28          my ( $self, $uid ) = @_;          my ( $self, $uid ) = @_;
29            my $file = "$full_path/$uid" . $self->extension;
30          my $file = "$path/$uid.yml";          warn "## file -> $file";
31            return $file;
         if ( -e $file ) {  
                 return LoadFile( $file );  
         }  
   
         return;  
32  }  }
33    
34  =head2 all_uids  sub save_hash {
35            my ( $self, $file, $hash ) = @_;
36    my @uids = $store->all_uids;          DumpFile( $file, $hash );
37    }
 =cut  
   
 sub all_uids {  
         my $self = shift;  
   
         opendir(my $d, $path) || die "can't opendir $path: $!";  
         my @uids = grep { /\.yml$/ && -f "$path/$_" } readdir($d);  
         closedir $d;  
38    
39          return map { my $l = $_; $l =~ s/\.yml$//; $l } @uids;  sub load_hash {
40            my ( $self, $file ) = @_;
41            LoadFile( $file );
42  }  }
43    
44    sub extension { '.yml' };
45    
46  1;  1;

Legend:
Removed from v.224  
changed lines
  Added in v.225

  ViewVC Help
Powered by ViewVC 1.1.26