| 1 | use 5.014; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Banal::Moosy::Mungers; # git description: a4d662b | ||
| 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 Banal::Util::Mini qw(peek tidy_arrayify); | ||
| 15 | |||
| 16 | use namespace::autoclean; | ||
| 17 | |||
| 18 | use Exporter::Shiny qw( | ||
| 19 | mhs_dict | ||
| 20 | mhs_dictionary | ||
| 21 | |||
| 22 | mhs_fallbacks | ||
| 23 | mhs_lazy_ro | ||
| 24 | mhs_specs | ||
| 25 | |||
| 26 | std_haz_mungers | ||
| 27 | ); | ||
| 28 | |||
| 29 | ####################################### | ||
| 30 | sub std_haz_mungers { | ||
| 31 | ####################################### | ||
| 32 | our %mungers = ( | ||
| 33 | haz => [ sub {; mhs_lazy_ro() } ], | ||
| 34 | haz_bool => [ sub {; mhs_lazy_ro(isa=>'Bool') } ], | ||
| 35 | haz_int => [ sub {; mhs_lazy_ro(isa=>'Int') } ], | ||
| 36 | haz_str => [ sub {; mhs_lazy_ro(isa=>'Str') } ], | ||
| 37 | haz_strs => [ sub {; mhs_lazy_ro(isa=>'ArrayRef[Str]', traits=>['Array'] ) } ], | ||
| 38 | haz_hash => [ sub {; mhs_lazy_ro(isa=>'HashRef', traits=>['Hash'] ) } ], | ||
| 39 | ); | ||
| 40 | %mungers; | ||
| 41 | } | ||
| 42 | |||
| 43 | ####################################### | ||
| 44 | sub mhs_fallbacks { | ||
| 45 | ####################################### | ||
| 46 | require Banal::Moosy::Mungers::DeviseFallbacks; | ||
| 47 | goto &Banal::Moosy::Mungers::DeviseFallbacks::mhs_fallbacks; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | ####################################### | ||
| 52 | sub mhs_lazy_ro { | ||
| 53 | ####################################### | ||
| 54 | mhs_specs( is => 'ro', init_arg => undef, lazy => 1, @_ ); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | ####################################### | ||
| 59 | sub mhs_specs { # Define meta specs for attributes (is, isa, lazy, ...) | ||
| 60 | ####################################### | ||
| 61 | # ATTENTION : Special calling convention and interface defined by MooseX::MungeHas. | ||
| 62 | my $name = $_; # $_ contains the attribute NAME | ||
| 63 | %_ = (@_, %_); # %_ contains the attribute SPECS, whereas @_ contains defaults (prefs) for those specs. | ||
| 64 | wantarray ? (%_) : +{%_} | ||
| 65 | } | ||
| 66 | |||
| 67 | ####################################### | ||
| 68 | sub mhs_dict { &mhs_dictionary } | ||
| 69 | sub mhs_dictionary { | ||
| 70 | # - Lookup meta specs for attributes from a given (src) dictonary; | ||
| 71 | # * Parameters destined to this routine (dict, src/src_dict, dest/dest_dict) will be removed from the context. | ||
| 72 | # * Remaining parameters will win over the values looked up from the src dictionnary. | ||
| 73 | # * Current munge context (%_) wins over all of the above | ||
| 74 | # - [OPTIONALLY] : merge the resulting specs onto a given (dest) dictionary, which may the same as (serc) | ||
| 75 | ####################################### | ||
| 76 | # ATTENTION : Special calling convention and interface defined by MooseX::MungeHas. | ||
| 77 | my $name = $_; # $_ contains the attribute NAME | ||
| 78 | %_ = (@_, %_); # %_ contains the attribute SPECS or params for mungers (including ourselves), | ||
| 79 | # @_ contains defaults. | ||
| 80 | #say STDERR 'Dictionnary access!'; | ||
| 81 | |||
| 82 | my @dict = tidy_arrayify( delete $_{dict} ); | ||
| 83 | my @src = tidy_arrayify( delete $_{src}, delete $_{src_dict}, @dict); | ||
| 84 | my @dest = tidy_arrayify( delete $_{dest}, delete $_{dest_dict}, @dict); | ||
| 85 | my $entry; | ||
| 86 | |||
| 87 | # multiple source dictionaries are supported. | ||
| 88 | foreach my $src (@src) { | ||
| 89 | #say STDERR ' Dictionnary : SOURCE lookup : ...'; | ||
| 90 | next unless defined( $entry = exists $src->{$name} ? $src->{$name} : undef); | ||
| 91 | do { $_{$_} = $entry->{$_} unless exists $_{$_} } for (keys %$entry); | ||
| 92 | } | ||
| 93 | |||
| 94 | # multiple destination dictionaries are supported. | ||
| 95 | foreach my $dest (@dest) { | ||
| 96 | #say STDERR ' Dictionnary : Updating DESTINATION : ...'; | ||
| 97 | my $entry = $dest->{$name} //= +{}; | ||
| 98 | $entry->{$_} = $_{$_} for (keys %_); | ||
| 99 | } | ||
| 100 | |||
| 101 | #say STDERR 'Dictionnary : about to return : ...'; | ||
| 102 | wantarray ? (%_) : +{%_} | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | =pod | ||
| 109 | |||
| 110 | =encoding UTF-8 | ||
| 111 | |||
| 112 | =head1 NAME | ||
| 113 | |||
| 114 | Banal::Moosy::Mungers - Provide several MUNGER functions that may be use in conjunction with C<MooseX::MungeHas>. | ||
| 115 | |||
| 116 | =head1 VERSION | ||
| 117 | |||
| 118 | version 0.001 | ||
| 119 | |||
| 120 | =head1 SYNOPSIS | ||
| 121 | |||
| 122 | =head1 DESCRIPTION | ||
| 123 | |||
| 124 | =for stopwords haz ro | ||
| 125 | |||
| 126 | use Banal::Moosy::Mungers qw(mhs_specs); | ||
| 127 | use Moose; | ||
| 128 | use MooseX::MungeHas { | ||
| 129 | haz => [ sub {; mhs_specs( is => 'ro', init_arg => undef, lazy => 1 ) }, | ||
| 130 | ] | ||
| 131 | }; | ||
| 132 | |||
| 133 | =for stopwords TABULO | ||
| 134 | |||
| 135 | This module provides several mungers that may be use in conjunction with C<MooseX::MungeHas>. | ||
| 136 | |||
| 137 | =head2 EXPORT_OK | ||
| 138 | |||
| 139 | =over 4 | ||
| 140 | |||
| 141 | =item * | ||
| 142 | |||
| 143 | mhs_lazy_ro | ||
| 144 | |||
| 145 | =item * | ||
| 146 | |||
| 147 | mhs_specs | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 SUPPORT | ||
| 152 | |||
| 153 | Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Banal-Moosy-Mungers> | ||
| 154 | (or L<bug-Banal-Moosy-Mungers@rt.cpan.org|mailto:bug-Banal-Moosy-Mungers@rt.cpan.org>). | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Tabulo <tabulo@cpan.org> | ||
| 159 | |||
| 160 | =head1 CONTRIBUTOR | ||
| 161 | |||
| 162 | =for stopwords Ayhan | ||
| 163 | |||
| 164 | Ayhan <dev@tabulo.net> | ||
| 165 | |||
| 166 | =head1 COPYRIGHT AND LICENSE | ||
| 167 | |||
| 168 | This software is copyright (c) 2018 by Tabulo. | ||
| 169 | |||
| 170 | This is free software; you can redistribute it and/or modify it under | ||
| 171 | the same terms as the Perl 5 programming language system itself. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | __END__ |