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