File Coverage

lib/Spoon/Hub.pm
Criterion Covered Total %
statement 51 78 65.3
branch 15 38 39.4
condition 2 11 18.1
subroutine 11 15 73.3
pod 0 10 0.0
total 79 152 51.9


line stmt bran cond sub pod time code
1             package Spoon::Hub;
2 4     4   1790 use Spoon::Base -Base;
  4         8  
  4         39  
3 4     4   4632  
  4     4   8  
  4         134  
  4         21  
  4         8  
  4         7028  
4             const class_id => 'hub';
5             field action => '_default_';
6              
7             field main => -weak;
8             field config_files => [];
9             field all_hooks => [];
10              
11 5     5 0 10 sub new {
12 5         21 $self = super;
13 5         496 $self->init;
14 5         30 $Spoon::Base::HUB = $self;
15             }
16              
17             our $AUTOLOAD;
18 12     12   2443 sub AUTOLOAD {
19 12 50       86 $AUTOLOAD =~ /.*::(.*)/
20             or die "Can't AUTOLOAD '$AUTOLOAD'";
21 12         32 my $class_id = $1;
22 12 50       41 return if $class_id eq 'DESTROY';
23 12         66 field $class_id =>
24             -init => "\$self->load_class('$class_id')";
25 12         4332 $self->$class_id(@_);
26             }
27              
28 0     0 0 0 sub pre_process {}
29 0     0 0 0 sub post_process {}
30              
31 0     0 0 0 sub process {
32 0         0 $self->preload;
33 0         0 my $action = $self->action;
34 0 0       0 die "No plugin for action '$action'"
35             unless defined $self->registry->lookup->action->{$action};
36 0         0 my ($class_id, $method) =
37 0         0 @{$self->registry->lookup->action->{$action}};
38 0   0     0 $method ||= $action;
39 0         0 return $self->$class_id->$method;
40             }
41              
42 0     0 0 0 sub preload {
43 0         0 my $preload = $self->registry->lookup->preload;
44 0         0 map {
45 0         0 $self->load_class($_->[0])
46             } sort {
47 0         0 $b->[1] <=> $a->[1]
48             } map {
49 0         0 my %hash = @{$preload->{$_}}[1..$#{$preload->{$_}}];
  0         0  
  0         0  
50 0   0     0 [$_, $hash{priority} || 0];
51             } keys %$preload;
52 0         0 return $self;
53             }
54              
55 13     13 0 200 sub load_class {
56 13         29 my $class_id = shift;
57 13 50       45 return $self if $class_id eq 'hub';
58 13 50 33     75 return $self->$class_id
59             if $self->can($class_id) and defined $self->{$class_id};
60              
61 13         180 my $class_class = $class_id . '_class';
62              
63 13 0       318 my $class_name = $self->config->can($class_class)
    50          
64             ? $self->config->$class_class
65             : $self->registry_loaded
66             ? $self->registry->lookup->classes->{$class_id}
67             : Carp::confess "Can't find a class for class_id '$class_id'";
68              
69 13 50       896 Carp::confess "No class defined for class_id '$class_id'"
70             unless $class_name;
71 13 100       106 unless ($class_name->can('new')) {
72 8         369 eval "require $class_name";
73 8 50       1956 die $@ if $@;
74             }
75             $self->add_hooks
76 13 100       112 unless $class_id eq 'hooks';
77 13 50       103 my $object = $class_name->new
78             or die "Can't create new '$class_name' object";
79 13   33     187 $class_id ||= $object->class_id;
80 13 50       41 die "No class_id defined for class: '$class_name'\n"
81             unless $class_id;
82 13         75 field $class_id =>
83             -init => "\$self->load_class('$class_id')";
84 13         1326 $self->$class_id($object);
85 13         202 $object->init;
86 13         151 return $object;
87             }
88              
89 10     10 0 19 sub add_hooks {
90 10 50       33 return unless $self->registry_loaded;
91 0 0       0 my $hooks = $self->registry->lookup->{hook}
92             or return;
93 0         0 for my $class_name (keys %$hooks) {
94 0 0       0 next unless $class_name->can('new');
95 0 0       0 $self->add_hook(@$_) for @{$hooks->{$class_name} || []};
  0         0  
96 0         0 delete $hooks->{$class_name};
97             }
98 0 0       0 delete $self->registry->lookup->{hook}
99             if not keys %$hooks;
100             }
101              
102 6     6 0 3144 sub add_hook {
103 6         168 my $hooks = $self->all_hooks;
104 6         256 push @$hooks, $self->hooks->add(@_);
105 6         583 return $hooks->[-1];
106             }
107              
108 2     2 0 5 sub remove_hooks {
109 2         53 my $hooks = $self->all_hooks;
110 2         19 while (@$hooks) {
111 6         16 pop(@$hooks)->unhook;
112             }
113             }
114              
115 11     11 0 67 sub registry_loaded {
116 11 50       73 defined $self->{registry} &&
117             defined $self->{registry}{lookup};
118             }
119              
120 2     2   4 sub DESTROY {
121 2         8 $self->remove_hooks;
122             }
123              
124             __END__