X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FSimpleConfig.pm;h=4754a7e101f4b660deb78fb3c44f719786093675;hb=f33cbec46d8eba5070be5978d005c9b2de206b26;hp=76380e5796aa18860f1daadddce07ccecd60349c;hpb=a61b087b18c87dde0503fac7cd4b5cf9a3b86ef0;p=gitmo%2FMooseX-SimpleConfig.git diff --git a/lib/MooseX/SimpleConfig.pm b/lib/MooseX/SimpleConfig.pm index 76380e5..4754a7e 100644 --- a/lib/MooseX/SimpleConfig.pm +++ b/lib/MooseX/SimpleConfig.pm @@ -3,29 +3,41 @@ package MooseX::SimpleConfig; use Moose::Role; with 'MooseX::ConfigFromFile'; -our $VERSION = '0.02'; +our $VERSION = '0.05'; use Config::Any (); sub get_config_from_file { my ($class, $file) = @_; - my $raw_cfany = Config::Any->load_files({ - files => [ $file ], - use_ext => 1, - }); - - die q{Specified configfile '} . $file - . q{' does not exist, is empty, or is not readable} - unless $raw_cfany->[0] - && exists $raw_cfany->[0]->{$file}; - - my $raw_config = $raw_cfany->[0]->{$file}; + my $files_ref = ref $file eq 'ARRAY' ? $file : [$file]; - die "configfile must represent a hash structure" - unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH'; - - $raw_config; + my $can_config_any_args = $class->can('config_any_args'); + my $extra_args = $can_config_any_args ? + $can_config_any_args->($class, $file) : {}; + ; + my $raw_cfany = Config::Any->load_files({ + %$extra_args, + use_ext => 1, + files => $files_ref, + flatten_to_hash => 1, + } ); + + my %raw_config; + foreach my $file_tested ( @{$files_ref} ) { + if ( ! exists $raw_cfany->{$file_tested} ) { + die qq{Specified configfile '$file_tested' does not exist, } . + q{is empty, or is not readable}; + } + + my $cfany_hash = $raw_cfany->{$file_tested}; + die "configfile must represent a hash structure in file: $file_tested" + unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH'; + + %raw_config = ( %raw_config, %{$cfany_hash} ); + } + + \%raw_config; } no Moose::Role; 1; @@ -44,45 +56,45 @@ MooseX::SimpleConfig - A Moose role for setting attributes from a simple configf foo: bar baz: 123 - ## In your class + ## In your class package My::App; use Moose; - + with 'MooseX::SimpleConfig'; - + has 'foo' => (is => 'ro', isa => 'Str', required => 1); has 'baz' => (is => 'rw', isa => 'Int', required => 1); - + # ... rest of the class here - + ## in your script #!/usr/bin/perl - + use My::App; - + my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); # ... rest of the script here #################### ###### combined with MooseX::Getopt: - ## In your class + ## In your class package My::App; use Moose; - + with 'MooseX::SimpleConfig'; with 'MooseX::Getopt'; - + has 'foo' => (is => 'ro', isa => 'Str', required => 1); has 'baz' => (is => 'rw', isa => 'Int', required => 1); - + # ... rest of the class here - + ## in your script #!/usr/bin/perl - + use My::App; - + my $app = My::App->new_with_options(); # ... rest of the script here @@ -106,7 +118,17 @@ well, which allows specifying C<-configfile> on the commandline. =head2 configfile -Provided by the base role L. +Provided by the base role L. You can +provide a default configfile pathname like so: + + has +configfile ( default => '/etc/myapp.yaml' ); + +You can pass an array of filenames if you want, but as usual the array +has to be wrapped in a sub ref. + + has +configfile ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } ); + +Config files are trivially merged at the top level, with the right-hand files taking precedence. =head1 CLASS METHODS