oops, long lost 0.02 commit
[gitmo/MooseX-SimpleConfig.git] / lib / MooseX / SimpleConfig.pm
1 package MooseX::SimpleConfig;
2
3 use Moose::Role;
4 with 'MooseX::ConfigFromFile';
5
6 our $VERSION   = '0.02';
7
8 use Config::Any ();
9
10 sub get_config_from_file {
11     my ($class, $file) = @_;
12
13     my $raw_cfany = Config::Any->load_files({
14         files => [ $file ],
15         use_ext => 1,
16     });
17
18     die q{Specified configfile '} . $file
19         . q{' does not exist, is empty, or is not readable}
20             unless $raw_cfany->[0]
21                 && exists $raw_cfany->[0]->{$file};
22
23     my $raw_config = $raw_cfany->[0]->{$file};
24
25     die "configfile must represent a hash structure"
26         unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH';
27
28     $raw_config;
29 }
30
31 no Moose::Role; 1;
32
33 __END__
34
35 =pod
36
37 =head1 NAME
38
39 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
40
41 =head1 SYNOPSIS
42
43   ## A YAML configfile named /etc/my_app.yaml:
44   foo: bar
45   baz: 123
46
47   ## In your class 
48   package My::App;
49   use Moose;
50   
51   with 'MooseX::SimpleConfig';
52   
53   has 'foo' => (is => 'ro', isa => 'Str', required => 1);
54   has 'baz'  => (is => 'rw', isa => 'Int', required => 1);
55   
56   # ... rest of the class here
57   
58   ## in your script
59   #!/usr/bin/perl
60   
61   use My::App;
62   
63   my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
64   # ... rest of the script here
65
66   ####################
67   ###### combined with MooseX::Getopt:
68
69   ## In your class 
70   package My::App;
71   use Moose;
72   
73   with 'MooseX::SimpleConfig';
74   with 'MooseX::Getopt';
75   
76   has 'foo' => (is => 'ro', isa => 'Str', required => 1);
77   has 'baz'  => (is => 'rw', isa => 'Int', required => 1);
78   
79   # ... rest of the class here
80   
81   ## in your script
82   #!/usr/bin/perl
83   
84   use My::App;
85   
86   my $app = My::App->new_with_options();
87   # ... rest of the script here
88
89   ## on the command line
90   % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
91
92 =head1 DESCRIPTION
93
94 This role loads simple configfiles to set object attributes.  It
95 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
96 L<Config::Any> to load your configfile.  L<Config::Any> will in
97 turn support any of a variety of different config formats, detected
98 by the file extension.  See L<Config::Any> for more details about
99 supported formats.
100
101 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
102 module is automatically supported by the L<MooseX::Getopt> role as
103 well, which allows specifying C<-configfile> on the commandline.
104
105 =head1 ATTRIBUTES
106
107 =head2 configfile
108
109 Provided by the base role L<MooseX::ConfigFromFile>.
110
111 =head1 CLASS METHODS
112
113 =head2 new_with_config
114
115 Provided by the base role L<MooseX::ConfigFromFile>.  Acts just like
116 regular C<new()>, but also accepts an argument C<configfile> to specify
117 the configfile from which to load other attributes.  Explicit arguments
118 to C<new_with_config> will override anything loaded from the configfile.
119
120 =head2 get_config_from_file
121
122 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
123 C<new_with_options>.  Invokes L<Config::Any> to parse C<configfile>.
124
125 =head1 AUTHOR
126
127 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
128
129 =head1 LICENSE
130
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133
134 =cut