lib/Bot/BasicBot/Pluggable/Store.pm

filter: Statementstmt 78.6% 81 / 103 Branchbran 73.3% 22 / 30 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 84.0% 21 / 25 Podpod 92.3% 12 / 13 totaltotal 77.6% 149 / 192 CC 26 SCAR 18.1
CC
26
Cov
77%
dump
12.0
restore
12.0
_keys_aux
11.0
CRAP 6.1
« blib/lib/App/Bot/BasicBot/Pluggable.pm ↑ Summary lib/Bot/BasicBot/Pluggable.pm »
1 package Bot::BasicBot::Pluggable::Store;
2 15 $Bot::BasicBot::Pluggable::Store::VERSION = '1.11';
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Module::Load qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use base qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new {
15 23 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=21.3
Pod documented
16 23 my $self;
17 23 my $logger = Log::Log4perl->get_logger($class);
18 23 ▶ if ( @_ % 2 == 0 ) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 16 7 23
elsif (@_ == 1 and ref $_[0] eq 'HASH') { } 1 6 7
elsif (@_ == 1 and not ref $_[0]) { } 6 0 6
elsif (not @_) { } 0 0 0
19 16 $self = bless {@_} => $class;
20 }
21 ▶ elsif ( @_ == 1 and ref $_[0] eq 'HASH' ) {
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
061
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
22 1 $self = $class->new_from_hashref( $_[0] );
23 }
24 ▶ elsif ( @_ == 1 and !ref $_[0] ) {
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
006
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
25 6 $self = $class->new_from_hashref( { type => $_[0] } );
26 }
27 elsif ( !@_ ) {
28 0 $self = bless {} => $class;
29 }
30 else {
31 0 $logger->warn(
32 "Argument to new() is neither an argument list, a hashref, a string nor empty"
33 );
34 }
35 23 $self->init();
36 23 $self->load();
37 23 return $self;
38 }
39  
40 sub new_from_hashref {
41 10 ▶ my ( $class, $args ) = @_;
Subroutine new_from_hashref: called CC=5 SCAR=17.2
Pod documented
42 10 my $logger = Log::Log4perl->get_logger($class);
43  
44 10 ▶ if ( ref($args) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args ne "HASH") 0 10 10
45 0 $logger->warn('Argument to store_from_hashref must be a hashref');
46 }
47  
48 10 ▶ my $store_class = delete $args->{type} || 'Memory';
Condition50%1/2
delete $args->{'type'} || 'Memory'
l!l
100
MC/DC0%0/1
delete $args->{'type'} || 'Memory'
delete $args->{'type'}
Truth table50%1/2
A
A= delete $args->{'type'}
Aresult
11
00
49  
50 10 ▶ $store_class = "Bot::BasicBot::Pluggable::Store::$store_class"
Branch: 1 branch
BranchTrueFalseTotal
unless $store_class =~ /::/ 10 0 10
51 unless $store_class =~ /::/;
52  
53 # load the store class
54 10 ▶ try { Module::Load::load $store_class; }
Subroutine __ANON__: called CC=1 SCAR=0.0
55 10 ▶ catch { $logger->warn("Couldn't load $store_class - $_"); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
56  
57 10 my $store = $store_class->new( %{$args} );
58  
59 10 ▶ croak "Couldn't init a $store_class store\n" unless $store;
Branch: 1 branch
BranchTrueFalseTotal
unless $store 0 10 10
60  
61 10 return $store;
62 }
63  
64 20 ▶sub init { undef }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
65  
66 22 ▶sub load { undef }
Subroutine load: called CC=1 SCAR=0.0
Pod documented
67  
68 ▶sub save { }
Subroutine save: called CC=1 SCAR=0.0
Pod documented
69  
70 sub keys {
71 17 ▶ my ( $self, $namespace, %opts ) = @_;
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
72 17 ▶ my $mod = $self->{store}{$namespace} || {};
Condition100%2/2
$self->{'store'}{$namespace} || {}
l!l
134
MC/DC100%1/1
$self->{'store'}{$namespace} || {}
$self->{'store'}{$namespace}
Truth table100%2/2
A
A= $self->{'store'}{$namespace}
Aresult
11
00
73 17 return $self->_keys_aux( $mod, $namespace, %opts );
74 }
75  
76 sub count_keys {
77 0 ▶ my ( $self, $namespace, %opts ) = @_;
Subroutine count_keys: not called CC=1 SCAR=6.9
Pod undocumented
78 0 $opts{_count_only} = 1;
79 0 $self->keys( $namespace, %opts );
80 }
81  
82 sub _keys_aux {
83 18 ▶ my ( $self, $mod, $namespace, %opts ) = @_;
Subroutine _keys_aux: called CC=11 SCAR=24.0
84  
85 18 ▶ my @res = ( exists $opts{res} ) ? @{ $opts{res} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'res'} ? : 10 8 18
86  
87 18 ▶ return CORE::keys %$mod unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 8 10 18
88  
89 10 my @return;
90 10 my $count = 0;
91 10 ▶ OUTER: while ( my ($key) = each %$mod ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = each %$mod) 74 8 82
92 74 for my $re (@res) {
93  
94 # limit matches
95 74 ▶ $re = "^" . lc($namespace) . "_.*${re}.*"
Branch: 1 branch
BranchTrueFalseTotal
if $re =~ /^[^\^].*[^\$]$/ 6 68 74
96 if $re =~ m!^[^\^].*[^\$]$!;
97 74 ▶ next OUTER unless $key =~ m!$re!;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /$re/ 65 9 74
98 }
99 9 ▶ push @return, $key if ( !$opts{_count_only} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"_count_only"} 9 0 9
100 9 ▶ last if $opts{limit} && ++$count >= $opts{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"limit"} and ++$count >= $opts{"limit"} 2 7 9
Condition100%3/3
$opts{"limit"} and ++$count >= $opts{"limit"}
!ll&&!rl&&r
432
MC/DC100%2/2
$opts{"limit"} and ++$count >= $opts{"limit"}
$opts{"limit"} ++$count >= $opts{"limit"}
Truth table100%3/3
A and B
A= $opts{"limit"}B= ++$count >= $opts{"limit"}
ABresult
0X0
100
111
101  
102 }
103  
104 10 ▶ return ( $opts{_count_only} ) ? $count : @return;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'_count_only'} ? : 0 10 10
105 }
106  
107 sub get {
108 1193 ▶ my ( $self, $namespace, $key ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
109 1193 return $self->{store}{$namespace}{$key};
110 }
111  
112 sub set {
113 107 ▶ my ( $self, $namespace, $key, $value ) = @_;
Subroutine set: called CC=1 SCAR=0.0
Pod documented
114 107 $self->{store}{$namespace}{$key} = $value;
115 107 $self->save($namespace);
116 107 return $self;
117 }
118  
119 sub unset {
120 7 ▶ my ( $self, $namespace, $key ) = @_;
Subroutine unset: called CC=1 SCAR=0.0
Pod documented
121 7 delete $self->{store}{$namespace}{$key};
122 7 $self->save($namespace);
123 7 return $self;
124 }
125  
126 sub namespaces {
127 2 ▶ my $self = shift;
Subroutine namespaces: called CC=1 SCAR=0.0
Pod documented
128 2 return CORE::keys( %{ $self->{store} } );
129 }
130  
131 sub dump {
132 0 ▶ my $self = shift;
Subroutine dump: not called CC=3 SCAR=24.8
Pod documented
133 0 my $data = {};
134 0 for my $n ( $self->namespaces ) {
135 0 warn "Dumping namespace '$n'.\n";
136 0 for my $k ( $self->keys($n) ) {
137 0 $data->{$n}{$k} = $self->get( $n, $k );
138 }
139 }
140 0 return nfreeze($data);
141 }
142  
143 sub restore {
144 0 ▶ my ( $self, $dump ) = @_;
Subroutine restore: not called CC=3 SCAR=24.8
Pod documented
145 0 my $data = thaw($dump);
146 0 for my $n ( CORE::keys(%$data) ) {
147 0 warn "Restoring namespace '$n'.\n";
148 0 for my $k ( CORE::keys( %{ $data->{$n} } ) ) {
149 0 $self->set( $n, $k, $data->{$n}{$k} );
150 }
151 }
152 0 warn "Complete.\n";
153 }
154  
155 15 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.11
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>
« blib/lib/App/Bot/BasicBot/Pluggable.pm ↑ Summary lib/Bot/BasicBot/Pluggable.pm »