From: Brandon L Black Date: Tue, 18 Dec 2007 22:44:08 +0000 (+0000) Subject: new dist: MooseX::ConfigFromFile X-Git-Tag: 0.01~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c35b639ebabd8c4e2917f17954bb99d269be8171;p=gitmo%2FMooseX-ConfigFromFile.git new dist: MooseX::ConfigFromFile --- c35b639ebabd8c4e2917f17954bb99d269be8171 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..167db3d --- /dev/null +++ b/ChangeLog @@ -0,0 +1,4 @@ +Revision history for Perl extension MooseX::ConfigFromFile + +0.01 - Dec 18, 2007 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..76c3fa3 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,20 @@ +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/ConfigFromFile.pm +Makefile.PL +MANIFEST This list of files +META.yml +README +t/01use.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..3316d0a --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,17 @@ +# Load the Module::Install bundled in ./inc/ +use inc::Module::Install 0.65; + +name 'MooseX-ConfigFromFile'; +all_from 'lib/MooseX/ConfigFromFile.pm'; + +test_requires 'Test::More' => '0.42'; + +requires 'Moose' => '0'; +requires 'MooseX::Types::Path::Class' => '0'; + +# Rebuild README for maintainers +system("pod2text lib/MooseX/ConfigFromFile.pm >README") if -e 'MANIFEST.SKIP'; + +auto_provides; +auto_install; +WriteAll; diff --git a/README b/README new file mode 100644 index 0000000..6f251de --- /dev/null +++ b/README @@ -0,0 +1,86 @@ +NAME + MooseX::ConfigFromFile - An abstract Moose role for setting attributes + from a configfile + +SYNOPSIS + ######## + ## A real role based on this abstract role: + ######## + + package MooseX::SomeSpecificConfigRole; + use Moose::Role; + + with 'MooseX::ConfigFromFile'; + + use Some::ConfigFile::Loader (); + + sub get_config_from_file { + my ($class, $file) = @_; + + my $options_hashref = Some::ConfigFile::Loader->load($file); + + return $options_hashref; + } + + + ######## + ## A class that uses it: + ######## + package Foo; + use Moose; + with 'MooseX::SomeSpecificConfigRole'; + + # ... insert your stuff here ... + + ######## + ## A script that uses the class with a configfile + ######## + + my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo'); + +DESCRIPTION + This is an abstract role which provides an alternate constructor for + creating objects using parameters passed in from a configuration file. + The actual implementation of reading the configuration file is left to + concrete subroles. + + It declares an attribute "configfile" and a class method + "new_with_config", and requires that concrete roles derived from it + implement the class method "get_config_from_file". + + MooseX::Getopt knows about this abstract role, and will use it if + available to load attributes from the file specified by the commandline + flag "--configfile" during its normal "new_with_options". + +Attributes + configfile + This is a Path::Class::File object which can be coerced from a regular + pathname string. This is the file your attributes are loaded from. + +Class Methods + new_with_config + This is an alternate constructor, which knows to look for the + "configfile" option in its arguments and use that to set attributes. It + is much like MooseX::Getopts's "new_with_options". Example: + + my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml'); + + Explicit arguments will overide anything set by the configfile. + + get_config_from_file + This class method is not implemented in this role, but it is required of + all subroles. Its two arguments are the classname and the configfile, + and it is expected to return a hashref of arguments to pass to "new()". + + meta + The Moose meta stuff, included here because otherwise pod tests fail + sometimes + +BUGS +AUTHOR + Brandon L. Black, + +LICENSE + This library is free software; you can redistribute it and/or modify it + under the same terms as Perl itself. + diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm new file mode 100644 index 0000000..2759f57 --- /dev/null +++ b/lib/MooseX/ConfigFromFile.pm @@ -0,0 +1,128 @@ +package MooseX::ConfigFromFile; + +use Moose::Role; +use MooseX::Types::Path::Class qw( File ); + +our $VERSION = '0.01'; + +requires 'get_config_from_file'; + +has configfile => ( + is => 'ro', + isa => File, + coerce => 1, + predicate => 'has_configfile', +); + +sub new_with_config { + my ($class, %opts) = @_; + + if($opts{configfile}) { + %opts = (%{$class->get_config_from_file($opts{configfile})}, %opts); + } + $class->new(%opts); +} + +no Moose::Role; 1; + +__END__ + +=pod + +=head1 NAME + +MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile + +=head1 SYNOPSIS + + ######## + ## A real role based on this abstract role: + ######## + + package MooseX::SomeSpecificConfigRole; + use Moose::Role; + + with 'MooseX::ConfigFromFile'; + + use Some::ConfigFile::Loader (); + + sub get_config_from_file { + my ($class, $file) = @_; + + my $options_hashref = Some::ConfigFile::Loader->load($file); + + return $options_hashref; + } + + + ######## + ## A class that uses it: + ######## + package Foo; + use Moose; + with 'MooseX::SomeSpecificConfigRole'; + + # ... insert your stuff here ... + + ######## + ## A script that uses the class with a configfile + ######## + + my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo'); + +=head1 DESCRIPTION + +This is an abstract role which provides an alternate constructor for creating +objects using parameters passed in from a configuration file. The +actual implementation of reading the configuration file is left to +concrete subroles. + +It declares an attribute C and a class method C, +and requires that concrete roles derived from it implement the class method +C. + +L knows about this abstract role, and will use it if available +to load attributes from the file specified by the commandline flag C<--configfile> +during its normal C. + +=head1 Attributes + +=head2 configfile + +This is a L object which can be coerced from a regular pathname +string. This is the file your attributes are loaded from. + +=head1 Class Methods + +=head2 new_with_config + +This is an alternate constructor, which knows to look for the C option +in its arguments and use that to set attributes. It is much like L's +C. Example: + + my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml'); + +Explicit arguments will overide anything set by the configfile. + +=head2 get_config_from_file + +This class method is not implemented in this role, but it is required of all subroles. +Its two arguments are the classname and the configfile, and it is expected to return +a hashref of arguments to pass to C. + +=head2 meta + +The Moose meta stuff, included here because otherwise pod tests fail sometimes + +=head1 BUGS + +=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..80a4331 --- /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::ConfigFromFile'); +}