blib/lib/App/Stash.pm

filter: Statementstmt 86.1% 62 / 72 Branchbran 42.3% 11 / 26 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 78.6% 96 / 122 CC 15 SCAR 20.1
CC
15
Cov
75%
dao
30.0
new
6.5
clear
2.0
CRAP 7.5
↑ Summary
1 package App::Stash;
2  
3 =head1 NAME
4  
5 App::Stash - persistent application data storage
6  
7 =head1 SYNOPSIS
8  
9 use App::Stash;
10 $stash = App::Stash->new({application => "test"});
11 $stash->data->{'test'} = 1;
12 $stash->d->{'test'} = 1;
13  
14 after new run:
15  
16 use App::Stash;
17 $s=App::Stash->new({application => "test"});
18 print $s->data->{'test'}, "\n";
19 print $s->dao->test, "\n";
20  
21 =head1 WARNING
22  
23 experimental, use on your own risk :-)
24  
25 =head1 DESCRIPTION
26  
27 The purpose of the module is to transparently save stash data (structure)
28 across application (script) execution. The save is done in L</DESTROY>
29 method. This has certain limitations. Basically make sure you never store
30 object in the L</data> as this one may get destroyed before L<App::Stash>
31 object does.
32  
33 The module+style is inspired by L<App::Cache>. Unlike L<App::Cache> it uses
34 L<JSON::Util> for storage and not L<Storable>. The stash is saved to
35 F<$HOME/.app-name/stash.json>. It is in the "pretty" format so it should be
36 easy to read and edit. I wanted to go with L<Storable> but using it in
37 DESTROY method causes C<Segmentation fault> on my Perl.
38  
39 Warn: no file locking in place, use L<Proc::PID::File> or similar to have just one
40 instance of program running or send a wish list bug report and wait for
41 implementation of stash file locking. :)
42  
43 =cut
44  
45 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 our $VERSION = '0.02';
49  
50 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use JSON::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55  
56 2 ▶use base qw( Class::Accessor::Chained::Fast );
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 __PACKAGE__->mk_accessors(qw( application directory stash_filename ));
58  
59 =head1 PROPERTIES
60  
61 application
62 directory
63 stash_filename
64  
65 See L<App::Cache/new> for a description of C<application> and C<directory>.
66 C<stash_filename> is the full path to the file where stash data will be
67 stored. All three are optional.
68  
69 =head1 METHODS
70  
71 =head2 new()
72  
73 Object constructor.
74  
75 =cut
76  
77 3 ▶sub new {
Subroutine new: called CC=6 SCAR=18.7
Pod documented
78 3 my $class = shift;
79 3 my $self = $class->SUPER::new(@_);
80  
81 3 ▶ unless ( $self->application ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->application) 3 0 3
82 3 my $caller = (caller)[0];
83 3 $self->application($caller);
84 }
85  
86 3 ▶ unless ( $self->directory ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->directory) 3 0 3
87 3 my $dir = dir( home(), "." . $self->_clean( $self->application ));
88 3 $self->directory($dir);
89 }
90 3 my $dir = $self->directory;
91 3 ▶ unless ( -d "$dir" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "$dir") 1 2 3
92 1 ▶ mkpath("$dir")
Branch: 1 branch
BranchTrueFalseTotal
mkpath("$dir") or die 'Error mkdiring ' . $self->directory . ": $!" 1 0 1
93 || die "Error mkdiring " . $self->directory . ": $!";
94 }
95  
96 3 ▶ unless ( $self->stash_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->stash_filename) 0 3 3
97 0 my $stash_filename = file($self->directory , "stash.json" )->stringify;
98 0 $self->stash_filename($stash_filename);
99 }
100  
101 3 return $self;
102 }
103  
104 =head2 d
105  
106 Shortcut for L</data>.
107  
108 =head2 data
109  
110 Returns reference to the stash data.
111  
112 =cut
113  
114 2 *d = *data;
115 7 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
Pod documented
116 7 my $self = shift;
117  
118 $self->load
119 7 ▶ if (not $self->{'data'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"} 3 4 7
120  
121 7 return $self->{'data'};
122 }
123  
124 =head2 dao()
125  
126 Returns L</data> passed to L<Data::AsObject/dao>. So basically the
127 data structure becomes an object. See L<Data::AsObject> for details.
128  
129 Note: L<Data::AsObject> is not compile time dependency. It will be used
130 if installed. If not the exception will be thrown only when calling L</dao>.
131 So if you plan to use it, make it a dependency of your module/program.
132  
133 =cut
134  
135 0 ▶sub dao {
Subroutine dao: not called CC=5 SCAR=34.0
Pod documented
136 0 my $self = shift;
137 0 ▶ if (not $INC{'Data/AsObject.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"Data/AsObject.pm"}) 0 0 0
138 0 eval 'use Data::AsObject;';
139 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
140 }
141 0 ▶ if (not $INC{'Storable.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"Storable.pm"}) 0 0 0
142 0 eval 'use Storable;';
143 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
144 }
145 0 return Data::AsObject::dao(Storable::dclone($self->data));
146 }
147  
148 =head2 clear
149  
150 Will delete stash data and remove the file with the stash data from the
151 disk.
152  
153 =cut
154  
155 1 ▶sub clear {
Subroutine clear: called CC=2 SCAR=7.0
Pod documented
156 1 my $self = shift;
157 1 delete $self->{'data'};
158 1 ▶ unlink($self->stash_filename) or die 'failed to unlink '.$self->stash_filename.' - '.$!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->stash_filename or die 'failed to unlink ' . $self->stash_filename . ' - ' . $! 1 0 1
159 1 return;
160
161 }
162  
163 =head2 load
164  
165 Load stash data from disk. Called automatically by first call to L</data>.
166 Can be used to revert current stash data to the state before current execution.
167  
168 =cut
169  
170 3 ▶sub load {
Subroutine load: called CC=2 SCAR=6.9
Pod documented
171 3 my $self = shift;
172 3 ▶ $self->{'data'} = eval { JSON::Util->decode([ $self->stash_filename ]) } || {};
Condition100%2/2
eval { do { 'JSON::Util'->decode([$self->stash_filename]) } } || {}
l!l
12
MC/DC100%1/1
eval { do { 'JSON::Util'->decode([$self->stash_filename]) } } || {}
eval { do { 'JSON::Util'->decode([$self->stash_filename]) } }
Truth table100%2/2
A
A= eval { do { 'JSON::Util'->decode([$self->stash_filename]) } }
Aresult
11
00
173 3 return;
174 }
175  
176 =head2 save
177  
178 Save stash data to disk - F<$HOME/.app-name/stash.json>. Called automatically
179 via DESTROY method when L<App::Stash> object is going to be destroyed.
180  
181 Will throw an exception if the file save fails.
182  
183 =cut
184  
185 4 ▶sub save {
Subroutine save: called CC=2 SCAR=7.0
Pod documented
186 4 my $self = shift;
187  
188 4 eval { JSON::Util->new->encode($self->data, [ $self->stash_filename ]); };
189 4 ▶ die 'failed to save application stash - '.$@
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
190 if $@;
191
192 4 return;
193 }
194  
195 =head2 DESTROY
196  
197 Calls L</save> and prints warning if it fails.
198  
199 =cut
200  
201 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
202 3 my $self = shift;
203  
204 3 eval { $self->save(); };
205 3 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
206 }
207  
208 3 ▶sub _clean {
Subroutine _clean: called CC=1 SCAR=0.0
209 3 my ( $self, $text ) = @_;
210 3 $text = lc $text;
211 3 $text =~ s/[^a-z0-9]+/_/g;
212 3 return $text;
213 }
214  
215 2 1;
216  
217  
218 __END__
↑ Summary