File Coverage

blib/lib/Sentry/Util.pm
Criterion Covered Total %
statement 51 52 98.0
branch 7 12 58.3
condition 4 7 57.1
subroutine 15 15 100.0
pod 0 5 0.0
total 77 91 84.6


line stmt bran cond sub pod time code
1             package Sentry::Util;
2 10     10   346324 use Mojo::Base -strict, -signatures;
  10         15  
  10         78  
3              
4 10     10   2736 use Exporter qw(import);
  10         19  
  10         417  
5 10     10   4872 use Mojo::Loader qw(load_class);
  10         131683  
  10         830  
6 10     10   68 use Mojo::Util qw(dumper monkey_patch);
  10         17  
  10         529  
7 10     10   8419 use UUID::Tiny ':std';
  10         196545  
  10         13277  
8              
9             our @EXPORT_OK = qw(uuid4 truncate merge around restore_original);
10              
11             sub uuid4 {
12 81     81 0 183084 my $uuid = create_uuid_as_string(UUID_V4);
13 81         11816 $uuid =~ s/-//g;
14 81         756 return $uuid;
15             }
16              
17 13     13 0 4612 sub truncate ($string, $max = 0) {
  13         24  
  13         17  
  13         18  
18 13 50 33     67 return $string if (ref($string) || $max == 0);
19              
20 13 100       48 return length($string) <= $max ? $string : substr($string, 0, $max) . '...';
21             }
22              
23 82     82 0 3869 sub merge ($target, $source, $key) {
  82         103  
  82         93  
  82         105  
  82         87  
24             $target->{$key}
25 82   100     533 = { ($target->{$key} // {})->%*, ($source->{$key} // {})->%* };
      50        
26             }
27              
28             my %Patched = ();
29              
30 16     16 0 3876 sub around ($package, $method, $cb) {
  16         31  
  16         28  
  16         63  
  16         30  
31 16         39 my $key = $package . '::' . $method;
32 16 100       67 return if $Patched{$key};
33              
34 14 50       131 if (my $e = load_class $package) {
35 0 0       0 die ref $e ? "Exception: $e" : "Module $package not found";
36             }
37              
38 14         188126 my $orig = $package->can($method);
39              
40 14     13   130 monkey_patch $package, $method => sub { $cb->($orig, @_) };
  13     5   43076  
        8      
        5      
        2      
41              
42 14         303 $Patched{$key} = $orig;
43              
44 14         58 return;
45             }
46              
47 3     3 0 13229 sub restore_original ($package, $method) {
  3         7  
  3         7  
  3         7  
48 3         8 my $key = $package . '::' . $method;
49 3 50       14 my $orig = $Patched{$key} or return;
50 3         15 monkey_patch $package, $method, $orig;
51 3         105 delete $Patched{$key};
52             }
53              
54             1;