File Coverage

blib/lib/Mojo/Webqq/Base.pm
Criterion Covered Total %
statement 51 67 76.1
branch 17 36 47.2
condition 6 15 40.0
subroutine 22 242 9.0
pod 0 3 0.0
total 96 363 26.4


line stmt bran cond sub pod time code
1             package Mojo::Webqq::Base;
2 1     1   7 use strict;
  1         2  
  1         31  
3 1     1   6 use warnings;
  1         2  
  1         32  
4 1     1   5 use feature ();
  1         2  
  1         12  
5            
6             # No imports because we get subclassed, a lot!
7 1     1   5 use Carp ();
  1         1  
  1         13  
8            
9             # Only Perl 5.14+ requires it on demand
10 1     1   662 use IO::Handle ();
  1         6703  
  1         104  
11            
12             # Supported on Perl 5.22+
13             my $NAME
14             = eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };
15            
16             # Protect subclasses using AUTOLOAD
17       0     sub DESTROY { }
18            
19             # Declared here to avoid circular require problems in Mojo::Util
20             sub _monkey_patch {
21 224     224   627 my ($class, %patch) = @_;
22 1     1   12 no strict 'refs';
  1         2  
  1         43  
23 1     1   7 no warnings 'redefine';
  1         3  
  1         650  
24 224         1438 *{"${class}::$_"} = $NAME->("${class}::$_", $patch{$_}) for keys %patch;
  224         1343  
25             }
26            
27             sub attr {
28 133     133 0 244 my ($self, $attrs, $value) = @_;
29 133 50 33     592 return unless (my $class = ref $self || $self) && $attrs;
      33        
30            
31 133 50 66     301 Carp::croak 'Default has to be a code reference or constant value'
32             if ref $value && ref $value ne 'CODE';
33            
34 133 100       164 for my $attr (@{ref $attrs eq 'ARRAY' ? $attrs : [$attrs]}) {
  133         357  
35 214 50       743 Carp::croak qq{Attribute "$attr" invalid} unless $attr =~ /^[a-zA-Z_]\w*$/;
36            
37             # Very performance sensitive code with lots of micro-optimizations
38 214 100       433 if (ref $value) {
    100          
39             _monkey_patch $class, $attr, sub {
40             return
41 0 0   0   0 exists $_[0]{$attr} ? $_[0]{$attr} : ($_[0]{$attr} = $value->($_[0]))
    0   0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
42             if @_ == 1;
43 0         0 $_[0]{$attr} = $_[1];
44 0         0 $_[0];
45 35         143 };
46             }
47             elsif (defined $value) {
48             _monkey_patch $class, $attr, sub {
49 0 0   0   0 return exists $_[0]{$attr} ? $_[0]{$attr} : ($_[0]{$attr} = $value)
    0   0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
50             if @_ == 1;
51 0         0 $_[0]{$attr} = $_[1];
52 0         0 $_[0];
53 66         319 };
54             }
55             else {
56             _monkey_patch $class, $attr,
57 113 0   0   424 sub { return $_[0]{$attr} if @_ == 1; $_[0]{$attr} = $_[1]; $_[0] };
  0     0   0  
  0     0   0  
  0     0   0  
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
58             }
59             }
60             }
61            
62             sub import {
63 25     25   50 my $class = shift;
64 25 100       6471 return unless my $flag = shift;
65            
66             # Base
67 10 100 66     109 if ($flag eq '-base') { $flag = $class }
  2 50       5  
    100          
68            
69             # Strict
70 0         0 elsif ($flag eq '-strict') { $flag = undef }
71            
72             # Module
73             elsif ((my $file = $flag) && !$flag->can('new')) {
74 2         25 $file =~ s!::|'!/!g;
75 2         930 require "$file.pm";
76             }
77            
78             # ISA
79 10 50       190761 if ($flag) {
80 10         24 my $caller = caller;
81 1     1   9 no strict 'refs';
  1         2  
  1         301  
82 10         17 push @{"${caller}::ISA"}, $flag;
  10         118  
83 10     133   70 _monkey_patch $caller, 'has', sub { attr($caller, @_) };
  133     133   260  
        133      
        133      
        133      
        133      
        133      
        133      
        133      
        133      
        133      
84             }
85            
86             # Mojo modules are strict!
87 10         135 $_->import for qw(strict warnings);
88 10         4504 feature->import(':5.10');
89             }
90            
91             sub new {
92 0     0 0   my $class = shift;
93 0 0 0       bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
  0 0          
94             }
95            
96             sub tap {
97 0     0 0   my ($self, $cb) = (shift, shift);
98 0           $_->$cb(@_) for $self;
99 0           return $self;
100             }
101            
102             1;