| 1 | use 5.014; # because we use the 'non-destructive substitution' feature (s///r) | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 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<MooseX::MungeHas>. | ||
| 8 | # KEYWORDS: Munge Has has MungeHas MooseX::MungeHas Moose MooseX Moo MooX | ||
| 9 | |||
| 10 | our $VERSION = '0.001'; | ||
| 11 | # AUTHORITY | ||
| 12 | |||
| 13 | use Data::Printer; # DEBUG purposes. | ||
| 14 | use Scalar::Util qw(reftype); | ||
| 15 | use List::Util 1.45 qw(pairs); | ||
| 16 | |||
| 17 | use Banal::Util::Mini qw(peek tidy_arrayify); | ||
| 18 | |||
| 19 | use namespace::autoclean; | ||
| 20 | use Exporter::Shiny qw( mhs_fallbacks ); | ||
| 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 | my $name = $_; # $_ contains the attribute NAME | ||
| 28 | %_ = (@_, %_); # %_ 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 | my $fbo_detected = exists $_{fallback}; | ||
| 34 | my %fbo = %{ delete( $_{fallback} ) // +{} }; | ||
| 35 | my $disabled = delete( $_{no_fallback} ) // peek(\%fbo, [qw(disable disabled)], 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 | author_specific => [ map {; ($_, 'lookup_' . $_ ) } qw(author author_specific author_prefs author_specific_prefs author_defaults author_settings) ], | ||
| 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 | while ( my ($k, $v) = (each %mappings) ) { | ||
| 71 | my @eqv = tidy_arrayify($v); | ||
| 72 | next SETTING if !@eqv; | ||
| 73 | |||
| 74 | my @array = (); | ||
| 75 | HASH: | ||
| 76 | foreach my $h (\%fbo, \%_) { | ||
| 77 | foreach my $e (@eqv) { #(grep {; $_ ne $k }(@eqv)) { | ||
| 78 | push @array, tidy_arrayify( delete($h->{$e}) ) if exists $h->{$e}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | @array = tidy_arrayify(@array); | ||
| 83 | SWITCH: | ||
| 84 | for (scalar(@array)) { | ||
| 85 | $_ == 0 and do { delete $fbo{$k}; last SWITCH }; # no need to keep it around if it is empty. | ||
| 86 | $_ == 1 and do { $fbo{$k} = pop @array; last SWITCH }; # It's prettier | ||
| 87 | $_ > 1 and do { $fbo{$k} = [ @array ]; last SWITCH }; # multiple items. | ||
| 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 | my @handles; | ||
| 95 | # 'delete' is used because we may end up with an empty list in the end. | ||
| 96 | my @aliases = tidy_arrayify( (delete $fbo{alias}) // [] ); | ||
| 97 | @aliases = map { | ||
| 98 | my $alias = $_; | ||
| 99 | if ( (reftype ($alias) // '') eq 'HASH') { | ||
| 100 | push @handles, ( map {; $_->value ? (@$_) : () } pairs %$alias); # push only those kv entries with a true value. | ||
| 101 | ( sort keys %$alias ); | ||
| 102 | } else { | ||
| 103 | $_ | ||
| 104 | } | ||
| 105 | } @aliases; | ||
| 106 | |||
| 107 | @aliases = tidy_arrayify( @aliases ); | ||
| 108 | |||
| 109 | $fbo{alias} = [@aliases] if scalar(@aliases); | ||
| 110 | $_{handles} = +{ @handles, %{ $_{handles} // +{} } } if scalar(@handles); | ||
| 111 | |||
| 112 | |||
| 113 | # Final determination of fallback setup status (enabled or disabled) | ||
| 114 | my $enabled = ($fbo_detected || !!%fbo) && !exists($_{default}); # && !$disabled; | ||
| 115 | $enabled //= 0; | ||
| 116 | |||
| 117 | # say STDERR "Fallback setup for attribute '$name' status : { enabled => $enabled } : " . np %fbo; | ||
| 118 | |||
| 119 | # Do the actual fallback setup. | ||
| 120 | if ( $enabled ) { | ||
| 121 | # say STDERR " ==> Setting up a 'default' subroutine for '$name' since { enabled => $enabled }"; | ||
| 122 | # $fbo{metam} //= +{%_}; | ||
| 123 | $fbo{isam} //= "$_{isa}"; # We need the stringification! Somehow, at this point MungeHas manages to make this into an oject. | ||
| 124 | $fbo{name} //= "$name"; | ||
| 125 | my $m = $fbo{method} // '_fallback'; | ||
| 126 | $_{lazy} //= 1, | ||
| 127 | $_{default} = sub { $_[0]->$m( \%fbo ) } | ||
| 128 | } | ||
| 129 | |||
| 130 | #'.. cannot have a lazy attribute without specifying a default' | ||
| 131 | delete($_{lazy}) unless exists $_{default}; | ||
| 132 | |||
| 133 | 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<MooseX::MungeHas>. | ||
| 145 | |||
| 146 | =head1 VERSION | ||
| 147 | |||
| 148 | version 0.001 | ||
| 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<MooseX::MungeHas>. | ||
| 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<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Banal-Moosy-Mungers> | ||
| 180 | (or L<bug-Banal-Moosy-Mungers@rt.cpan.org|mailto:bug-Banal-Moosy-Mungers@rt.cpan.org>). | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Tabulo <tabulo@cpan.org> | ||
| 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__ |