From: Dobrica Pavlinusic Date: Fri, 26 Oct 2007 21:04:55 +0000 (+0000) Subject: simple YAML store plugin to ease debugging X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=5932049f4e38bb24ce33fedc7574ef1988099618;hp=-c;p=perl-cwmp.git simple YAML store plugin to ease debugging git-svn-id: https://perl-cwmp.googlecode.com/svn/branches/store-pluggable@135 836a5e1a-633d-0410-964b-294494ad4392 --- 5932049f4e38bb24ce33fedc7574ef1988099618 diff --git a/lib/CWMP/Store/YAML.pm b/lib/CWMP/Store/YAML.pm new file mode 100644 index 0000000..588060f --- /dev/null +++ b/lib/CWMP/Store/YAML.pm @@ -0,0 +1,85 @@ +# Dobrica Pavlinusic, 10/26/07 21:37:12 CEST +package CWMP::Store::YAML; + +use strict; +use warnings; + +use Data::Dump qw/dump/; +use YAML qw/LoadFile DumpFile/; + +=head1 NAME + +CWMP::Store::YAML - use YAML as storage + +=head1 METHODS + +=head2 open + +=cut + +my $dir = 'yaml'; + +my $debug = 1; + +sub open { + my $self = shift; + + warn "open ",dump( @_ ); + + if ( ! -e $dir ) { + mkdir $dir || die "can't create $dir: $!"; + warn "created $dir directory\n"; + } + +} + +=head2 update_uid_state + + $store->update_uid_state( $uid, $state ); + +=cut + +sub update_uid_state { + my ( $self, $uid, $state ) = @_; + + my $file = "$dir/$uid.yml"; + + DumpFile( $file, $state ) || die "can't write $file: $!"; + +} + +=head2 get_state + + $store->get_state( $uid ); + +=cut + +sub get_state { + my ( $self, $uid ) = @_; + + my $file = "$dir/$uid.yml"; + + if ( -e $file ) { + return LoadFile( $file ); + } + + return; +} + +=head2 all_uids + + my @uids = $store->all_uids; + +=cut + +sub all_uids { + my $self = shift; + + opendir(my $d, $dir) || die "can't opendir $dir: $!"; + my @uids = grep { /\.yml$/ && -f "$dir/$_" } readdir($d); + closedir $d; + + return map { s/\.yml$// } @uids; +} + +1;