Ambrosia

 view release on metacpan or  search on metacpan

lib/Ambrosia/Config.pm  view on Meta::CPAN

        if ( ref $conf eq 'HASH' )
        {
            no strict 'refs';
            no warnings 'redefine';
            Ambrosia::core::ClassFactory::create($package, {public => [keys %$conf]});
            *{"$package\::DESTROY"} = sub {};
            ${"$package\::AUTOLOAD"} = '';
            *{"$package\::AUTOLOAD"} = sub : lvalue {
                my $this = shift;
                my $value = shift;
                my ($func) = our $AUTOLOAD =~ /(\w+)$/
                    or throw Ambrosia::error::Exception 'Error: cannot resolve AUTOLOAD: ' . $AUTOLOAD;
                *{$package . '::' . $func} = sub : lvalue { $_[0]->[1]->{$func} };
                $this->$func = $value;
            };

            $self = $package->new($conf);
        }
        elsif($conf)
        {
            die 'Bad config format in ' . $prm . '. Config file must return reference to hash.';
        }
    };
    if ( $@ )
    {
        throw Ambrosia::error::Exception('Error in config: ' . $prm . ';', $@);
    }
    return $self;
}

sub DESTROY
{
    
}

1;


__END__

=head1 NAME

Ambrosia::Config - a class for read a configuration data.
It implements the pattern B<Singleton>.

=head1 VERSION

version 0.010

=head1 SYNOPSIS

    #In the file "test.pl"
    use Ambrosia::Config;
    use Foo;
    BEGIN
    {
        instance Ambrosia::Config( myApplication => './foo.conf' );
    };
    Ambrosia::Config::assign 'myApplication';
    #..............
    say Foo::proc1();
    #..............

    #In the file "Foo.pm"
    package Foo;
    use Ambrosia::Config;

    sub proc1
    {
        return config->ParamA;
    }
    
    1;

    #In the config file "foo.conf"
    return { ParamA => 'ABC' };

=head1 DESCRIPTION

C<Ambrosia::Config> is a class of object Ambrosia::Config.
The file of config is the perl script that MUST return reference to hash.
Each key of the hash becomes a method of object of type Ambrosia::Config that return an appropriate value.

WARNING!
This method is "lvalue" and you can modify a config value on the fly.

=head2 instance

This method instantiates the named object of type C<Ambrosia::Config> in the pool.
This method not exported. Use as constructor: C<instance Ambrosia::Config(.....)>
C<instance(name =&gt; path)> - where the "name" is a keyname for config and the "path" is a path to config file.
C<instance(name =&gt; hash)> - where the "name" is a keyname for config and the "hash" is a config data.

=head2 config

Returns the global object of type C<Ambrosia::Config>.
C<config(name)> - the "name" is optional param. Call with "name" if you not assign current process to config.

=head2 assign ($name)

Assign current process to the global named object of type C<Ambrosia::Config>.

=head1 DEPENDENCIES

L<Ambrosia::core::Exceptions>
L<Ambrosia::Meta>

=head1 THREADS

Not tested.

=head1 BUGS

Please report bugs relevant to C<Ambrosia> to <knm[at]cpan.org>.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010-2012 Nickolay Kuritsyn. All rights reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.



( run in 1.224 second using v1.01-cache-2.11-cpan-364913b4093 )