/[cwmp]/google/branches/store-pluggable/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

Annotation of /google/branches/store-pluggable/lib/CWMP/Store/YAML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (hide annotations)
Sat Oct 27 22:43:25 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 1764 byte(s)
- added clean parametar to stores to start with empty database
- much less chatty without debug
- test both existing store plugins
- finish API rename in CWMP::Store, version bump [0.05]
1 dpavlin 136 # 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 dpavlin 139 use Hash::Merge qw/merge/;
10 dpavlin 140 use Carp qw/confess/;
11 dpavlin 136
12     =head1 NAME
13    
14     CWMP::Store::YAML - use YAML as storage
15    
16     =head1 METHODS
17    
18     =head2 open
19    
20 dpavlin 147 $store->open({
21     path => 'var/',
22     debug => 1,
23     clean => 1,
24     });
25    
26 dpavlin 136 =cut
27    
28 dpavlin 140 my $path;
29 dpavlin 136
30 dpavlin 147 my $debug = 0;
31 dpavlin 136
32     sub open {
33     my $self = shift;
34    
35 dpavlin 140 my $args = shift;
36 dpavlin 136
37 dpavlin 140 $debug = $args->{debug};
38     $path = $args->{path} || confess "no path?";
39    
40     warn "open ",dump( $args ) if $debug;
41    
42     $path = "$path/yaml";
43    
44     if ( ! -e $path ) {
45     mkdir $path || die "can't create $path: $!";
46 dpavlin 147 warn "created $path directory\n" if $debug;
47     } elsif ( $args->{clean} ) {
48     warn "removed old $path\n" if $debug;
49     foreach my $uid ( $self->all_uids ) {
50     my $file = "$path/$uid.yml";
51     unlink $file || die "can't remove $file: $!";
52     }
53 dpavlin 136 }
54    
55 dpavlin 147
56 dpavlin 136 }
57    
58     =head2 update_uid_state
59    
60     $store->update_uid_state( $uid, $state );
61    
62     =cut
63    
64     sub update_uid_state {
65     my ( $self, $uid, $state ) = @_;
66    
67 dpavlin 140 my $file = "$path/$uid.yml";
68 dpavlin 136
69 dpavlin 139 my $old_state = $self->get_state( $uid );
70 dpavlin 136
71 dpavlin 139 my $combined = merge( $state, $old_state );
72    
73     # warn "## ",dump( $old_state, $state, $combined );
74    
75     DumpFile( $file, $combined ) || die "can't write $file: $!";
76    
77 dpavlin 136 }
78    
79     =head2 get_state
80    
81     $store->get_state( $uid );
82    
83     =cut
84    
85     sub get_state {
86     my ( $self, $uid ) = @_;
87    
88 dpavlin 140 my $file = "$path/$uid.yml";
89 dpavlin 136
90     if ( -e $file ) {
91     return LoadFile( $file );
92     }
93    
94     return;
95     }
96    
97     =head2 all_uids
98    
99     my @uids = $store->all_uids;
100    
101     =cut
102    
103     sub all_uids {
104     my $self = shift;
105    
106 dpavlin 140 opendir(my $d, $path) || die "can't opendir $path: $!";
107     my @uids = grep { /\.yml$/ && -f "$path/$_" } readdir($d);
108 dpavlin 136 closedir $d;
109    
110 dpavlin 137 return map { my $l = $_; $l =~ s/\.yml$//; $l } @uids;
111 dpavlin 136 }
112    
113     1;

  ViewVC Help
Powered by ViewVC 1.1.26