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

filter: Statementstmt 86.4% 32 / 37 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 84.2% 48 / 57 CC 7 SCAR 10.7
CC
7
Cov
80%
save
5.5
load
2.9
init
2.3
CRAP 2.9
« blib/lib/Bot/BasicBot/Pluggable/Module.pm ↑ Summary blib/lib/Test/Bot/BasicBot/Pluggable.pm »
1 package Bot::BasicBot::Pluggable::Store::Storable;
2 1 $Bot::BasicBot::Pluggable::Store::Storable::VERSION = '1.11';
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Storable qw( nstore retrieve );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw( Bot::BasicBot::Pluggable::Store );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub init {
12 1 ▶ my $self = shift;
Subroutine init: called CC=2 SCAR=8.1
Pod documented
13 1 ▶ if ( !$self->{dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"dir"}) 0 1 1
14 0 $self->{dir} = File::Spec->curdir();
15 }
16 }
17  
18 sub save {
19 3 ▶ my $self = shift;
Subroutine save: called CC=5 SCAR=17.0
Pod documented
20 3 my $namespace = shift;
21 3 ▶ my @modules = $namespace ? ($namespace) : keys( %{ $self->{store} } );
Branch: 1 branch
BranchTrueFalseTotal
$namespace ? : 3 0 3
22  
23 3 for my $name (@modules) {
24 3 my $filename = File::Spec->catfile( $self->{dir}, $name . ".storable" );
25 3 my ( $fh, $tempfile ) = tempfile( DIR => $self->{dir}, UNLINK => 0 );
26 3 ▶ nstore( $self->{store}{$name}, $tempfile )
Branch: 1 branch
BranchTrueFalseTotal
nstore($self->{"store"}{$name}, $tempfile) or die "Cannot save to $tempfile\n" 0 3 3
27 or die "Cannot save to $tempfile\n";
28 3 ▶ rename $tempfile, $filename
Branch: 1 branch
BranchTrueFalseTotal
rename $tempfile, $filename or die "Cannot create ${filename}: $!\n" 0 3 3
29 or die "Cannot create $filename: $!\n";
30 }
31 }
32  
33 sub load {
34 1 ▶ my $self = shift;
Subroutine load: called CC=2 SCAR=10.5
Pod documented
35 1 for my $file ( glob File::Spec->catfile( $self->{dir}, '*.storable' ) ) {
36 0 my (undef, undef, $name) = map {File::Spec->splitpath($_)} $file =~ /^(.*?)\.storable$/;
37 0 $self->{store}{$name} = retrieve($file);
38 }
39 }
40  
41 1 1;
42  
43 __END__
44  
45 =head1 NAME
46  
47 Bot::BasicBot::Pluggable::Store::Storable - use Storable to provide a storage backend
48  
49 =head1 VERSION
50  
51 version 1.11
52  
53 =head1 SYNOPSIS
54  
55 my $store = Bot::BasicBot::Pluggable::Store::Storable->new(
56 dir => "directory"
57 );
58  
59 $store->set( "namespace", "key", "value" );
60
61 =head1 DESCRIPTION
62  
63 This is a L<Bot::BasicBot::Pluggable::Store> that uses Storable to store
64 the values set by modules.
65  
66 =head1 AUTHOR
67  
68 Mario Domgoergen <mdom@cpan.org>
69  
70 This program is free software; you can redistribute it
71 and/or modify it under the same terms as Perl itself.
« blib/lib/Bot/BasicBot/Pluggable/Module.pm ↑ Summary blib/lib/Test/Bot/BasicBot/Pluggable.pm »