File Coverage

blib/lib/Perl/ToPerl6/UserProfile.pm
Criterion Covered Total %
statement 124 137 90.5
branch 18 32 56.2
condition 16 21 76.1
subroutine 30 31 96.7
pod 8 8 100.0
total 196 229 85.5


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::UserProfile;
2              
3 16     16   27492 use 5.006001;
  16         762  
4 16     16   74 use strict;
  16         20  
  16         753  
5 16     16   61 use warnings;
  16         23  
  16         488  
6              
7 16     16   810 use English qw(-no_match_vars);
  16         5706  
  16         79  
8 16     16   8894 use Readonly;
  16         16917  
  16         782  
9              
10 16     16   8480 use Config::Tiny qw();
  16         13675  
  16         341  
11 16     16   88 use File::Spec qw();
  16         25  
  16         243  
12              
13 16     16   7051 use Perl::ToPerl6::OptionsProcessor qw();
  16         47  
  16         516  
14 16     16   103 use Perl::ToPerl6::Utils qw{ :characters transformer_long_name transformer_short_name };
  16         26  
  16         958  
15 16     16   3508 use Perl::ToPerl6::Exception::Fatal::Internal qw{ throw_internal };
  16         26  
  16         699  
16 16     16   4833 use Perl::ToPerl6::Exception::Configuration::Generic qw{ throw_generic };
  16         32  
  16         701  
17 16     16   3651 use Perl::ToPerl6::TransformerConfig;
  16         34  
  16         21674  
18              
19             our $VERSION = '0.03';
20              
21             #-----------------------------------------------------------------------------
22              
23             sub new {
24              
25 130     130 1 14002 my ( $class, %args ) = @_;
26 130         345 my $self = bless {}, $class;
27 130         471 $self->_init( %args );
28 126         360 return $self;
29             }
30              
31             #-----------------------------------------------------------------------------
32              
33             sub _init {
34              
35 130     130   289 my ( $self, %args ) = @_;
36             # The profile can be defined, undefined, or an empty string.
37 130 100       646 my $profile = defined $args{-profile} ? $args{-profile} : _find_profile_path();
38 130         474 $self->_load_profile( $profile );
39 126         349 $self->_set_options_processor();
40 126         231 return $self;
41             }
42              
43             #-----------------------------------------------------------------------------
44              
45             sub options_processor {
46              
47 560     560 1 552 my ($self) = @_;
48 560         1798 return $self->{_options_processor};
49             }
50              
51             #-----------------------------------------------------------------------------
52              
53             sub transformer_params {
54              
55 1370     1370 1 1578 my ( $self, $transformer ) = @_;
56              
57 1370         2509 my $short_name = transformer_short_name($transformer);
58              
59 1370         2871 return Perl::ToPerl6::TransformerConfig->new(
60             $short_name,
61             $self->raw_transformer_params($transformer),
62             );
63             }
64              
65             #-----------------------------------------------------------------------------
66              
67             sub raw_transformer_params {
68              
69 1381     1381 1 2491 my ( $self, $transformer ) = @_;
70 1381         1946 my $profile = $self->{_profile};
71 1381   33     4715 my $long_name = ref $transformer || transformer_long_name( $transformer );
72 1381         2762 my $short_name = transformer_short_name( $long_name );
73              
74             return
75             $profile->{$short_name}
76             || $profile->{$long_name}
77             || $profile->{"-$short_name"}
78 1381   100     18380 || $profile->{"-$long_name"}
79             || {};
80             }
81              
82             #-----------------------------------------------------------------------------
83              
84             sub transformer_is_disabled {
85              
86 998     998 1 1653 my ( $self, $transformer ) = @_;
87 998         1271 my $profile = $self->{_profile};
88 998   66     2337 my $long_name = ref $transformer || transformer_long_name( $transformer );
89 998         2277 my $short_name = transformer_short_name( $long_name );
90              
91             return exists $profile->{"-$short_name"}
92 998   100     7051 || exists $profile->{"-$long_name"};
93             }
94              
95             #-----------------------------------------------------------------------------
96              
97             sub transformer_is_enabled {
98              
99 997     997 1 1055 my ( $self, $transformer ) = @_;
100 997         1161 my $profile = $self->{_profile};
101 997   66     2055 my $long_name = ref $transformer || transformer_long_name( $transformer );
102 997         1857 my $short_name = transformer_short_name( $long_name );
103              
104             return exists $profile->{$short_name}
105 997   66     4862 || exists $profile->{$long_name};
106             }
107              
108             #-----------------------------------------------------------------------------
109              
110             sub listed_transformers {
111              
112 121     121 1 192 my ( $self, $transformer ) = @_;
113 121         207 my @normalized_transformer_names = ();
114              
115 121         156 for my $transformer_name ( sort keys %{$self->{_profile}} ) {
  121         769  
116 61         69 $transformer_name =~ s/\A - //xmso; #Chomp leading "-"
117 61         119 my $transformer_long_name = transformer_long_name( $transformer_name );
118 61         91 push @normalized_transformer_names, $transformer_long_name;
119             }
120              
121 121         394 return @normalized_transformer_names;
122             }
123              
124             #-----------------------------------------------------------------------------
125              
126             sub source {
127 913     913 1 863 my ( $self ) = @_;
128              
129 913         1516 return $self->{_source};
130             }
131              
132             sub _set_source {
133 3     3   7 my ( $self, $source ) = @_;
134              
135 3         14 $self->{_source} = $source;
136              
137 3         9 return;
138             }
139              
140             #-----------------------------------------------------------------------------
141             # Begin PRIVATE methods
142              
143             Readonly::Hash my %LOADER_FOR => (
144             ARRAY => \&_load_profile_from_array,
145             DEFAULT => \&_load_profile_from_file,
146             HASH => \&_load_profile_from_hash,
147             SCALAR => \&_load_profile_from_string,
148             );
149              
150             sub _load_profile {
151              
152 130     130   214 my ( $self, $profile ) = @_;
153              
154 130   100     626 my $ref_type = ref $profile || 'DEFAULT';
155 130         1240 my $loader = $LOADER_FOR{$ref_type};
156              
157 130 100       1189 if (not $loader) {
158 1         9 throw_internal qq{Can't load UserProfile from type "$ref_type"};
159             }
160              
161 129         372 $self->{_profile} = $loader->($self, $profile);
162 126         227 return $self;
163             }
164              
165             #-----------------------------------------------------------------------------
166              
167             sub _set_options_processor {
168              
169 126     126   192 my ($self) = @_;
170 126         228 my $profile = $self->{_profile};
171 126   100     549 my $defaults = delete $profile->{__defaults__} || {};
172             $self->{_options_processor} =
173 126         184 Perl::ToPerl6::OptionsProcessor->new( %{ $defaults } );
  126         784  
174 126         278 return $self;
175             }
176              
177             #-----------------------------------------------------------------------------
178              
179             sub _load_profile_from_file {
180 115     115   198 my ( $self, $file ) = @_;
181              
182             # Handle special cases.
183 115 100       341 return {} if not defined $file;
184 90 100       252 return {} if $file eq $EMPTY;
185 87 100       340 return {} if $file eq 'NONE';
186              
187 3         16 $self->_set_source( $file );
188              
189 3         26 my $profile = Config::Tiny->read( $file );
190 3 100       3307 if (not defined $profile) {
191 1         7 my $errstr = Config::Tiny::errstr();
192 1         12 throw_generic
193             message => qq{Could not parse profile "$file": $errstr},
194             source => $file;
195             }
196              
197 2         10 _fix_defaults_key( $profile );
198              
199 2         5 return $profile;
200             }
201              
202             #-----------------------------------------------------------------------------
203              
204             sub _load_profile_from_array {
205 1     1   2 my ( $self, $array_ref ) = @_;
206 1         3 my $joined = join qq{\n}, @{ $array_ref };
  1         4  
207 1         30 my $profile = Config::Tiny->read_string( $joined );
208              
209 1 50       70 if (not defined $profile) {
210 0         0 throw_generic 'Profile error: ' . Config::Tiny::errstr();
211             }
212              
213 1         4 _fix_defaults_key( $profile );
214              
215 1         3 return $profile;
216             }
217              
218             #-----------------------------------------------------------------------------
219              
220             sub _load_profile_from_string {
221 5     5   9 my ( $self, $string ) = @_;
222 5         10 my $profile = Config::Tiny->read_string( ${ $string } );
  5         30  
223              
224 5 100       3000 if (not defined $profile) {
225 2         6 throw_generic 'Profile error: ' . Config::Tiny::errstr();
226             }
227              
228 3         10 _fix_defaults_key( $profile );
229              
230 3         8 return $profile;
231             }
232              
233             #-----------------------------------------------------------------------------
234              
235             sub _load_profile_from_hash {
236 8     8   17 my ( $self, $hash_ref ) = @_;
237 8         29 return $hash_ref;
238             }
239              
240             #-----------------------------------------------------------------------------
241              
242             sub _find_profile_path {
243              
244             #Define default filename
245 1     1   1348 my $rc_file = '.perlmogrifyrc';
246              
247             #Check explicit environment setting
248 1 50       6 return $ENV{PERLMOGRIFY} if exists $ENV{PERLMOGRIFY};
249              
250             #Check current directory
251 0 0       0 return $rc_file if -f $rc_file;
252              
253             #Check home directory
254 0 0       0 if ( my $home_dir = _find_home_dir() ) {
255 0         0 my $path = File::Spec->catfile( $home_dir, $rc_file );
256 0 0       0 return $path if -f $path;
257             }
258              
259             #No profile defined
260 0         0 return;
261             }
262              
263             #-----------------------------------------------------------------------------
264              
265             sub _find_home_dir {
266              
267             # Try using File::HomeDir
268 0 0   0   0 if ( eval { require File::HomeDir } ) {
  0         0  
269 0         0 return File::HomeDir->my_home();
270             }
271              
272             # Check usual environment vars
273 0         0 for my $key (qw(HOME USERPROFILE HOMESHARE)) {
274 0 0       0 next if not defined $ENV{$key};
275 0 0       0 return $ENV{$key} if -d $ENV{$key};
276             }
277              
278             # No home directory defined
279 0         0 return;
280             }
281              
282             #-----------------------------------------------------------------------------
283              
284             # !$%@$%^ Config::Tiny uses a completely non-descriptive name for global
285             # values.
286             sub _fix_defaults_key {
287 6     6   10 my ( $profile ) = @_;
288              
289 6         14 my $defaults = delete $profile->{_};
290 6 100       23 if ($defaults) {
291 3         8 $profile->{__defaults__} = $defaults;
292             }
293              
294 6         12 return;
295             }
296              
297             1;
298              
299             __END__
300              
301             #-----------------------------------------------------------------------------
302              
303             =pod
304              
305             =for stopwords UserProfile
306              
307             =head1 NAME
308              
309             Perl::ToPerl6::UserProfile - The contents of the user's profile, often F<.perlmogrifyrc>.
310              
311              
312             =head1 DESCRIPTION
313              
314             This is a helper class that encapsulates the contents of the user's
315             profile, which is usually stored in a F<.perlmogrifyrc> file. There are
316             no user-serviceable parts here.
317              
318              
319             =head1 INTERFACE SUPPORT
320              
321             This is considered to be a non-public class. Its interface is subject
322             to change without notice.
323              
324              
325             =head1 CONSTRUCTOR
326              
327             =over
328              
329             =item C< new( -profile => $p ) >
330              
331             B<-profile> is the path to the user's profile. If -profile is not
332             defined, then it looks for the profile at F<./.perlmogrifyrc> and then
333             F<$HOME/.perlmogrifyrc>. If neither of those files exists, then the
334             UserProfile is created with default values.
335              
336             This object does not take into account any command-line overrides;
337             L<Perl::ToPerl6::Config|Perl::ToPerl6::Config> does that.
338              
339              
340             =back
341              
342              
343             =head1 METHODS
344              
345             =over
346              
347             =item C< options_processor() >
348              
349             Returns the
350             L<Perl::ToPerl6::OptionsProcessor|Perl::ToPerl6::OptionsProcessor>
351             object for this UserProfile.
352              
353              
354             =item C< transformer_is_disabled( $transformer ) >
355              
356             Given a reference to a L<Perl::ToPerl6::Transformer|Perl::ToPerl6::Transformer>
357             object or the name of one, returns true if the user has disabled that
358             transformer in their profile.
359              
360              
361             =item C< transformer_is_enabled( $transformer ) >
362              
363             Given a reference to a L<Perl::ToPerl6::Transformer|Perl::ToPerl6::Transformer>
364             object or the name of one, returns true if the user has explicitly
365             enabled that transformer in their user profile.
366              
367              
368             =item C< transformer_params( $transformer ) >
369              
370             Given a reference to a L<Perl::ToPerl6::Transformer|Perl::ToPerl6::Transformer>
371             object or the name of one, returns a
372             L<Perl::ToPerl6::TransformerConfig|Perl::ToPerl6::TransformerConfig> for the
373             user's configuration parameters for that transformer.
374              
375              
376             =item C< raw_transformer_params( $transformer ) >
377              
378             Given a reference to a L<Perl::ToPerl6::Transformer|Perl::ToPerl6::Transformer>
379             object or the name of one, returns a reference to a hash of the user's
380             configuration parameters for that transformer.
381              
382              
383             =item C< listed_transformers() >
384              
385             Returns a list of the names of all the Transformers that are mentioned in
386             the profile. The Transformer names will be fully qualified (e.g.
387             Perl::ToPerl6::Foo).
388              
389              
390             =item C< source() >
391              
392             The place where the profile information came from, if available.
393             Usually the path to a F<.perlmogrifyrc>.
394              
395              
396             =back
397              
398              
399             =head1 SEE ALSO
400              
401             L<Perl::ToPerl6::Config|Perl::ToPerl6::Config>,
402             L<Perl::ToPerl6::OptionsProcessor|Perl::ToPerl6::OptionsProcessor>
403              
404              
405             =head1 AUTHOR
406              
407             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
408              
409              
410             =head1 COPYRIGHT
411              
412             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
413              
414             This program is free software; you can redistribute it and/or modify
415             it under the same terms as Perl itself. The full text of this license
416             can be found in the LICENSE file included with this module.
417              
418             =cut
419              
420             # Local Variables:
421             # mode: cperl
422             # cperl-indent-level: 4
423             # fill-column: 78
424             # indent-tabs-mode: nil
425             # c-indentation-style: bsd
426             # End:
427             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :