File Coverage

blib/lib/App/PerlWatcher/Util/Storable.pm
Criterion Covered Total %
statement 52 52 100.0
branch 3 4 75.0
condition 3 6 50.0
subroutine 10 10 100.0
pod 0 2 0.0
total 68 74 91.8


line stmt bran cond sub pod time code
1             package App::PerlWatcher::Util::Storable;
2             {
3             $App::PerlWatcher::Util::Storable::VERSION = '0.20';
4             }
5             # ABSTRACT: Used to freeze/thaw PerlWatcher status (watcher memories and shelf of statuses)
6              
7 4     4   59 use 5.12.0;
  4         14  
  4         188  
8 4     4   28 use strict;
  4         8  
  4         135  
9 4     4   22 use warnings;
  4         6  
  4         159  
10              
11 4     4   35 use Smart::Comments -ENV;
  4         7  
  4         48  
12 4     4   5834 use Storable;
  4         12  
  4         324  
13              
14 4     4   24 use App::PerlWatcher::Engine;
  4         7  
  4         116  
15 4     4   3767 use App::PerlWatcher::Watcher;
  4         14  
  4         190  
16              
17 4     4   37 use parent qw/Exporter/;
  4         8  
  4         65  
18             our @EXPORT_OK = qw/freeze thaw/;
19              
20             local our %Watchers_Pool;
21              
22             sub freeze {
23 5     5 0 450 my ($engine) = @_;
24 8         74 my %watchers_memories =
25 5         154 map { $_ => $_->memory }
26 5         14 @{ $engine->watchers };
27              
28 5   50     160 my $stored_items = {
29             version => $App::PerlWatcher::Engine::VERSION // 'dev',
30             shelf => $engine->shelf,
31             watchers_memories => \%watchers_memories,
32             };
33 5         28 return Storable::freeze($stored_items);
34             }
35              
36             # return true on success
37             sub thaw {
38 6     6 0 200 my ($engine, $serialized) = @_;
39 6         126 my $watchers = $engine->watchers;
40 6         18 local our %Watchers_Pool;
41 6         24 @Watchers_Pool{ @$watchers } = @$watchers;
42              
43 6         43 my $stored_items = eval { Storable::thaw($serialized) };
  6         33  
44 6 100       311 return 0 if $@;
45              
46 5   50     25 my $version = $stored_items->{version} // 'dev';
47 5 50 50     28 return 0
48             unless $version eq ($App::PerlWatcher::Engine::VERSION // 'dev');
49              
50 5         9 my %watchers_memories = %{ $stored_items->{watchers_memories} };
  5         21  
51 5         12 my $shelf = $stored_items->{shelf};
52              
53             my @actual_watcher_ids
54 5         12 = grep { $Watchers_Pool{$_} } keys %watchers_memories;
  8         123  
55              
56             $Watchers_Pool{$_}->memory($watchers_memories{$_})
57 5         154 for(@actual_watcher_ids);
58              
59 5         21 my $statuses = $shelf->statuses;
60 5         10 my $actual_statuses = {};
61 5         9 @$actual_statuses{ @actual_watcher_ids } = @{$statuses}{ @actual_watcher_ids };
  5         15  
62 5         18 $shelf->statuses($actual_statuses);
63              
64 5         4771 $engine->shelf($shelf);
65 5         429 return 1;
66             }
67              
68             1;
69              
70             __END__