| 1 | package Dancer::Engine; | ||||||||||||||||||||||||||||
| 2 | 251 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: base class for Dancer engines | ||||||||||||||||||||||||||||
| 4 | 251 | $Dancer::Engine::VERSION = '1.3522'; | |||||||||||||||||||||||||||
| 5 | # This is the base-class of every engine abstract class. | ||||||||||||||||||||||||||||
| 6 | # This allow us to put in that single place the engine creation | ||||||||||||||||||||||||||||
| 7 | # from a namespace and a name, to its configuration initialization. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 251 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 251 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 251 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 251 | ▶ | use Dancer::ModuleLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 251 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 251 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # constructor arguments: | ||||||||||||||||||||||||||||
| 17 | # name => $name_of_the_engine | ||||||||||||||||||||||||||||
| 18 | # settings => $hash_of_engine_settings | ||||||||||||||||||||||||||||
| 19 | 251 | Dancer::Engine->attributes(qw(name type)); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Accessor to the config hash, it may not be initialized if someone | ||||||||||||||||||||||||||||
| 22 | # creates a new engine without giving the appropriate arguments. | ||||||||||||||||||||||||||||
| 23 | # e.g. Dancer::Template::Simple->new(); | ||||||||||||||||||||||||||||
| 24 | 606 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 606 | my ($self) = @_; | |||||||||||||||||||||||||||
| 26 | 606 | ▶ | return $self->{config} if defined $self->{config}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 165 | $self->{config} = {}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # static method for initializing an engine | ||||||||||||||||||||||||||||
| 31 | # this will create an engine instance of the appropriate $type, named $name | ||||||||||||||||||||||||||||
| 32 | # if Dancer::$type::$name exists. | ||||||||||||||||||||||||||||
| 33 | 489 | ▶ | sub build { | ||||||||||||||||||||||||||
|
Subroutine
build: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 489 | my ($class, $type, $name, $config) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 489 | ▶ | raise core_engine => "cannot build engine without type and name " | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name and $type
MC/DC50%1/2
$name and $type
$name
$type
Truth table66%2/3
A and B
A= $nameB= $type
| |||||||||||||||||||||||||||||
| 37 | unless $name and $type; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 488 | my $class_name = $class->_engine_class($type); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 488 | ▶ | $config ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config ||= {}
MC/DC100%1/1
$config ||= {}
$config
Truth table100%2/2
A
A= $config
| |||||||||||||||||||||||||||||
| 42 | 488 | ▶ | $config->{engines} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"engines"} ||= {}
MC/DC100%1/1
$config->{"engines"} ||= {}
$config->{"engines"}
Truth table100%2/2
A
A= $config->{"engines"}
| |||||||||||||||||||||||||||||
| 43 | 488 | ▶ | my $settings = $config->{engines}{$name} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'engines'}{$name} || {}
MC/DC100%1/1
$config->{'engines'}{$name} || {}
$config->{'engines'}{$name}
Truth table100%2/2
A
A= $config->{'engines'}{$name}
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # trying to load the engine | ||||||||||||||||||||||||||||
| 46 | 488 | my $engine_class = | |||||||||||||||||||||||||||
| 47 | Dancer::ModuleLoader->class_from_setting($class_name => $name); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 488 | my( $loaded, $error ) = Dancer::ModuleLoader->load($engine_class); | |||||||||||||||||||||||||||
| 50 | 488 | ▶ | $error = '' unless defined $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 488 | ▶ | unless( $loaded ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 6 | my $tip = ''; | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | if( $error =~ /Can't locate (\S+)\.pm in \@INC/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 4 | my $module = $1; | |||||||||||||||||||||||||||
| 56 | 4 | $module =~ s#/#::#g; | |||||||||||||||||||||||||||
| 57 | 4 | $tip = " (perhaps you need to install $module?)"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | $error = ": $error" if length $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 6 | raise core_engine => "unable to load $type engine '$name'$tip$error"; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # creating the engine | ||||||||||||||||||||||||||||
| 65 | 482 | return $engine_class->new( | |||||||||||||||||||||||||||
| 66 | name => $name, | ||||||||||||||||||||||||||||
| 67 | type => $type, | ||||||||||||||||||||||||||||
| 68 | config => $settings, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 538 | ▶ | sub _engine_class { | ||||||||||||||||||||||||||
|
Subroutine
_engine_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 538 | my ($class, $type) = @_; | |||||||||||||||||||||||||||
| 74 | 538 | $type = ucfirst($type); | |||||||||||||||||||||||||||
| 75 | 538 | return "Dancer::${type}"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 50 | ▶ | sub engine { | ||||||||||||||||||||||||||
|
Subroutine
engine: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 50 | my ($class, $type) = @_; | |||||||||||||||||||||||||||
| 80 | 50 | return $class->_engine_class($type)->engine(); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 251 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||