File Coverage

blib/lib/Bot/IRC/Store.pm
Criterion Covered Total %
statement 11 28 39.2
branch 0 4 0.0
condition 0 2 0.0
subroutine 4 8 50.0
pod 1 2 50.0
total 16 44 36.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Bot::IRC persistent data storage with YAML
2              
3             use 5.014;
4 11     11   2332584 use exact;
  11         103  
5 11     11   384  
  11         27092  
  11         48  
6             use YAML::XS qw( LoadFile DumpFile );
7 11     11   10798  
  11         23419  
  11         4290  
8             our $VERSION = '1.39'; # VERSION
9              
10             my ($bot) = @_;
11             my $obj = __PACKAGE__->new($bot);
12 4     4 0 10  
13 4         20 $bot->subs( 'store' => sub { return $obj } );
14             }
15 4     0   36  
  0            
16             my ( $class, $bot ) = @_;
17             my $self = bless( {}, $class );
18              
19 0     0     $self->{file} = $bot->vars || 'store.yaml';
20 0            
21             eval {
22 0   0       unless ( -f $self->{file} ) {
23             DumpFile( $self->{file}, {} );
24 0           }
25 0 0         else {
26 0           LoadFile( $self->{file} );
27             }
28             };
29 0           die qq{Unable to use "$self->{file}" for YAML storage in the Bot::IRC::Store plugin\n} if ($@);
30              
31             return $self;
32 0 0         }
33              
34 0           my ( $self, $key ) = @_;
35             return LoadFile( $self->{file} )->{ ( caller() )[0] }{$key};
36             }
37              
38 0     0 1   my ( $self, $key, $value ) = @_;
39 0            
40             my $data = LoadFile( $self->{file} );
41             $data->{ ( caller() )[0] }{$key} = $value;
42              
43 0     0     DumpFile( $self->{file}, $data );
44             return $self;
45 0           }
46 0            
47             1;
48 0            
49 0            
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Bot::IRC::Store - Bot::IRC persistent data storage with YAML
57              
58             =head1 VERSION
59              
60             version 1.39
61              
62             =head1 SYNOPSIS
63              
64             use Bot::IRC;
65              
66             Bot::IRC->new(
67             connect => { server => 'irc.perl.org' },
68             plugins => ['Store'],
69             vars => { store => 'bot.yaml' },
70             )->run;
71              
72             =head1 DESCRIPTION
73              
74             This L<Bot::IRC> plugin provides a very simple persistent storage mechanism. It
75             stores all its data in a single YAML file. This makes things easy when you're
76             dealing with a small amount of data, but performance will get increasingly bad
77             as data increases. Consequently, you should probably not use this module
78             specifically in a long-running production bot. Instead, use some Storage
79             pseudo sub-class like L<Bot::IRC::Store::SQLite>.
80              
81             =head1 EXAMPLE USE
82              
83             This plugin adds a single sub to the bot object called C<store()>. Calling it
84             will return a storage object which itself provides C<get()> and C<set()>
85             methods. These operate just like you would expect.
86              
87             =head2 set
88              
89             $bot->store->set( user => { nick => 'gryphon', score => 42 } );
90              
91             =head2 get
92              
93             my $score = $bot->store->get('user')->{score};
94              
95             =head1 PSEUDO SUB-CLASSES
96              
97             Pseudo sub-classes of Bot::IRC::Store should implement the same interface
98             as this plugin. Also, they should call C<register()> to ensure plugins that
99             require storage don't clobber the C<store()> of whatever pseudo sub-class
100             is used.
101              
102             $bot->register('Bot::IRC::Store');
103              
104             =head2 SEE ALSO
105              
106             L<Bot::IRC>
107              
108             =for Pod::Coverage init new
109              
110             =head1 AUTHOR
111              
112             Gryphon Shafer <gryphon@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is Copyright (c) 2016-2050 by Gryphon Shafer.
117              
118             This is free software, licensed under:
119              
120             The Artistic License 2.0 (GPL Compatible)
121              
122             =cut