Catmandu

 view release on metacpan or  search on metacpan

lib/Catmandu/Env.pm  view on Meta::CPAN

}

sub store {
    my $self = shift;
    $self->_named_package('store', $self->store_namespace,
        $self->default_store, $self->default_store_package,
        $self->stores,        @_);
}

sub importer {
    my $self = shift;
    $self->_named_package('importer', $self->importer_namespace,
        $self->default_importer, $self->default_importer_package,
        undef,                   @_);
}

sub exporter {
    my $self = shift;
    $self->_named_package('exporter', $self->exporter_namespace,
        $self->default_exporter, $self->default_exporter_package,
        undef,                   @_);
}

sub validator {
    my $self = shift;
    $self->_named_package(
        'validator',              $self->validator_namespace,
        $self->default_validator, $self->default_validator_package,
        $self->validators,        @_
    );
}

sub _named_package {
    my $self            = shift;
    my $type            = shift;
    my $ns              = shift;
    my $default_name    = shift;
    my $default_package = shift;
    my $cache           = shift;
    my $name            = shift;
    my $key             = $name || $default_name;

    return $name if is_instance($name) && index(ref($name), $ns) == 0;

    # return cached instance if no arguments are given
    if ($cache && !@_ and my $instance = $cache->{$key}) {
        return $instance;
    }

    if (exists $self->config->{$type}) {
        if (my $c = $self->config->{$type}{$key}) {
            check_hash_ref($c);
            check_string(my $package = $c->{package} || $default_package);
            my $opts = check_hash_ref($c->{options} || {});
            if (@_ > 1) {
                $opts = {%$opts, @_};
            }
            elsif (@_ == 1) {
                $opts = {%$opts, %{$_[0]}};
            }
            my $instance = require_package($package, $ns)->new($opts);

            # cache this instance if no arguments are given
            if ($cache && !@_) {
                $cache->{$key} = $instance;
            }

            return $instance;
        }
    }

    check_string(my $package = $name || $default_package);
    require_package($package, $ns)->new(@_);
}

1;

__END__

=pod

=head1 NAME

Catmandu::Env - A catmandu configuration file loader

=head1 SYNOPSIS

    use Catmandu::Env;

    my $env = Catmandu::Env->new(load_paths => [ '/etc/catmandu '] );
    my $env = Catmandu::Env->new(load_paths => [ ':up'] );

    my $store    = $env->store('mongodb');
    my $importer = $env->importer('loc');
    my $exporter = $env->exporter('europeana');
    my $fixer    = $env->fixer('my_fixes');
    my $conf     = $env->config;

=head1 DESCRIPTION

This class loads the catmandu.*.pl, catmandu.*.json, catmandu.*.yml and catmandu.*.yaml file from
all provided load_paths. Programmers are advised I<not> to use this class directly 
but use the equivalent functionality provided in the Catmandu package:

     Catmandu->load('/etc/catmandu');
     Catmandu->load(':up');

     my $store    = Catmandu->store('mongodb');
     my $importer = Catmandu->importer('loc');
     my $exporter = Catmandu->exporter('europeana');
     my $fixer    = Catmandu->fixer('my_fixes');
     my $conf     = Catmandu->config;

=head1 SEE ALSO

L<Catmandu>

=cut



( run in 1.487 second using v1.01-cache-2.11-cpan-5a3173703d6 )