File Coverage

blib/lib/StorageDisplay/Moose/Cached.pm
Criterion Covered Total %
statement 52 52 100.0
branch 6 8 75.0
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 70 73 95.8


line stmt bran cond sub pod time code
1             #
2             # This file is part of StorageDisplay
3             #
4             # This software is copyright (c) 2014-2023 by Vincent Danjean.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 9     9   63 use strict;
  9         23  
  9         349  
10 9     9   66 use warnings;
  9         19  
  9         824  
11              
12             package StorageDisplay::Moose::Cached;
13             # ABSTRACT: Moose extension for StorageDisplay
14              
15             our $VERSION = '2.06'; # VERSION
16              
17 9     9   62 use Carp;
  9         17  
  9         1212  
18              
19             our %orig_has; # save original 'has' sub routines here
20              
21             sub import {
22 18     18   127 my $callpkg = caller 0;
23             {
24 9     9   59 no strict 'refs'; ## no critic
  9         18  
  9         434  
  18         76  
25 9     9   45 no warnings 'redefine';
  9         18  
  9         8487  
26 18         75 $orig_has{$callpkg} = *{$callpkg."::has"}{CODE};
  18         410  
27 18         105 *{$callpkg."::has"} = \&cached_has;
  18         175  
28             }
29 18         4903 return;
30             }
31              
32             sub cached_has {
33 63     63 0 582 my ($attr, %args) = @_;
34              
35 63         220 my $callpkg = caller 0;
36 63 100       323 if (exists $args{cached_hash} ) {
37 18         56 my $compute = $args{compute};
38 18         62 my $type = $args{cached_hash};
39 18 50       124 croak "'compute' attribute required" if not exists $args{compute};
40 18         61 my $cache_set = '_cached_set_'.$attr;
41 18         47 my $cache_has = '_cached_has_'.$attr;
42 18         49 my $cache_get = '_cached_get_'.$attr;
43 18         87 $args{handles}->{$cache_set} = 'set';
44 18         77 $args{handles}->{$cache_has} = 'exists';
45 18         73 $args{handles}->{$cache_get} = 'get';
46             %args = (
47             is => 'bare',
48             required => 1,
49 18     18   893 default => sub { return {}; },
50 18         373 lazy => 1,
51             init_arg => undef, # prevent from being set by constructor
52             %args,
53             traits => [ 'Hash' ],
54             isa => "HashRef[$type]",
55             );
56 18         54 delete $args{cached_hash};
57 18         56 delete $args{compute};
58             #print STDERR "My cached arg $attr\n";
59             $callpkg->meta->add_method(
60             $attr => sub {
61 176     176   372 my $self = shift;
        176      
        176      
62 176         406 my $name = shift;
63              
64 176 100       9202 if ($self->$cache_has($name)) {
65 80         4106 return $self->$cache_get($name);
66             }
67 96         543 my $elem = $compute->($self, $name, @_);
68 96 50       379 if (defined($elem)) {
69 96         5388 $self->$cache_set($name, $elem);
70             }
71 96         610 return $elem;
72              
73 18         141 });
74             }
75 63         1964 $orig_has{$callpkg}->($attr, %args);
76             }
77              
78             BEGIN {
79             # Mark current package as loaded;
80 9     9   39 my $p = __PACKAGE__;
81 9         55 $p =~ s,::,/,g;
82 9         111034 chomp(my $cwd = `pwd`);
83 9         1398 $INC{$p.'.pm'} = $cwd.'/'.__FILE__;#k"current file";
84             }
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             StorageDisplay::Moose::Cached - Moose extension for StorageDisplay
97              
98             =head1 VERSION
99              
100             version 2.06
101              
102             =head1 AUTHOR
103              
104             Vincent Danjean <Vincent.Danjean@ens-lyon.org>
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             This software is copyright (c) 2014-2023 by Vincent Danjean.
109              
110             This is free software; you can redistribute it and/or modify it under
111             the same terms as the Perl 5 programming language system itself.
112              
113             =cut