File Coverage

blib/lib/Sub/Throttler/algo.pm
Criterion Covered Total %
statement 57 61 93.4
branch 21 32 65.6
condition 3 6 50.0
subroutine 16 17 94.1
pod 3 3 100.0
total 100 119 84.0


line stmt bran cond sub pod time code
1             package Sub::Throttler::algo;
2 8     8   3114 use 5.010001;
  8         17  
  8         232  
3 8     8   31 use warnings;
  8         12  
  8         177  
4 8     8   30 use strict;
  8         8  
  8         171  
5 8     8   66 use utf8;
  8         11  
  8         30  
6 8     8   128 use Carp;
  8         9  
  8         8172  
7             our @CARP_NOT = qw( Sub::Throttler );
8              
9             our $VERSION = 'v0.2.3';
10              
11 8     8   38 use Scalar::Util qw( blessed );
  8         11  
  8         510  
12 8     8   32 use List::Util qw( any );
  8         10  
  8         614  
13 8     8   1907 use Sub::Throttler qw( throttle_add );
  8         19  
  8         44  
14              
15              
16 8     8   7566 use constant DEFAULT_KEY => 'default';
  8         11  
  8         3626  
17              
18              
19             sub apply_to {
20 38     38 1 8522 goto &throttle_add;
21             }
22              
23             sub apply_to_functions {
24 3     3 1 20 my ($self, @func) = @_;
25 2 50       39 my %func = map { $_ => DEFAULT_KEY }
  2         13  
26 3         8 map {/::/ms ? $_ : caller().q{::}.$_} @func;
27             $self->apply_to(sub {
28 11     11   19 my ($this, $name) = @_;
29 11 100       39 my $key
    100          
30             = $this ? undef
31             : @func ? $func{$name}
32             : DEFAULT_KEY
33             ;
34 11 100       43 return $key ? {$key=>1} : undef;
35 3         24 });
36 3         18 return $self;
37             }
38              
39             sub apply_to_methods {
40 7     7 1 12 my ($self, $class_or_obj, @func) = @_;
41 7 50 66     45 croak 'require class or object'
42             if ref $class_or_obj && !blessed($class_or_obj);
43 7 50   6   72 croak 'method must not contain ::' if any {/::/ms} @func;
  6         20  
44 7         21 my %func = map { $_ => DEFAULT_KEY } @func;
  6         17  
45 7 50       24 if (1 == @_) {
    100          
46             $self->apply_to(sub {
47 0     0   0 my ($this) = @_;
48 0 0       0 my $key = $this ? DEFAULT_KEY : undef;
49 0 0       0 return $key ? {$key=>1} : undef;
50 0         0 });
51             } elsif (ref $class_or_obj) {
52 4         4 my $obj = $class_or_obj;
53             $self->apply_to(sub {
54 4     4   7 my ($this, $name) = @_;
55 4 50 33     39 my $key
    50          
56             = !$this || !ref $this || $this != $obj ? undef
57             : @func ? $func{$name}
58             : DEFAULT_KEY
59             ;
60 4 50       12 return $key ? {$key=>1} : undef;
61 4         15 });
62             } else {
63 3         12 my $class = $class_or_obj;
64             $self->apply_to(sub {
65 23     23   26 my ($this, $name) = @_;
66 23         62 my $key
67 23 100       27 = !eval {local $SIG{__DIE__}; $this->isa($class)} ? undef
  23 100       187  
68             : @func ? $func{$name}
69             : DEFAULT_KEY
70             ;
71 23 100       80 return $key ? {$key=>1} : undef;
72 3         21 });
73             }
74 7         24 return $self;
75             }
76              
77              
78             1; # Magic true value required at end of module
79             __END__