File Coverage

blib/lib/RapidApp/Handler.pm
Criterion Covered Total %
statement 22 22 100.0
branch 9 14 64.2
condition 4 6 66.6
subroutine 5 5 100.0
pod 0 2 0.0
total 40 49 81.6


line stmt bran cond sub pod time code
1             package RapidApp::Handler;
2 6     6   40 use Moose;
  6         14  
  6         40  
3              
4 6     6   39601 use RapidApp::Util qw(:all);
  6         15  
  6         4289  
5              
6             has 'scope' => ( is => 'rw', default => undef, isa => 'Maybe[Object]' );
7             has 'method' => ( is => 'rw', default => undef, isa => 'Maybe[Str]' );
8             has 'code' => ( is => 'rw', default => undef, isa => 'Maybe[CodeRef]' );
9              
10             sub BUILD {
11 1030     1030 0 1542910 my $self = shift;
12            
13 1030 50 66     31094 die 'neither code nor method supplied.' unless (defined $self->method or defined $self->code);
14 1030 50 66     24654 die 'method and code cannot be used together' if (defined $self->method and defined $self->code);
15            
16 1030 100       23807 if (defined $self->method) {
17 718 50       17463 die 'scope is required with method' unless (defined $self->scope);
18 718 50       16221 $self->scope->can($self->method) or die ref($self->scope) . ' does not have a method named "' . $self->method . '"';
19             }
20             }
21              
22             sub call {
23 215     215 0 401 my $self = shift;
24            
25 215         467 my @args = @_;
26            
27 215 100       5234 return $self->_call_coderef(@args) if (defined $self->code);
28 194         4811 my $method = $self->method;
29 194         4616 return $self->scope->$method(@args);
30             }
31              
32             sub _call_coderef {
33 21     21   52 my $self = shift;
34 21         50 my @arg = ();
35 21 50       515 push @arg, $self->scope if (defined $self->scope);
36 21         89 push @arg, @_;
37 21         502 return $self->code->(@arg);
38             }
39              
40              
41             1;