blib/lib/Bot/BasicBot/Pluggable/Store.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 184 SCAR 54.8
CC
15
Cov
0%
_keys_aux
72.0
new
30.0
new_from_hashref
20.0
CRAP 240.0
1 package Bot::BasicBot::Pluggable::Store;
2 $Bot::BasicBot::Pluggable::Store::VERSION = '1.20';
3 use strict;
4 use warnings;
5 use Carp qw( croak );
6 use Data::Dumper;
7 use Storable qw( nfreeze thaw );
8 use Try::Tiny;
9 use Module::Load qw();
10 use Log::Log4perl;
11  
12 use base qw( );
13  
14 sub new {
15 my $class = shift;
16 my $self;
17 my $logger = Log::Log4perl->get_logger($class);
18 if ( @_ % 2 == 0 ) {
19 $self = bless {@_} => $class;
20 }
21 elsif ( @_ == 1 and ref $_[0] eq 'HASH' ) {
22 $self = $class->new_from_hashref( $_[0] );
23 }
24 elsif ( @_ == 1 and !ref $_[0] ) {
25 $self = $class->new_from_hashref( { type => $_[0] } );
26 }
27 elsif ( !@_ ) {
28 $self = bless {} => $class;
29 }
30 else {
31 $logger->warn(
32 "Argument to new() is neither an argument list, a hashref, a string nor empty"
33 );
34 }
35 $self->init();
36 $self->load();
37 return $self;
38 }
39  
40 sub new_from_hashref {
41 my ( $class, $args ) = @_;
42 my $logger = Log::Log4perl->get_logger($class);
43  
44 if ( ref($args) ne 'HASH' ) {
45 $logger->warn('Argument to store_from_hashref must be a hashref');
46 }
47  
48 my $store_class = delete $args->{type} || 'Memory';
49  
50 $store_class = "Bot::BasicBot::Pluggable::Store::$store_class"
51 unless $store_class =~ /::/;
52  
53 # load the store class
54 try { Module::Load::load $store_class; }
55 catch { $logger->warn("Couldn't load $store_class - $_"); };
56  
57 my $store = $store_class->new( %{$args} );
58  
59 croak "Couldn't init a $store_class store\n" unless $store;
60  
61 return $store;
62 }
63  
64 sub init { undef }
65  
66 sub load { undef }
67  
68 sub save { }
69  
70 sub keys {
71 my ( $self, $namespace, %opts ) = @_;
72 my $mod = $self->{store}{$namespace} || {};
73 return $self->_keys_aux( $mod, $namespace, %opts );
74 }
75  
76 sub count_keys {
77 my ( $self, $namespace, %opts ) = @_;
78 $opts{_count_only} = 1;
79 $self->keys( $namespace, %opts );
80 }
81  
82 sub _keys_aux {
83 my ( $self, $mod, $namespace, %opts ) = @_;
84  
85 my @res = ( exists $opts{res} ) ? @{ $opts{res} } : ();
86  
87 return CORE::keys %$mod unless @res;
88  
89 my @return;
90 my $count = 0;
91 OUTER: while ( my ($key) = each %$mod ) {
92 for my $re (@res) {
93  
94 # limit matches
95 $re = "^" . lc($namespace) . "_.*${re}.*"
96 if $re =~ m!^[^\^].*[^\$]$!;
97 next OUTER unless $key =~ m!$re!;
98 }
99 push @return, $key if ( !$opts{_count_only} );
100 last if $opts{limit} && ++$count >= $opts{limit};
101  
102 }
103  
104 return ( $opts{_count_only} ) ? $count : @return;
105 }
106  
107 sub get {
108 my ( $self, $namespace, $key ) = @_;
109 return $self->{store}{$namespace}{$key};
110 }
111  
112 sub set {
113 my ( $self, $namespace, $key, $value ) = @_;
114 $self->{store}{$namespace}{$key} = $value;
115 $self->save($namespace);
116 return $self;
117 }
118  
119 sub unset {
120 my ( $self, $namespace, $key ) = @_;
121 delete $self->{store}{$namespace}{$key};
122 $self->save($namespace);
123 return $self;
124 }
125  
126 sub namespaces {
127 my $self = shift;
128 return CORE::keys( %{ $self->{store} } );
129 }
130  
131 sub dump {
132 my $self = shift;
133 my $data = {};
134 for my $n ( $self->namespaces ) {
135 warn "Dumping namespace '$n'.\n";
136 for my $k ( $self->keys($n) ) {
137 $data->{$n}{$k} = $self->get( $n, $k );
138 }
139 }
140 return nfreeze($data);
141 }
142  
143 sub restore {
144 my ( $self, $dump ) = @_;
145 my $data = thaw($dump);
146 for my $n ( CORE::keys(%$data) ) {
147 warn "Restoring namespace '$n'.\n";
148 for my $k ( CORE::keys( %{ $data->{$n} } ) ) {
149 $self->set( $n, $k, $data->{$n}{$k} );
150 }
151 }
152 warn "Complete.\n";
153 }
154  
155 1;
156 __END__
157  
158 =head1 NAME
159  
160 Bot::BasicBot::Pluggable::Store - base class for the back-end pluggable store
161  
162 =head1 VERSION
163  
164 version 1.20
165  
166 =head1 SYNOPSIS
167  
168 my $store = Bot::BasicBot::Pluggable::Store->new( option => "value" );
169  
170 my $namespace = "MyModule";
171  
172 for ( $store->keys($namespace) ) {
173 my $value = $store->get($namespace, $_);
174 $store->set( $namespace, $_, "$value and your momma." );
175 }
176  
177 Store classes should subclass this and provide some persistent way of storing things.
178  
179 =head1 METHODS
180  
181 =over 4
182  
183 =item new()
184  
185 Standard C<new> method, blesses a hash into the right class and
186 puts any key/value pairs passed to it into the blessed hash. If
187 called with an hash argument as its first argument, new_from_hashref
188 will be run with the hash as its only argument. See L</new_from_hashref>
189 for the possible keys and values. You can also pass a string and
190 it will try to call new_from_hashref with a hash reference { type
191 => $string }. Calls C<load()> to load any internal variables, then
192 C<init>, which you can also override in your module.
193  
194 =item new_from_hashref( $hashref )
195  
196 Intended to be called as class method to dynamically create a store
197 object. It expects a hash reference as its only argument. The only
198 required hash element is a string specified by I<type>. This should
199 be either a fully qualified classname or a colonless string that
200 is appended to I<Bot::BasicBot::Pluggable::Store>. All other arguments
201 are passed down to the real object constructor.
202  
203 =item init()
204  
205 Called as part of new class construction, before C<load()>.
206  
207 =item load()
208  
209 Called as part of new class construction, after C<init()>.
210  
211 =item save()
212  
213 Subclass me. But, only if you want to. See ...Store::Storable.pm as an example.
214  
215 =item keys($namespace,[$regex])
216  
217 Returns a list of all store keys for the passed C<$namespace>.
218  
219 If you pass C<$regex> then it will only pass the keys matching C<$regex>
220  
221 =item get($namespace, $variable)
222  
223 Returns the stored value of the C<$variable> from C<$namespace>.
224  
225 =item set($namespace, $variable, $value)
226  
227 Sets stored value for C<$variable> to C<$value> in C<$namespace>. Returns store object.
228  
229 =item unset($namespace, $variable)
230  
231 Removes the C<$variable> from the store. Returns store object.
232  
233 =item namespaces()
234  
235 Returns a list of all namespaces in the store.
236  
237 =item dump()
238  
239 Dumps the complete store to a huge Storable scalar. This is mostly so
240 you can convert from one store to another easily, i.e.:
241  
242 my $from = Bot::BasicBot::Pluggable::Store::Storable->new();
243 my $to = Bot::BasicBot::Pluggable::Store::DBI->new( ... );
244 $to->restore( $from->dump );
245  
246 C<dump> is written generally so you don't have to re-implement it in subclasses.
247  
248 =item restore($data)
249  
250 Restores the store from a L<dump()>.
251  
252 =back
253  
254 =head1 AUTHOR
255  
256 Mario Domgoergen <mdom@cpan.org>
257  
258 This program is free software; you can redistribute it
259 and/or modify it under the same terms as Perl itself.
260  
261 =head1 SEE ALSO
262  
263 L<Bot::BasicBot::Pluggable>
264  
265 L<Bot::BasicBot::Pluggable::Module>