r136@llin (orig r135): dpavlin | 2007-10-26 23:04:55 +0200
[perl-cwmp.git] / lib / CWMP / Store / YAML.pm
1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 10/26/07 21:37:12 CEST
2 package CWMP::Store::YAML;
3
4 use strict;
5 use warnings;
6
7 use Data::Dump qw/dump/;
8 use YAML qw/LoadFile DumpFile/;
9
10 =head1 NAME
11
12 CWMP::Store::YAML - use YAML as storage
13
14 =head1 METHODS
15
16 =head2 open
17
18 =cut
19
20 my $dir = 'yaml';
21
22 my $debug = 1;
23
24 sub open {
25         my $self = shift;
26
27         warn "open ",dump( @_ );
28
29         if ( ! -e $dir ) {
30                 mkdir $dir || die "can't create $dir: $!";
31                 warn "created $dir directory\n";
32         }
33
34 }
35
36 =head2 update_uid_state
37
38   $store->update_uid_state( $uid, $state );
39
40 =cut
41
42 sub update_uid_state {
43         my ( $self, $uid, $state ) = @_;
44
45         my $file = "$dir/$uid.yml";
46
47         DumpFile( $file, $state ) || die "can't write $file: $!";
48
49 }
50
51 =head2 get_state
52
53   $store->get_state( $uid );
54
55 =cut
56
57 sub get_state {
58         my ( $self, $uid ) = @_;
59
60         my $file = "$dir/$uid.yml";
61
62         if ( -e $file ) {
63                 return LoadFile( $file );
64         }
65
66         return;
67 }
68
69 =head2 all_uids
70
71   my @uids = $store->all_uids;
72
73 =cut
74
75 sub all_uids {
76         my $self = shift;
77
78         opendir(my $d, $dir) || die "can't opendir $dir: $!";
79         my @uids = grep { /\.yml$/ && -f "$dir/$_" } readdir($d);
80         closedir $d;
81
82         return map { s/\.yml$// } @uids;
83 }
84
85 1;