File Coverage

blib/lib/Dancer/Engine.pm
Criterion Covered Total %
statement 44 44 100.0
branch 12 12 100.0
condition 8 9 88.8
subroutine 10 10 100.0
pod 2 3 66.6
total 76 78 97.4


line stmt bran cond sub pod time code
1             package Dancer::Engine;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: base class for Dancer engines
4             $Dancer::Engine::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::Engine::VERSION = '1.351404';
6             # This is the base-class of every engine abstract class.
7             # This allow us to put in that single place the engine creation
8             # from a namespace and a name, to its configuration initialization.
9              
10 196     196   2142 use strict;
  196         337  
  196         5072  
11 196     196   981 use warnings;
  196         349  
  196         4273  
12 196     196   842 use Carp;
  196         324  
  196         9595  
13 196     196   3980 use Dancer::ModuleLoader;
  196         392  
  196         4497  
14 196     196   1007 use base 'Dancer::Object';
  196         340  
  196         24681  
15 196     196   1242 use Dancer::Exception qw(:all);
  196         352  
  196         89730  
16              
17             # constructor arguments:
18             # name => $name_of_the_engine
19             # settings => $hash_of_engine_settings
20             Dancer::Engine->attributes(qw(name type));
21              
22             # Accessor to the config hash, it may not be initialized if someone
23             # creates a new engine without giving the appropriate arguments.
24             # e.g. Dancer::Template::Simple->new();
25             sub config {
26 450     450 1 955 my ($self) = @_;
27 450 100       2695 return $self->{config} if defined $self->{config};
28 57         263 $self->{config} = {};
29             }
30              
31             # static method for initializing an engine
32             # this will create an engine instance of the appropriate $type, named $name
33             # if Dancer::$type::$name exists.
34             sub build {
35 442     442 1 1979 my ($class, $type, $name, $config) = @_;
36              
37 442 100 66     2336 raise core_engine => "cannot build engine without type and name "
38             unless $name and $type;
39              
40 441         1446 my $class_name = $class->_engine_class($type);
41              
42 441   100     1458 $config ||= {};
43 441   100     2177 $config->{engines} ||= {};
44 441   100     1881 my $settings = $config->{engines}{$name} || {};
45              
46             # trying to load the engine
47 441         2395 my $engine_class =
48             Dancer::ModuleLoader->class_from_setting($class_name => $name);
49              
50 441         2079 my( $loaded, $error ) = Dancer::ModuleLoader->load($engine_class);
51 441 100       1506 $error = '' unless defined $error;
52              
53 441 100       1192 unless( $loaded ) {
54 6         15 my $tip = '';
55 6 100       41 if( $error =~ /Can't locate (\S+)\.pm in \@INC/ ) {
56 4         12 my $module = $1;
57 4         18 $module =~ s#/#::#g;
58 4         16 $tip = " (perhaps you need to install $module?)";
59             }
60              
61 6 100       31 $error = ": $error" if length $error;
62 6         43 raise core_engine => "unable to load $type engine '$name'$tip$error";
63             }
64              
65             # creating the engine
66 435         3483 return $engine_class->new(
67             name => $name,
68             type => $type,
69             config => $settings,
70             );
71             }
72              
73             sub _engine_class {
74 491     491   1159 my ($class, $type) = @_;
75 491         1427 $type = ucfirst($type);
76 491         1526 return "Dancer::${type}";
77             }
78              
79             sub engine {
80 50     50 0 105 my ($class, $type) = @_;
81 50         121 return $class->_engine_class($type)->engine();
82             }
83              
84              
85             1;
86              
87             __END__