GithubHelp home page GithubHelp logo

edwardbetts / moosex-configfromfile Goto Github PK

View Code? Open in Web Editor NEW

This project forked from moose/moosex-configfromfile

0.0 2.0 0.0 68 KB

An abstract Moose role for setting attributes from a configfile

Home Page: http://metacpan.org/release/MooseX-ConfigFromFile/

License: Other

Perl 100.00%

moosex-configfromfile's Introduction

NAME

MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile

VERSION

version 0.14

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';

# optionally, default the configfile:
sub _get_default_configfile { '/tmp/foo.yaml' }

# ... 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 sub-roles.

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.

Attributes specified directly as arguments to new_with_config supersede those in the configfile.

MooseX::Getopt knows about this abstract role, and will use it if available to load attributes from the file specified by the command line flag --configfile during its normal new_with_options.

Attributes

configfile

This is a Path::Tiny object which can be coerced from a regular path string or any object that supports stringification. This is the file your attributes are loaded from. You can add a default configfile in the consuming class and it will be honored at the appropriate time; see below at "_get_default_configfile".

If you have MooseX::Getopt installed, this attribute will also have the Getopt trait supplied, so you can also set the configfile from the command line.

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 override anything set by the configfile.

get_config_from_file

This class method is not implemented in this role, but it is required of all classes or roles that consume this role. Its two arguments are the class name and the configfile, and it is expected to return a hashref of arguments to pass to new() which are sourced from the configfile.

_get_default_configfile

This class method is not implemented in this role, but can and should be defined in a consuming class or role to return the default value of the configfile (if not passed into the constructor explicitly).

AUTHOR

Brandon L. Black <[email protected]>

CONTRIBUTORS

COPYRIGHT AND LICENSE

This software is copyright (c) 2007 by Brandon L. Black.

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

moosex-configfromfile's People

Contributors

blblack avatar bobtfish avatar karenetheridge avatar nothingmuch avatar perigrin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.