File Coverage

blib/lib/Thunderhorse/Router/SpecializedCache.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 11 11 100.0


line stmt bran cond sub pod time code
1             package Thunderhorse::Router::SpecializedCache;
2             $Thunderhorse::Router::SpecializedCache::VERSION = '0.102';
3 1     1   978948 use v5.40;
  1         3  
4 1     1   4 use Mooish::Base -standard, -role;
  1         2  
  1         8  
5              
6 1     1   11715 use Gears::X::Thunderhorse;
  1         115  
  1         954  
7              
8             requires qw(
9             get
10             set
11             );
12              
13             has field 'router' => (
14             isa => InstanceOf ['Thunderhorse::Router'],
15             writer => 1,
16             weak_ref => 1,
17             );
18              
19             # store needs to clone
20             my sub store ($matches)
21             {
22             return undef unless defined $matches;
23              
24             my @new_matches = $matches->@*;
25             foreach my $match (@new_matches) {
26             if ($match isa 'Gears::Router::Match') {
27             $match = {
28             _match_class => ref $match,
29             location => $match->location->name,
30             matched => $match->matched,
31             };
32             }
33             elsif (ref $match eq 'ARRAY') {
34             $match = __SUB__->($match);
35             }
36             }
37              
38             return \@new_matches;
39             }
40              
41             my sub retrieve ($router, $matches)
42             {
43             return undef unless defined $matches;
44              
45             my @new_matches = $matches->@*;
46             foreach my $match (@new_matches) {
47             if (ref $match eq 'HASH' && $match->{_match_class}) {
48             my %args = $match->%*;
49             my $class = delete $args{_match_class};
50             my $location = $args{location};
51             $args{location} = $router->find($location);
52              
53             # locations must be created in a deterministic order - otherwise,
54             # this error may occur
55             Gears::X::Thunderhorse->raise("invalid cached location $location")
56             unless $args{location};
57             $match = bless \%args, $class;
58             }
59             elsif (ref $match eq 'ARRAY') {
60             $match = __SUB__->($router, $match);
61             }
62             }
63              
64             return \@new_matches;
65             }
66              
67             around set => sub ($orig, $self, $key, $value, @args) {
68             return $self->$orig($key, store($value), @args);
69             };
70              
71             around get => sub ($orig, $self, @args) {
72             return retrieve($self->router, $self->$orig(@args));
73             };
74