Dancer::Engine - base class for Dancer engines


Dancer documentation Contained in the Dancer distribution.

Index


Code Index:

NAME

Top

Dancer::Engine - base class for Dancer engines

SYNOPSIS

Top

    my $engine = Dancer::Engine->build( Serializer => 'JSON', $configuration );

DESCRIPTION

Top

Dancer has various engines such Serializer engines, Template engines, Logger engines and Session handlers engines. This is the base class for all Dancer engines.

If you're writing an engine of a common type (such as those mentioned above), you probably want to simply use their base class, which in turn use Dancer::Engine. For example, Template engines inherit from Dancer::Template::Abstract and Serializer engines inherit from Dancer::Serializer::Abstract. Those Abstract base classes inherit from Dancer::Engine.

If a new type of Dancer engine is created, it is best it inherits from this class.

ATTRIBUTES

Top

name

The name of the engine, such as JSON, or Simple.

type

The type of the engine, such as Serializer, or Session.

METHODS/SUBROUTINES

Top

config

Fetches the configuration of the engine.

    my $configuration = $engine->config;

You can only set the configuration at initialization time, not after.

build

Builds and returns the engine.

    my $engine = Dancer::Engine->build( $type => $name, $config );

AUTHOR

Top

Alexis Sukrieh

LICENSE AND COPYRIGHT

Top


Dancer documentation Contained in the Dancer distribution.

package Dancer::Engine;

# This is the base-class of every engine abstract class.
# This allow us to put in that single place the engine creation
# from a namespace and a name, to its configuration initialization.

use strict;
use warnings;
use Carp;
use Dancer::ModuleLoader;
use base 'Dancer::Object';

# constructor arguments:
#      name     => $name_of_the_engine
#      settings => $hash_of_engine_settings
Dancer::Engine->attributes(qw(name type));

# Accessor to the config hash, it may not be initialized if someone
# creates a new engine without giving the appropriate arguments.
# e.g. Dancer::Template::Simple->new();
sub config {
    my ($self) = @_;
    return $self->{config} if defined $self->{config};
    $self->{config} = {};
}

# static method for initializing an engine
# this will create an engine instance of the appropriate $type, named $name
# if Dancer::$type::$name exists.
sub build {
    my ($class, $type, $name, $config) = @_;

    croak "cannot build engine without type and name "
      unless $name and $type;

    my $class_name = $class->_engine_class($type);

    $config ||= {};
    $config->{engines} ||= {};
    my $settings = $config->{engines}{$name} || {};

    # trying to load the engine
    my $engine_class =
      Dancer::ModuleLoader->class_from_setting($class_name => $name);

    croak "unknown $type engine '$name', "
      . "perhaps you need to install $engine_class?"
      unless Dancer::ModuleLoader->load($engine_class);

    # creating the engine
    return $engine_class->new(
        name   => $name,
        type   => $type,
        config => $settings,
    );
}

sub _engine_class {
    my ($class, $type) = @_;
    $type = ucfirst($type);
    return "Dancer::${type}";
}

sub engine {
    my ($class, $type) = @_;
    return $class->_engine_class($type)->engine();
}


1;

__END__