File Coverage

blib/lib/WebDAO/Base.pm
Criterion Covered Total %
statement 251 393 63.8
branch 111 242 45.8
condition 3 13 23.0
subroutine 36 52 69.2
pod 3 9 33.3
total 404 709 56.9


line stmt bran cond sub pod time code
1             package WebDAO::Base;
2             our $VERSION = '0.02';
3              
4             =head1 NAME
5              
6             WebDAO::Base - Base class
7              
8             =head1 SYNOPSIS
9              
10             =head1 DESCRIPTION
11              
12             WebDAO::Base - Base class
13              
14             =cut
15              
16 9     9   48 use Carp;
  9         17  
  9         542  
17 9     9   77 use warnings;
  9         26  
  9         2863  
18              
19             @WebDAO::Base::ISA = qw(Exporter);
20             @WebDAO::Base::EXPORT = qw(mk_attr mk_route _log1 _log2 _log3
21             _log4 _log5 _log6);
22              
23             =head2 mk_attr ( _attr1=>'default value', __attr2=>undef, __attr2=>1)
24              
25             Make accessor for class attribute
26              
27             use WebDAO;
28             mk_attr( _session=>undef, __obj=>undef, __events=>undef);
29              
30              
31             =cut
32              
33             sub mk_attr {
34 41     41 1 1402 my ($pkg) = caller;
35 41 100 100     275 shift if $_[0] =~ /\:\:/ or $_[0] eq $pkg;
36 41         156 my %attrs = @_;
37 41         115 %{"${pkg}::_WEBDAO_ATTRIBUTES_"} = %attrs;
  41         1294  
38 41         82 my $code = "";
39 41         119 foreach my $attr ( keys %attrs ) {
40              
41             # If the accessor is already present, give a warning
42 172 50       1218 if ( UNIVERSAL::can( $pkg, "$attr" ) ) {
43 0         0 carp "$pkg already has method: $attr";
44 0         0 next;
45             }
46 172         402 $code .= _define_attr_accessor( $pkg, $attr, $attrs{$attr} );
47             }
48 41 50   16 0 16156 eval $code;
  16 100   15 0 30  
  16 50   7 0 41  
  4 50   10 0 16  
  4 100   2 0 19  
  4 50   1 0 10  
  4 100   1   13  
  12 50   0   34  
  0 0   2   0  
  12 100   1   71  
  15 50   2   31  
  15 0   2   42  
  4 50   1   14  
  4 100   2   16  
  4 50   2   9  
  4 50   0   13  
  11 50   18   33  
  0 0   21   0  
  11 50   6   33  
  7 50   32   15  
  7 0   0   28  
  7 0   0   23  
  4 0   4   19  
  7 0   6   15  
  7 50   7   24  
  0 100   2   0  
  0 50   3   0  
  0 50   2   0  
  10 50   1   21  
  10 0   0   32  
  10 50   4   47  
  4 100   1   20  
  10 50   8   19  
  10 50   5   26  
  0 100   2   0  
  0 50       0  
  0 50       0  
  2 50       2  
  2 0       6  
  1 50       4  
  1 100       4  
  1 50       2  
  1 50       3  
  1 100       10  
  0 50       0  
  1 0       3  
  1 0       3  
  1 0       4  
  1 50       3  
  1 100       4  
  1 50       3  
  1 50       12  
  0 100       0  
  0 50       0  
  0 50       0  
  1 100       3  
  1 100       4  
  1 50       3  
  1 100       4  
  1 50       2  
  1 0       9  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 50       0  
  0 100       0  
  0 100       0  
  0 50       0  
  0 100       0  
  0 100       0  
  0 50       0  
  2 100       4  
  2 50       97  
  1 50       5  
  1 50       5  
  1 0       2  
  1 50       3  
  1 100       4  
  0 50       0  
  1 50       3  
  1 100       2  
  1 50       4  
  1 50       86  
  1 50       5  
  1 0       2  
  1 0       5  
  0 0       0  
  0 0       0  
  0 50       0  
  2 50       5  
  2 0       6  
  1 50       3  
  1 50       3  
  1 0       2  
  1 50       3  
  1 100       4  
  0 100       0  
  1 50       3  
  2 100       4  
  2 100       5  
  1 50       4  
  1 100       3  
  1 50       3  
  1         2  
  1         3  
  0         0  
  1         3  
  1         2  
  1         3  
  1         9  
  1         4  
  1         3  
  1         3  
  0         0  
  0         0  
  0         0  
  2         4  
  2         5  
  1         5  
  1         5  
  1         2  
  1         3  
  1         4  
  0         0  
  1         6  
  2         4  
  2         6  
  1         3  
  1         3  
  1         3  
  1         2  
  1         3  
  0         0  
  1         4  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  18         30  
  18         48  
  12         35  
  12         41  
  12         22  
  12         30  
  6         14  
  0         0  
  6         20  
  21         57  
  21         58  
  12         46  
  12         35  
  12         30  
  12         26  
  9         26  
  0         0  
  9         60  
  6         11  
  6         16  
  1         4  
  1         6  
  1         4  
  1         3  
  5         10  
  3         13  
  2         8  
  32         60  
  32         84  
  12         63  
  12         60  
  12         29  
  12         32  
  20         44  
  0         0  
  20         78  
  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  
  4         7  
  4         12  
  1         4  
  1         5  
  1         3  
  1         3  
  3         11  
  1         7  
  2         8  
  6         9  
  6         17  
  2         9  
  2         8  
  2         4  
  2         5  
  4         10  
  3         19  
  1         5  
  7         13  
  7         26  
  3         8  
  3         12  
  3         7  
  3         7  
  4         16  
  4         22  
  0         0  
  2         4  
  2         7  
  2         5  
  2         8  
  2         6  
  2         4  
  0         0  
  0         0  
  0         0  
  3         6  
  3         7  
  1         4  
  1         5  
  1         3  
  1         2  
  2         11  
  0         0  
  2         17  
  2         4  
  2         6  
  1         4  
  1         3  
  1         3  
  1         2  
  1         4  
  1         7  
  0         0  
  1         2  
  1         4  
  1         4  
  1         4  
  1         2  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  4         10  
  4         19  
  4         20  
  4         20  
  4         11  
  4         11  
  0         0  
  0         0  
  0         0  
  1         2  
  1         4  
  1         12  
  1         5  
  1         3  
  1         3  
  0         0  
  0         0  
  0         0  
  8         16  
  8         20  
  3         9  
  3         17  
  3         6  
  3         7  
  5         21  
  3         60  
  2         10  
  5         11  
  5         18  
  2         7  
  2         8  
  2         4  
  2         5  
  3         11  
  1         22  
  2         5  
  2         3  
  2         14  
  1         12  
  1         77  
  1         3  
  1         55  
  1         4  
  0         0  
  1         11  
49 41 50       224 if ($@) {
50 0         0 die "ERROR defining and attributes for '$pkg':"
51             . "\n\t$@\n"
52             . "-----------------------------------------------------"
53             . $code;
54             }
55             }
56              
57             =head2 mk_route ( 'route1'=> 'Class::Name', 'route2'=> sub { return new My::Class() })
58              
59             Make route table for object
60              
61             use WebDAO;
62             mk_route(
63             user=>'MyClass::User',
64             test=>sub { return MyClass->new( param1=>1 ) }
65             );
66              
67             =cut
68              
69 0         0 sub mk_route {
70 0     0 1 0 my ($pkg) = caller;
71 0 0 0     0 shift if $_[0] =~ /\:\:/ or $_[0] eq $pkg;
72 0         0 my %attrs = @_;
73 9     9   47 no strict 'refs';
  9         12  
  9         1674  
74 0         0 while ( my ( $route, $class ) = each %attrs ) {
75              
76             #check non loaded mods
77 0         0 my ( $main, $module ) = $class =~ m/(.*\:\:)?(\S+)$/;
78 0   0     0 $main ||= 'main::';
79 0         0 $module .= '::';
80 0 0       0 unless ( exists $$main{$module} ) {
81 0         0 _log6("try autoload class $module");
82 0         0 eval "use $class";
83 0 0       0 if ($@) {
84 0         0 carp "Error make route for for class :$class with $@ ";
85             }
86             }
87             }
88 0         0 %{"${pkg}::_WEBDAO_ROUTE_"} = %attrs;
  0         0  
89 9     9   44 use strict 'refs';
  9         15  
  9         5874  
90             }
91              
92             sub _define_attr_accessor {
93 172     172   256 my ( $pkg, $attr, $default ) = @_;
94              
95             # qq makes this block behave like a double-quoted string
96 172         753 my $code = qq{
97             package $pkg;
98             sub $attr { # Accessor ...
99             my \$self=shift;
100             if (\@_) {
101             my \$prev = exists \$self->{"$attr"} ? \$self->{"$attr"} : \${"${pkg}::_WEBDAO_ATTRIBUTES_"}{"$attr"};
102             \$self->{"$attr"} = shift ;
103             return \$prev
104             }
105             return \${"${pkg}::_WEBDAO_ATTRIBUTES_"}{"$attr"} unless exists \$self->{"$attr"};
106             \$self->{"$attr"}
107             }
108             };
109 172         546 $code;
110             }
111              
112              
113             sub new {
114 0     0 1 0 my $class = shift;
115 0         0 my $self = {};
116 0         0 my $stat;
117 0         0 bless( $self, $class );
118 0         0 return $self;
119 0 0       0 return ( $stat = $self->_init(@_) ) ? $self : $stat;
120             }
121              
122             sub _init {
123 0     0   0 my $self = shift;
124 0         0 return 1;
125             }
126              
127             #put message into syslog
128             sub _deprecated {
129 0     0   0 my $self = shift;
130 0         0 my $new_method = shift;
131 0         0 my ( $old_method, $called_from_str, $called_from_method ) =
132             ( ( caller(1) )[3], ( caller(1) )[2], ( caller(2) )[3] );
133 0   0     0 $called_from_method ||= $0;
134 0         0 _log3(
135             "called deprecated method $old_method from $called_from_method at line $called_from_str. Use method $new_method instead."
136             );
137             }
138              
139 0 0   0   0 sub _log1 { shift if ref( $_[0] ); _log( level => 1, par => \@_ ) }
  0         0  
140 0 0   0   0 sub _log2 { shift if ref( $_[0] ); _log( level => 2, par => \@_ ) }
  0         0  
141 0 0   0   0 sub _log3 { shift if ref( $_[0] ); _log( level => 3, par => \@_ ) }
  0         0  
142 0 0   0   0 sub _log4 { shift if ref( $_[0] ); _log( level => 4, par => \@_ ) }
  0         0  
143 0 0   0   0 sub _log5 { shift if ref( $_[0] ); _log( level => 5, par => \@_ ) }
  0         0  
144 0 0   0   0 sub _log6 { shift if ref( $_[0] ); _log( level => 6, par => \@_ ) }
  0         0  
145              
146             sub _log {
147 0   0 0   0 my $dbg_level = $ENV{wdDebug} || $ENV{WD_DEBUG} || 0;
148 0 0       0 return 0 unless $dbg_level;
149 0 0       0 return $dbg_level unless ( scalar @_ );
150 0         0 my %args = @_;
151 0 0       0 return $dbg_level if $dbg_level < $args{level};
152 0         0 my ( $mod_sub, $str ) = ( caller(2) )[ 3, 2 ];
153 0         0 ($str) = ( caller(1) )[2];
154 0         0 print STDERR "$$ [$args{level}] $mod_sub:$str @{$args{par}} \n";
  0         0  
155             }
156              
157             1;
158             __DATA__