File Coverage

blib/lib/Banal/Mini/Utils/MungeHas.pm
Criterion Covered Total %
statement 20 47 42.5
branch 0 10 0.0
condition 0 2 0.0
subroutine 7 18 38.8
pod 0 5 0.0
total 27 82 32.9


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