File Coverage

blib/lib/Blosxom/Component.pm
Criterion Covered Total %
statement 17 52 32.6
branch 1 14 7.1
condition 0 7 0.0
subroutine 5 8 62.5
pod 3 4 75.0
total 26 85 30.5


line stmt bran cond sub pod time code
1             package Blosxom::Component;
2 1     1   902 use strict;
  1         2  
  1         24  
3 1     1   5 use warnings;
  1         2  
  1         21  
4 1     1   4 use Carp qw/croak/;
  1         1  
  1         253  
5              
6             my ( %attribute_of, %requires, %component_of );
7              
8             sub load_components {
9 0     0 0 0 my ( $class, @components ) = @_;
10 0   0     0 push @{ $component_of{$class} ||= [] }, @_;
  0         0  
11             }
12              
13             sub requires {
14 0     0 1 0 my ( $class, @methods ) = @_;
15 0   0     0 push @{ $requires{$class} ||= [] }, @methods;
  0         0  
16             }
17              
18             sub mk_accessors {
19 1     1 1 2 my $class = shift;
20 1         4 while ( @_ ) {
21 1         1 my $field = shift;
22 1 50       5 my $default = ref $_[0] eq 'CODE' ? shift : undef;
23 1         5 $attribute_of{ $class }{ $field } = $default;
24             }
25             }
26              
27             sub init {
28 0     0 1   my $class = shift;
29 0           my $caller = shift;
30 1     1   7 my $stash = do { no strict 'refs'; \%{"$class\::"} };
  1         2  
  1         359  
  0            
  0            
  0            
31              
32 0 0         if ( my $components = $component_of{$class} ) {
33 0           my @args = @{ $components };
  0            
34 0           while ( @args ) {
35 0           my $component = shift;
36 0 0         my $config = ref $args[0] eq 'HASH' ? shift @args : undef;
37 0           $caller->add_component( $component => $config );
38             }
39             }
40              
41 0 0         if ( my $requires = $requires{$class} ) {
42 0 0         if ( my @methods = grep { !$caller->can($_) } @{$requires} ) {
  0            
  0            
43 0           my $methods = join ', ', @methods;
44 0           croak "Can't apply '$class' to '$caller' - missing $methods";
45             }
46             }
47              
48 0 0         if ( my $attribute = $attribute_of{$class} ) {
49 0           while ( my ($field, $default) = each %{$attribute} ) {
  0            
50 0           $caller->add_attribute( $field, $default );
51             }
52             }
53              
54             # NOTE: use keys() instead
55 0           while ( my ($name, $glob) = each %{$stash} ) {
  0            
56 0 0 0       if ( defined *{$glob}{CODE} and $name ne 'init' ) {
  0            
57 0           $caller->add_method( $name => *{$glob}{CODE} );
  0            
58             }
59             }
60              
61 0           return;
62             }
63              
64             1;
65              
66             __END__