From: Brandon L Black Date: Tue, 18 Dec 2007 22:44:46 +0000 (+0000) Subject: new dist: MooseX::SimpleConfig X-Git-Tag: 0.01~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=33defd4302d59a7ce13ac2c65dfaa82cf2982dde;p=gitmo%2FMooseX-SimpleConfig.git new dist: MooseX::SimpleConfig --- 33defd4302d59a7ce13ac2c65dfaa82cf2982dde diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..a919ccc --- /dev/null +++ b/Build.PL @@ -0,0 +1,3 @@ +# Dear Distribution Packager. This use of require is intentional. +# Module::Install detects Build.PL usage and acts accordingly. +require 'Makefile.PL'; diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..2cc6c1c --- /dev/null +++ b/ChangeLog @@ -0,0 +1,4 @@ +Revision history for Perl extension MooseX::SimpleConfig + +0.01 - Dec 18, 2007 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..4b8e113 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,21 @@ +ChangeLog +inc/Module/AutoInstall.pm +inc/Module/Install.pm +inc/Module/Install/AutoInstall.pm +inc/Module/Install/Base.pm +inc/Module/Install/Build.pm +inc/Module/Install/Can.pm +inc/Module/Install/Fetch.pm +inc/Module/Install/Include.pm +inc/Module/Install/Makefile.pm +inc/Module/Install/Metadata.pm +inc/Module/Install/Win32.pm +inc/Module/Install/WriteAll.pm +lib/MooseX/SimpleConfig.pm +Makefile.PL +MANIFEST This list of files +META.yml +README +t/01use.t +t/10simple.t +t/lib/MXSimpleConfigTest.pm diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..4a31474 --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1,20 @@ +^_build +^Build$ +^blib +~$ +\.bak$ +^MANIFEST\.SKIP$ +CVS +\.svn +\.DS_Store +cover_db +\..*\.sw.?$ +^Makefile$ +^pm_to_blib$ +^MakeMaker-\d +^blibdirs$ +\.old$ +^#.*#$ +^\.# +\.gz$ +Build.PL diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..3f37100 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,18 @@ +# Load the Module::Install bundled in ./inc/ +use inc::Module::Install 0.65; + +name 'MooseX-SimpleConfig'; +all_from 'lib/MooseX/SimpleConfig.pm'; + +test_requires 'Test::More' => '0.42'; + +requires 'Moose' => '0'; +requires 'MooseX::ConfigFromFile' => '0'; +requires 'Config::Any' => '0'; + +# Rebuild README for maintainers +system("pod2text lib/MooseX/SimpleConfig.pm >README") if -e 'MANIFEST.SKIP'; + +auto_provides; +auto_install; +WriteAll; diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/lib/MooseX/SimpleConfig.pm b/lib/MooseX/SimpleConfig.pm new file mode 100644 index 0000000..6fda06c --- /dev/null +++ b/lib/MooseX/SimpleConfig.pm @@ -0,0 +1,134 @@ +package MooseX::SimpleConfig; + +use Moose::Role; +with 'MooseX::ConfigFromFile'; + +our $VERSION = '0.01'; + +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}; + + die "configfile must represent a hash structure" + unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH'; + + $raw_config; +} + +no Moose::Role; 1; + +__END__ + +=pod + +=head1 NAME + +MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile + +=head1 SYNOPSIS + + ## A YAML configfile named /etc/my_app.yaml: + foo: bar + baz: 123 + + ## 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 + 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 + + ## on the command line + % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123 + +=head1 DESCRIPTION + +This role loads simple configfiles to set object attributes. It +is based on the abstract role L, and uses +L to load your configfile. L will in +turn support any of a variety of different config formats, detected +by the file extension. See L for more details about +supported formats. + +Like all L -derived configfile loaders, this +module is automatically supported by the L role as +well, which allows specifying C<-configfile> on the commandline. + +=head1 ATTRIBUTES + +=head2 configfile + +Provided by the base role L. + +=head1 CLASS METHODS + +=head2 new_with_config + +Provided by the base role L. Acts just like +regular C, but also accepts an argument C to specify +the configfile from which to load other attributes. Explicit arguments +to C will override anything loaded from the configfile. + +=head2 get_config_from_file + +Called internally by either C or L's +C. Invokes L to parse C. + +=head1 AUTHOR + +Brandon L. Black, Eblblack@gmail.comE + +=head1 LICENSE + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/t/01use.t b/t/01use.t new file mode 100644 index 0000000..ac7b132 --- /dev/null +++ b/t/01use.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +BEGIN { + use_ok('MooseX::SimpleConfig'); +} diff --git a/t/10simple.t b/t/10simple.t new file mode 100644 index 0000000..c1d0722 --- /dev/null +++ b/t/10simple.t @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 6; + +use lib 't/lib'; +use lib '../t/lib'; + +BEGIN { + use_ok('MXSimpleConfigTest'); +} + +# Does it work with no configfile and not barf? +{ + eval { MXSimpleConfigTest->new(req_attr => 'foo') }; + ok(!$@, 'Did not die with no configfile specified') + or diag $@; +} + +# Can it load a simple YAML file with the options +{ + open(my $test_yaml, '>', 'test.yaml') + or die "Cannot create test.yaml: $!"; + print $test_yaml "direct_attr: 123\ninherited_ro_attr: asdf\nreq_attr: foo\n"; + close($test_yaml); + + my $foo = eval { + MXSimpleConfigTest->new_with_config(configfile => 'test.yaml'); + }; + ok(!$@, 'Did not die with good YAML configfile') + or diag $@; + + is($foo->req_attr, 'foo', 'req_attr works'); + is($foo->direct_attr, 123, 'direct_attr works'); + is($foo->inherited_ro_attr, 'asdf', 'inherited_ro_attr works'); +} + +END { unlink('test.yaml') } diff --git a/t/lib/MXSimpleConfigTest.pm b/t/lib/MXSimpleConfigTest.pm new file mode 100644 index 0000000..413012e --- /dev/null +++ b/t/lib/MXSimpleConfigTest.pm @@ -0,0 +1,19 @@ +package MXSimpleConfigTestBase; +use Moose; + +has 'inherited_ro_attr' => (is => 'ro', isa => 'Str'); + +no Moose; +1; + +package MXSimpleConfigTest; +use Moose; +extends 'MXSimpleConfigTestBase'; +with 'MooseX::SimpleConfig'; + +has 'direct_attr' => (is => 'ro', isa => 'Int'); + +has 'req_attr' => (is => 'rw', isa => 'Str', required => 1); + +no Moose; +1;