line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::TeleGramma::Store; |
2
|
|
|
|
|
|
|
$App::TeleGramma::Store::VERSION = '0.12'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Persistent datastore for TeleGramma and plugins |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
1644
|
use Mojo::Base -base; |
|
5
|
|
|
|
|
1867
|
|
|
5
|
|
|
|
|
31
|
|
7
|
5
|
|
|
5
|
|
2112
|
use Storable qw/store retrieve/; |
|
5
|
|
|
|
|
7120
|
|
|
5
|
|
|
|
|
296
|
|
8
|
5
|
|
|
5
|
|
30
|
use Carp qw/croak/; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
197
|
|
9
|
5
|
|
|
5
|
|
390
|
use File::Spec::Functions qw/catfile/; |
|
5
|
|
|
|
|
597
|
|
|
5
|
|
|
|
|
2105
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'path'; |
12
|
|
|
|
|
|
|
has 'dbs' => sub { {} }; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub hash { |
16
|
4
|
|
|
4
|
1
|
2078
|
my $self = shift; |
17
|
4
|
|
50
|
|
|
11
|
my $db = shift || die "no db?"; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
|
|
|
11
|
$self->check_db_name($db); |
20
|
|
|
|
|
|
|
|
21
|
4
|
100
|
|
|
|
11
|
if (! $self->dbs->{$db} ) { |
22
|
2
|
|
|
|
|
5
|
my $hash = $self->read_db_into_hash($db); |
23
|
2
|
|
|
|
|
70
|
$self->dbs->{$db} = $hash; |
24
|
|
|
|
|
|
|
} |
25
|
4
|
|
|
|
|
25
|
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
|
|
|
|
20
|
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
|
|
|
|
|
3
|
my $db = shift; |
41
|
|
|
|
|
|
|
|
42
|
2
|
50
|
|
|
|
5
|
if (! -d $self->path) { |
43
|
0
|
|
|
|
|
0
|
croak "no path '" . $self->path . "'?"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
34
|
my $db_file = catfile($self->path, $db); |
47
|
2
|
100
|
|
|
|
40
|
if (! -e $db_file) { |
48
|
1
|
|
|
|
|
3
|
my $hash = {}; |
49
|
1
|
|
|
|
|
4
|
store($hash, $db_file); |
50
|
1
|
|
|
|
|
162
|
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
|
|
|
|
|
7
|
store($self->hash($db), $db_file); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub save_all { |
66
|
1
|
|
|
1
|
1
|
8
|
my $self = shift; |
67
|
1
|
|
|
|
|
3
|
my @dbs = keys %{ $self->dbs }; |
|
1
|
|
|
|
|
2
|
|
68
|
1
|
|
|
|
|
8
|
$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.12 |
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@eatmorecode.com> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This software is copyright (c) 2017 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 |