| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::TeleGramma::Store; | 
| 2 |  |  |  |  |  |  | $App::TeleGramma::Store::VERSION = '0.14'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: Persistent datastore for TeleGramma and plugins | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 1979 | use Mojo::Base -base; | 
|  | 5 |  |  |  |  | 153553 |  | 
|  | 5 |  |  |  |  | 37 |  | 
| 7 | 5 |  |  | 5 |  | 2487 | use Storable qw/store retrieve/; | 
|  | 5 |  |  |  |  | 8332 |  | 
|  | 5 |  |  |  |  | 320 |  | 
| 8 | 5 |  |  | 5 |  | 29 | use Carp qw/croak/; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 209 |  | 
| 9 | 5 |  |  | 5 |  | 374 | use File::Spec::Functions qw/catfile/; | 
|  | 5 |  |  |  |  | 618 |  | 
|  | 5 |  |  |  |  | 2738 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has 'path'; | 
| 12 |  |  |  |  |  |  | has 'dbs' => sub { {} }; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub hash { | 
| 16 | 4 |  |  | 4 | 1 | 2489 | my $self = shift; | 
| 17 | 4 |  | 50 |  |  | 13 | my $db   = shift || die "no db?"; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 4 |  |  |  |  | 10 | $self->check_db_name($db); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 4 | 100 |  |  |  | 13 | if (! $self->dbs->{$db} ) { | 
| 22 | 2 |  |  |  |  | 5 | my $hash = $self->read_db_into_hash($db); | 
| 23 | 2 |  |  |  |  | 93 | $self->dbs->{$db} = $hash; | 
| 24 |  |  |  |  |  |  | } | 
| 25 | 4 |  |  |  |  | 26 | return $self->dbs->{$db}; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub check_db_name { | 
| 29 | 4 |  |  | 4 | 0 | 7 | my $self = shift; | 
| 30 | 4 |  |  |  |  | 6 | my $db   = shift; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 4 | 50 |  |  |  | 24 | if ($db !~ /^[\w\-]+$/) { | 
| 33 | 0 |  |  |  |  | 0 | croak "invalid db name '$db'\n"; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub read_db_into_hash { | 
| 39 | 2 |  |  | 2 | 0 | 4 | my $self = shift; | 
| 40 | 2 |  |  |  |  | 4 | my $db   = shift; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 2 | 50 |  |  |  | 6 | if (! -d $self->path) { | 
| 43 | 0 |  |  |  |  | 0 | croak "no path '" . $self->path . "'?"; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 2 |  |  |  |  | 50 | my $db_file = catfile($self->path, $db); | 
| 47 | 2 | 100 |  |  |  | 59 | if (! -e $db_file) { | 
| 48 | 1 |  |  |  |  | 3 | my $hash = {}; | 
| 49 | 1 |  |  |  |  | 4 | store($hash, $db_file); | 
| 50 | 1 |  |  |  |  | 232 | return $hash; | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 1 |  |  |  |  | 6 | return retrieve($db_file); | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub save { | 
| 57 | 1 |  |  | 1 | 1 | 2 | my $self = shift; | 
| 58 | 1 |  | 50 |  |  | 4 | my $db   = shift || die "no db?"; | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 1 |  |  |  |  | 3 | my $db_file = catfile($self->path, $db); | 
| 61 | 1 |  |  |  |  | 10 | store($self->hash($db), $db_file); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub save_all { | 
| 66 | 1 |  |  | 1 | 1 | 22 | my $self = shift; | 
| 67 | 1 |  |  |  |  | 3 | my @dbs = keys %{ $self->dbs }; | 
|  | 1 |  |  |  |  | 9 |  | 
| 68 | 1 |  |  |  |  | 9 | $self->save($_) foreach @dbs; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | 1; | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | __END__ | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =pod | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =encoding UTF-8 | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head1 NAME | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | App::TeleGramma::Store - Persistent datastore for TeleGramma and plugins | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =head1 VERSION | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | version 0.14 | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | my $store = App::TeleGramma::Store->new(path => "/some/dir"); | 
| 90 |  |  |  |  |  |  | my $hashref1 = $store->hash('mydata-1'); | 
| 91 |  |  |  |  |  |  | $hashref1->{foo} = 'bar'; | 
| 92 |  |  |  |  |  |  | $hashref1->{bar} = 'baz'; | 
| 93 |  |  |  |  |  |  | $store->save('mydata-1');  # persisted | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | my $hashref2 = $store->hash('mydata-2'); # new data structure | 
| 96 |  |  |  |  |  |  | $hashref2->{users} = [ qw/ a b c / ]; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | $store->save_all;  # persist data in both the 'mydata1' hash and the 'mydata2' hash | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 METHODS | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head2 hash | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | Return the hash reference for a named entry in your data store. Note that | 
| 105 |  |  |  |  |  |  | the names become disk filenames, and thus must consist of alphanumeric characters | 
| 106 |  |  |  |  |  |  | or '-' only. | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head2 save | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | Save a named hash to the data store. | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | References are saved using L<Storable> and the limitations in terms of data | 
| 113 |  |  |  |  |  |  | stored can be found in that documenation. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | In general, if you stick with simple hashrefs, arrayrefs and scalars you will | 
| 116 |  |  |  |  |  |  | be fine. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head2 save_all | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Persist all named hashrefs to the store at once. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 AUTHOR | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Justin Hawkins <justin@hawkins.id.au> | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | This software is copyright (c) 2019 by Justin Hawkins <justin@eatmorecode.com>. | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 131 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =cut |