File Coverage

blib/lib/Net/HTTP/Spore/Role/Middleware.pm
Criterion Covered Total %
statement 35 35 100.0
branch 11 12 91.6
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 3 0.0
total 57 62 91.9


line stmt bran cond sub pod time code
1             package Net::HTTP::Spore::Role::Middleware;
2             $Net::HTTP::Spore::Role::Middleware::VERSION = '0.07';
3 21     21   8587 use Moose::Role;
  21         48  
  21         171  
4 21     21   100029 use Class::Load;
  21         45  
  21         1016  
5 21     21   118 use Scalar::Util qw/blessed/;
  21         44  
  21         8538  
6              
7             has middlewares => (
8             is => 'rw',
9             isa => 'ArrayRef',
10             traits => ['Array'],
11             lazy => 1,
12             default => sub { [] },
13             auto_deref => 1,
14             handles => { _add_middleware => 'push', _filter_middlewares => 'grep' },
15             );
16              
17             sub _load_middleware {
18 41     41   159 my ( $self, $mw, $cond, @args ) = @_;
19              
20 41 100       316 Class::Load::load_class($mw) unless blessed($mw);
21              
22 39         1307 my $code = $mw->wrap( $cond, @args );
23 39         420 $self->_trace_msg('== enabling middleware %s', $mw);
24 39         1546 $self->_add_middleware($code);
25             }
26              
27             sub _complete_mw_name {
28 39     39   121 my ($self, $mw) = @_;
29              
30 39 100       229 if ($mw =~ /^\+/) {
    50          
31 1         4 $mw =~ s/^\+//;
32             }
33             elsif ($mw !~ /Net\:\:HTTP\:\:Spore\:\:Middleware/) {
34 38         117 $mw = "Net::HTTP::Spore::Middleware::".$mw;
35             }
36              
37 39         135 return $mw;
38             }
39              
40             sub enable {
41 41     41 0 608 my ($self, $mw, @args) = @_;
42              
43 41 100       167 confess "middleware name is missing" unless $mw;
44              
45 40     39   331 $self->enable_if(sub{1}, $mw, @args);
  39         153  
46 38         128 $self;
47             }
48              
49             sub enable_if {
50 42     42 0 864 my ($self, $cond, $mw, @args) = @_;
51              
52 42 100 66     344 confess "condition must be a code ref" if (!$cond || ref $cond ne 'CODE');
53              
54 41 100       151 if(ref($mw) eq 'CODE'){ # anonymous middleware
55 2         10 Class::Load::load_class('Net::HTTP::Spore::Middleware');
56 2         65 my $anon = Class::MOP::Class->create_anon_class(
57             superclasses => ['Net::HTTP::Spore::Middleware'],
58             methods => {
59             call => $mw
60             }
61             );
62 2         1981 $mw = $anon->new_object;
63             } else {
64 39         292 $mw = $self->_complete_mw_name($mw);
65             }
66 41         622 $self->_load_middleware($mw, $cond, @args);
67 39         93 $self;
68             }
69              
70             sub reset_middlewares {
71 1     1 0 4 my $self = shift;
72 1         48 $self->middlewares([]);
73             }
74              
75             1;
76              
77             __END__
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             Net::HTTP::Spore::Role::Middleware
86              
87             =head1 VERSION
88              
89             version 0.07
90              
91             =head1 AUTHORS
92              
93             =over 4
94              
95             =item *
96              
97             Franck Cuny <franck.cuny@gmail.com>
98              
99             =item *
100              
101             Ash Berlin <ash@cpan.org>
102              
103             =item *
104              
105             Ahmad Fatoum <athreef@cpan.org>
106              
107             =back
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2012 by Linkfluence.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut