File Coverage

blib/lib/Eidolon/Core/Loader.pm
Criterion Covered Total %
statement 24 28 85.7
branch 4 10 40.0
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 37 47 78.7


line stmt bran cond sub pod time code
1             package Eidolon::Core::Loader;
2             # ==============================================================================
3             #
4             # Eidolon
5             # Copyright (c) 2009, Atma 7
6             # ---
7             # Eidolon/Core/Loader.pm - driver loader
8             #
9             # ==============================================================================
10              
11 2     2   4554 use Eidolon::Driver::Exceptions;
  2         5  
  2         76  
12 2     2   10 use warnings;
  2         3  
  2         38  
13 2     2   47 use strict;
  2         4  
  2         559  
14              
15             our $VERSION = "0.02"; # 2009-05-13 22:32:29
16              
17             # ------------------------------------------------------------------------------
18             # \% new()
19             # constructor
20             # ------------------------------------------------------------------------------
21             sub new
22             {
23 1     1 1 1012 my ($class, $self);
24              
25 1         2 $class = shift;
26              
27             # class attributes
28 1         5 $self = { "type_cache" => {} };
29              
30 1         3 bless $self, $class;
31            
32 1         3 return $self;
33             }
34              
35             # ------------------------------------------------------------------------------
36             # load($class, @params)
37             # load a driver
38             # ------------------------------------------------------------------------------
39             sub load
40             {
41 1     1 1 4 my ($self, $class, @params, $r);
42              
43 1         3 ($self, $class, @params) = @_;
44 1         7 $r = Eidolon::Core::Registry->get_instance;
45            
46             # loading driver
47 1         66 eval "require $class";
48 1 50       6 throw CoreError::Compile($@) if $@;
49            
50 1 50       11 throw CoreError::Loader::InvalidDriver($class) if !$class->isa("Eidolon::Driver");
51 1 50       9 throw CoreError::Loader::AlreadyLoaded($class) if exists $self->{"type_cache"}->{$class};
52              
53 1         4 $self->{"type_cache"}->{$class} = $class->new( @params );
54             }
55              
56             # ------------------------------------------------------------------------------
57             # \% get_object($class)
58             # get driver object
59             # ------------------------------------------------------------------------------
60             sub get_object
61             {
62 1     1 1 5 my ($self, $class) = @_;
63              
64             # find object by class
65 1 50       18 return $self->{"type_cache"}->{$class} if (exists $self->{"type_cache"}->{$class});
66              
67             # find driver by parent class
68 0           foreach (keys %{ $self->{"type_cache"} })
  0            
69             {
70 0 0         return $self->{"type_cache"}->{$_} if ($_->isa($class));
71             }
72              
73 0           return undef;
74             }
75              
76             1;
77              
78             __END__