File Coverage

blib/lib/Banal/Moosy/Mungers/DeviseFallbacks.pm
Criterion Covered Total %
statement 26 75 34.6
branch 0 24 0.0
condition 0 28 0.0
subroutine 9 11 81.8
pod 0 1 0.0
total 35 139 25.1


line stmt bran cond sub pod time code
1 1     1   268663 use 5.014; # because we use the 'non-destructive substitution' feature (s///r)
  1         17  
2 1     1   16 use strict;
  1         11  
  1         53  
3 1     1   7 use warnings;
  1         3  
  1         146  
4              
5             package Banal::Moosy::Mungers::DeviseFallbacks;
6             # vim: set ts=2 sts=2 sw=2 tw=115 et :
7             # ABSTRACT: Provide several MUNGER functions that may be use in conjunction with C.
8             # KEYWORDS: Munge Has has MungeHas MooseX::MungeHas Moose MooseX Moo MooX
9              
10             our $VERSION = '0.002';
11             # AUTHORITY
12              
13 1     1   728 use Data::Printer; # DEBUG purposes.
  1         42013  
  1         7  
14 1     1   131 use Scalar::Util qw(reftype);
  1         2  
  1         53  
15 1     1   6 use List::Util 1.45 qw(pairs);
  1         18  
  1         157  
16              
17 1     1   454 use Banal::Util::Mini qw(peek tidy_arrayify);
  1         38132  
  1         8  
18              
19 1     1   985 use namespace::autoclean;
  1         3  
  1         8  
20 1     1   63 use Exporter::Shiny qw( mhs_fallbacks );
  1         3  
  1         5  
21              
22              
23             #######################################
24             sub mhs_fallbacks { # Munge attr specs so that the attribute may use a 'fallback' routine for its 'default' sub.
25             #######################################
26             # ATTENTION : Special calling convention and interface defined by MooseX::MungeHas.
27 0     0 0   my $name = $_; # $_ contains the attribute NAME
28 0           %_ = (@_, %_); # %_ contains the attribute SPECS, whereas @_ contains defaults (prefs) for those specs.
29              
30             # say STDERR 'Fallback munger : about to start munging : ...';
31              
32             # Initial determination of some key properties involving fallback setup.
33 0           my $fbo_detected = exists $_{fallback};
34 0   0       my %fbo = %{ delete( $_{fallback} ) // +{} };
  0            
35 0   0       my $disabled = delete( $_{no_fallback} ) // peek(\%fbo, [qw(disable disabled)], 0) // 0;
      0        
36              
37             # Grok some properties (either directly from the 'has' parameters (%_), or from the 'fallback' hash (%fbo)
38             my %mappings = (
39             # Aliases
40             alias => [qw(aka alias aliases) ],
41              
42             #Actual fallback routines or values
43             apriori => [qw(apriori primo) ],
44             mid => [qw(mid nrm normally) ],
45             final => [qw(def last fin final finally) ],
46             via => [qw(via) ],
47              
48              
49             # Fallback source specifiers
50 0           author_specific => [ map {; ($_, 'lookup_' . $_ ) } qw(author author_specific author_prefs author_specific_prefs author_defaults author_settings) ],
  0            
51              
52             # Special handling
53             no_implicit => [qw(no_implicit) ],
54             blanker_token => [qw(blanker blankers blanker_token blanker_tokens ) ],
55             implicit_suffix => [qw(implicit_suffix implicit_suffixes implicit_suffices implicit_sfx ) ],
56              
57             # wants
58             multivalue => [qw(multivalue) ],
59              
60             # Processing to be done on the result
61             grep => [qw(grep greps filter filters) ],
62             sort => [qw(sort) ],
63             uniq => [qw(uniq unique) ],
64             no_uniq => [qw(no_uniq no_unique) ],
65             );
66              
67             #say STDERR 'Fallback munger : about to start groking SETTINGS : ...';
68              
69             SETTING:
70 0           while ( my ($k, $v) = (each %mappings) ) {
71 0           my @eqv = tidy_arrayify($v);
72 0 0         next SETTING if !@eqv;
73              
74 0           my @array = ();
75             HASH:
76 0           foreach my $h (\%fbo, \%_) {
77 0           foreach my $e (@eqv) { #(grep {; $_ ne $k }(@eqv)) {
78 0 0         push @array, tidy_arrayify( delete($h->{$e}) ) if exists $h->{$e};
79             }
80             }
81              
82 0           @array = tidy_arrayify(@array);
83             SWITCH:
84 0           for (scalar(@array)) {
85 0 0         $_ == 0 and do { delete $fbo{$k}; last SWITCH }; # no need to keep it around if it is empty.
  0            
  0            
86 0 0         $_ == 1 and do { $fbo{$k} = pop @array; last SWITCH }; # It's prettier
  0            
  0            
87 0 0         $_ > 1 and do { $fbo{$k} = [ @array ]; last SWITCH }; # multiple items.
  0            
  0            
88             }
89             }
90              
91             # Process aka/alias properties that are HASH references, which implies them being added to the 'handles' hash parameter.
92             # This helps with the DRY principle, and is done regardless of fallback being enabled or not.
93              
94 0           my @handles;
95             # 'delete' is used because we may end up with an empty list in the end.
96 0   0       my @aliases = tidy_arrayify( (delete $fbo{alias}) // [] );
97             @aliases = map {
98 0           my $alias = $_;
  0            
99 0 0 0       if ( (reftype ($alias) // '') eq 'HASH') {
100 0 0         push @handles, ( map {; $_->value ? (@$_) : () } pairs %$alias); # push only those kv entries with a true value.
  0            
101 0           ( sort keys %$alias );
102             } else {
103 0           $_
104             }
105             } @aliases;
106              
107 0           @aliases = tidy_arrayify( @aliases );
108              
109 0 0         $fbo{alias} = [@aliases] if scalar(@aliases);
110 0 0 0       $_{handles} = +{ @handles, %{ $_{handles} // +{} } } if scalar(@handles);
  0            
111              
112              
113             # Final determination of fallback setup status (enabled or disabled)
114 0   0       my $enabled = ($fbo_detected || !!%fbo) && !exists($_{default}); # && !$disabled;
115 0   0       $enabled //= 0;
116              
117             # say STDERR "Fallback setup for attribute '$name' status : { enabled => $enabled } : " . np %fbo;
118              
119             # Do the actual fallback setup.
120 0 0         if ( $enabled ) {
121             # say STDERR " ==> Setting up a 'default' subroutine for '$name' since { enabled => $enabled }";
122             # $fbo{metam} //= +{%_};
123 0   0       $fbo{isam} //= "$_{isa}"; # We need the stringification! Somehow, at this point MungeHas manages to make this into an oject.
124 0   0       $fbo{name} //= "$name";
125 0   0       my $m = $fbo{method} // '_fallback';
126             $_{lazy} //= 1,
127 0     0     $_{default} = sub { $_[0]->$m( \%fbo ) }
128 0   0       }
129              
130             #'.. cannot have a lazy attribute without specifying a default'
131 0 0         delete($_{lazy}) unless exists $_{default};
132              
133 0 0         wantarray ? (%_) : +{%_}
134             }
135              
136             1;
137              
138             =pod
139              
140             =encoding UTF-8
141              
142             =head1 NAME
143              
144             Banal::Moosy::Mungers::DeviseFallbacks - Provide several MUNGER functions that may be use in conjunction with C.
145              
146             =head1 VERSION
147              
148             version 0.002
149              
150             =head1 SYNOPSIS
151              
152             =head1 DESCRIPTION
153              
154             =for stopwords haz ro
155              
156             use Moose;
157             use MooseX::MungeHas {
158             haz => [ sub {; mhs_specs( is => 'ro', init_arg => undef, lazy => 1 ) },
159             sub {; mhs_fallbacks() },
160             ]
161             };
162              
163             =for stopwords TABULO
164              
165             This module provides several mungers that may be use in conjunction with C.
166              
167             =head2 EXPORT_OK
168              
169             =over 4
170              
171             =item *
172              
173             mhs_fallbacks
174              
175             =back
176              
177             =head1 SUPPORT
178              
179             Bugs may be submitted through L
180             (or L).
181              
182             =head1 AUTHOR
183              
184             Tabulo
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is copyright (c) 2018 by Tabulo.
189              
190             This is free software; you can redistribute it and/or modify it under
191             the same terms as the Perl 5 programming language system itself.
192              
193             =cut
194              
195             __END__