0.03, configfile default stuff
[gitmo/MooseX-SimpleConfig.git] / lib / MooseX / SimpleConfig.pm
CommitLineData
33defd43 1package MooseX::SimpleConfig;
2
3use Moose::Role;
4with 'MooseX::ConfigFromFile';
5
a96520dd 6our $VERSION = '0.03';
33defd43 7
8use Config::Any ();
9
10sub 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
31no Moose::Role; 1;
32
33__END__
34
35=pod
36
37=head1 NAME
38
39MooseX::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
94This role loads simple configfiles to set object attributes. It
95is based on the abstract role L<MooseX::ConfigFromFile>, and uses
96L<Config::Any> to load your configfile. L<Config::Any> will in
97turn support any of a variety of different config formats, detected
98by the file extension. See L<Config::Any> for more details about
99supported formats.
100
101Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
102module is automatically supported by the L<MooseX::Getopt> role as
103well, which allows specifying C<-configfile> on the commandline.
104
105=head1 ATTRIBUTES
106
107=head2 configfile
108
a96520dd 109Provided by the base role L<MooseX::ConfigFromFile>. You can
110provide a default configfile pathname like so:
111
112 has +configfile ( default => '/etc/myapp.yaml' );
33defd43 113
114=head1 CLASS METHODS
115
116=head2 new_with_config
117
118Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
119regular C<new()>, but also accepts an argument C<configfile> to specify
120the configfile from which to load other attributes. Explicit arguments
121to C<new_with_config> will override anything loaded from the configfile.
122
123=head2 get_config_from_file
124
125Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
126C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
127
128=head1 AUTHOR
129
130Brandon L. Black, E<lt>blblack@gmail.comE<gt>
131
132=head1 LICENSE
133
134This library is free software; you can redistribute it and/or modify
135it under the same terms as Perl itself.
136
137=cut