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