ETLp
view release on metacpan or search on metacpan
lib/ETLp/ItemBuilder.pm view on Meta::CPAN
package ETLp::ItemBuilder;
use MooseX::Declare;
=head1 NAME
ETLp::ItemBuilder - Builds the pipeline of items to execute
=head1 DESCRIPTION
The pipeline is effectivey an array of anonymous methods that will be
called when the pipline execution begins
=head1 METHODS
=head2 new
=head3 parameters
* plugins: Required. A hashref of item types types and the name of
the plugin that executes that type
* pipeline_type: Required. Either "iteration" or "serial"
* include_env: Optional. Whether the environment variables should be
substituted into the configuration. Defaults to false (0)
* env_config: Optional The environment variables (a hashref)
* app_root: Required. The application root directory
=head3 returns
an ETLp::ItemBuilder object
=cut
class ETLp::ItemBuilder with ETLp::Role::Config {
use ETLp::Exception;
use Data::Dumper;
use UNIVERSAL::require;
use Try::Tiny;
use Clone qw(clone);
use File::Basename;
has 'plugins' => (is => 'ro', isa => 'HashRef', required => 1);
has 'pipeline_type' => (is => 'ro', isa => 'Str', required => 1);
has 'allow_env_vars' =>
(is => 'ro', isa => 'Bool', required => 0, default => 0);
has 'env_conf' =>
(is => 'ro', isa => 'HashRef', required => 0, default => sub { {} });
has 'app_root' => (is => 'ro', isa => 'Maybe[Str]', required => 1);
has 'config' => (is => 'ro', isa => 'HashRef', required => 1);
# Recursively navigate the items, replacing the placeholders
# with their values
method _parse_item(HashRef $config, $item, Maybe [Str] $filename) {
my $app_root = $self->app_root;
my %master_config = (%{$self->env_conf}, %{$config->{config}});
$self->logger->debug("Master config: " . Dumper(\%master_config));
if (ref $item eq 'HASH') {
foreach my $item_key (keys %$item) {
if (!ref $item->{$item_key}) {
if ($item->{$item_key} =~ /%app_root%/) {
$item->{$item_key} =~ s/%app_root%/$app_root/g;
}
if ($filename) {
if ($item->{$item_key} =~ /%filename%/) {
$item->{$item_key} =~ s/%filename%/$filename/g;
}
if ($item->{$item_key} =~ /%basename\(filename\)%/) {
my $base_filename = basename($filename);
$item->{$item_key} =~
s/%basename\(filename\)%/$base_filename/g;
}
}
foreach my $config_key (keys %master_config) {
if ($item->{$item_key} =~ /%$config_key%/) {
my $env_value = $master_config{$config_key};
$item->{$item_key} =~ s/%$config_key%/$env_value/g;
}
}
} else {
$self->_parse_item($config, $item->{$item_key}, $filename);
}
}
} elsif (ref $item eq 'ARRAY') {
for (my $j = 0 ; $j < @$item ; $j++) {
$self->_parse_item($config, $item->[$j], $filename);
}
}
return $item;
}
# Go through the config section and the items, replacing the placeholders
# with their values
method _parse_config(HashRef $config, Maybe [Str] $filename){
my $app_root = $self->app_root;
# Firstly, replace app_root and filename throughout the config section
foreach my $config_key (keys %{$config->{config}}) {
# replace the app_root placeholder
if ($config->{config}->{$config_key} =~ /%app_root%/) {
$config->{config}->{$config_key} =~ s/%app_root%/$app_root/g;
}
# replace the environment placeholders if we are allowed to
foreach my $env_config_key (keys %{$self->env_conf}) {
( run in 2.484 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )