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