oops, wrong version number
[gitmo/MooseX-Attribute-ENV.git] / lib / MooseX / Attribute / ENV.pm
CommitLineData
650d6350 1package MooseX::Attribute::ENV;
2
ce989a70 3use Moose::Role;
4
ef05887e 5our $VERSION = "0.01";
ce989a70 6our $AUTHORITY = 'cpan:JJNAPIORK';
650d6350 7
8=head1 NAME
9
10MooseX::Attribute::ENV - Set default of an attribute to a value from %ENV
11
650d6350 12=head1 SYNOPSIS
13
ce989a70 14The following is example usage for this attribute trait.
650d6350 15
ce989a70 16 package MyApp::MyClass;
650d6350 17
18 use Moose;
19 use MooseX::Attribute::ENV;
20
ce989a70 21 ## Checks $ENV{username} and $ENV{USERNAME}
22 has 'username' => (
23 traits => ['ENV'],
24 );
25
26 ## Checks $ENV{GLOBAL_PASSWORD}
27 has 'password' => (
28 traits => ['ENV'],
29 env_key => 'GLOBAL_PASSWORD',
30 );
31
32 ## Checks $ENV{last_login}, $ENV{LAST_LOGIN} and then uses the default
33 has 'last_login' => (
34 traits => ['ENV'],
35 default => sub {localtime},
36 );
37
38 ## Checks $ENV{XXX_config_name} and $ENV{XXX_CONFIG_NAME}
39 has 'config_name' => (
40 traits => ['ENV'],
41 env_prefix => 'XXX',
42 );
43
44 ## Checks $ENV{MyApp_MyClass_extra} and $ENV{MYAPP_MYCLASS_EXTRA}
45 has 'extra' => (
46 traits => ['ENV'],
47 env_package_prefix => 1,
48 );
650d6350 49
50Please see the test cases for more detailed examples.
51
52=head1 DESCRIPTION
53
54This is a L<Moose> attribute trait that you use when you want the default value
55for an attribute to be populated from the %ENV hash. So, for example if you
ce989a70 56have set the environment variable USERNAME = 'John' you can do:
650d6350 57
58 package MyApp::MyClass;
59
60 use Moose;
61 use MooseX::Attribute::ENV;
62
63 has 'username' => (is=>'ro', traits=>['ENV']);
64
65 package main;
66
67 my $myclass = MyApp::MyClass->new();
68
69 print $myclass->username; # STDOUT => 'John';
70
71This is basically similar functionality to something like:
72
73 has 'attr' => (
74 is=>'ro',
75 default=> sub {
ce989a70 76 $ENV{uc 'attr'};
650d6350 77 },
78 );
79
80but this module has a few other features that offer merit, as well as being a
ce989a70 81simple enough attribute trait that I hope it can serve as a learning tool.
82
83If the named key isn't found in %ENV, then defaults will execute as normal.
84
85=head1 ATTRIBUTES
86
87This role defines the following attributes.
88
89=head2 env_key ($Str)
90
91By default we look for a key in %ENV based on the actual attribute name. If
92want or need to override this behavior, you can use this modifier.
93
94=cut
95
96has 'env_key' => (
97 is=>'ro',
98 isa=>'Str',
99 predicate=>'has_env_key',
100);
101
102=head2 env_prefix ($Str)
103
104A prefix to attach to the generated filename. The prefix is prepended with a
105trailing underscore. For example, if you attribute was 'attr' and your set a
106prefix of 'xxx' then we'd check for $ENV{xxx_attr} and $ENV{XXX_ATTR}.
107
108=cut
109
110has 'env_prefix' => (
111 is=>'ro',
112 isa=>'Str',
113 predicate=>'has_env_prefix',
114);
115
116=head2 env_package_prefix ($Bool)
117
118Similar to env_prefix, but automatically sets the prefix based on the consuming
119classes package name. So if your attribute is 'attr' and it's in a package
120called: 'Myapp::Myclass' the follow keys in %ENV will be examined:
121
122* Myapp_Myclass_attr
123* MYAPP_MYCLASS_ATTR
124
125Please be aware that if you use this feature, your attribute will automatically
126be converted to lazy, which might effect any default subrefs you also assign to
127this attribute.
128
129Please note that you can't currently use this option along with the option
130'lazy_build'. That might change in a future release, however since these
131attributes are likely to hold simple strings the lazy_build option probably
132won't be missed.
133
134=cut
135
136has 'env_package_prefix' => (
137 is=>'ro',
138 isa=>'Str',
139 predicate=>'has_env_package_prefix',
140);
650d6350 141
142=head1 METHODS
143
144This module defines the following methods.
145
ce989a70 146=head2 _process_options
147
148Overload method so that we can assign the default to be what's in %ENV
149
150=cut
151
152around '_process_options' => sub
153{
154 my ($_process_options, $self, $name, $options) = (shift, @_);
155
156 ## get some stuff we need.
157 my $key = $options->{env_key} || $name;
158 my $default = $options->{default};
159 my $use_pp = $options->{env_package_prefix};
160
161 ## Make it lazy if we are using the package prefix option
162 if( defined $use_pp && $use_pp )
163 {
164 $options->{lazy} = 1;
165 }
166
167 ## Prepend any custom prefixes.
168 if($options->{env_prefix})
169 {
170 $key = join('_', ($options->{env_prefix}, $key));
171 }
172
173 ## override/update the default method for this attribute.
174 CHECK_ENV: {
175
176 $options->{default} = sub {
177
178 if(defined $use_pp && $use_pp)
179 {
180 my $class = blessed $_[0];
181 $class =~s/::/_/g;
182
183 $key = join ('_', ($class, $key));
184 }
185
186 ## Wish we could use perl 5.10 given instead :)
187 if(defined $ENV{$key})
188 {
189 return $ENV{$key};
190 }
191 elsif(defined $ENV{uc $key})
192 {
193 return $ENV{uc $key};
194 }
195 elsif(defined $default)
196 {
197 return ref $default eq 'CODE' ? $default->(@_) : $default;
198 }
199
200 return;
201 };
202 }
203
204 $_process_options->($self, $name, $options);
205};
206
650d6350 207=head1 AUTHOR
208
209John Napiorkowski, C<< <jjnapiork at cpan.org> >>
210
211=head1 BUGS
212
213Please report any bugs or feature requests to:
214
215 C<MooseX-Attribute-ENV at rt.cpan.org>
216
217or through the web interface at:
218
219 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Attribute-ENV>
220
221I will be notified, and then you'll automatically be notified of progress on
222your bug as I make changes.
223
224=head1 SUPPORT
225
226You can find documentation for this module with the perldoc command.
227
228 perldoc MooseX::Attribute::ENV
229
230You can also look for information at:
231
232=over 4
233
234=item * RT: CPAN's request tracker
235
236L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Attribute-ENV>
237
238=item * AnnoCPAN: Annotated CPAN documentation
239
240L<http://annocpan.org/dist/MooseX-Attribute-ENV>
241
242=item * CPAN Ratings
243
244L<http://cpanratings.perl.org/d/MooseX-Attribute-ENV>
245
246=item * Search CPAN
247
248L<http://search.cpan.org/dist/DBIx-Class-PopulateMore>
249
250=back
251
252=head1 LICENSE
253
254This program is free software; you can redistribute it and/or modify it
255under the same terms as Perl itself.
256
257=cut
258
ce989a70 259## Register the trait so this can be used without verbose invocation.
260package Moose::Meta::Attribute::Custom::Trait::ENV;
261sub register_implementation { 'MooseX::Attribute::ENV' }
262
650d6350 2631;