App-Sqitch

 view release on metacpan or  search on metacpan

t/lib/TestConfig.pm  view on Meta::CPAN

package TestConfig;
use strict;
use warnings;
use base 'App::Sqitch::Config';
use Path::Class;

BEGIN {
    # Suppress warnings from Locale::Messages.
    # https://github.com/gflohr/libintl-perl/issues/14
    use Locale::Messages;
    if ($Locale::Messages::package eq 'gettext_pp' && Locale::Messages->VERSION < 1.35) {
        no warnings qw(redefine prototype);
        no strict 'refs';
        my $orig = \&Locale::gettext_pp::__locale_category;
        *{"Locale::gettext_pp::__locale_category"} = sub {
            local $SIG{__WARN__} = sub {};
            $orig->();
        }
    }
}

# Creates and returns a new TestConfig, which inherits from
# App::Sqitch::Config. Sets nonexistent values for the file locations and
# calls update() on remaining args.
#
#   my $config = TestConfig->new(
#      'core.engine'      => 'sqlite',
#      'add.all'          => 1,
#      'deploy.variables' => { _prefix => 'test_', user => 'bob' }
#      'foo.bar'          => [qw(one two three)],
#  );
sub new {
    my $self = shift->SUPER::new;
    $self->{test_local_file}  = 'nonexistent.local';
    $self->{test_user_file}   = 'nonexistent.user';
    $self->{test_system_file} = 'nonexistent.system';
    $self->update(@_);
    return $self;
}

# Pass in key/value pairs to set the data. Does not clear existing data. Keys
# should be "$section.$name". Values can be scalars, arrays, or hashes.
# Scalars are simply set as-is, unless the value is `undef`, in which case the
# key is deleted. Arrays are set as multiple values for the key. Hashes have
# each of their keys appended as "$section.$name.$key", with the values
# assigned as-is. Existing keys will be replaced with the new values.
#
#   my $config->update(
#      'core.engine'      => 'sqlite',
#      'add.all'          => 1,
#      'deploy.variables' => { _prefix => 'test_', user => 'bob' }
#      'foo.bar'          => [qw(one two three)],
#  );
sub update {
    my $self = shift;
    my %p = @_ or return;
    $self->data({}) unless $self->is_loaded;
    # Set a unique origin to be sure to override any previous values for each key.
    my @args = (origin => ('update_' . ++$self->{__update}));

    while (my ($k, $v) = each %p) {
        my $ref = ref $v;
        if ($ref eq '') {
            if (defined $v) {
                $k =~ s/[.]([^.]+)$//;
                $self->define(@args, section => $k, name => $1, value => $v);
            } else {
                $self->set_multiple( $k, 0 ) if $self->is_multiple( $k );
                $k = lc $k;
                delete $_->{$k} for ($self->origins, $self->data, $self->casing);
            }
        } elsif ($ref eq 'HASH') {
            $self->define(@args, section => $k, name => $_, value => $v->{$_} )
                for keys %{ $v };
        } elsif ($ref eq 'ARRAY') {



( run in 2.426 seconds using v1.01-cache-2.11-cpan-524268b4103 )