File Coverage

blib/lib/Data/MuForm/Meta.pm
Criterion Covered Total %
statement 20 27 74.0
branch 3 4 75.0
condition n/a
subroutine 9 13 69.2
pod n/a
total 32 44 72.7


line stmt bran cond sub pod time code
1             package Data::MuForm::Meta;
2             # ABSTRACT: Meta magic to create 'has_fields'
3              
4              
5 84     296   4196546 use Moo::_Utils;
  84         137  
  84         22188  
6              
7             sub import {
8 314     501   212586 my $class = shift;
9              
10             # the package into which we're importing
11 314         553 my $target = caller;
12              
13             # local meta_fields to package, closed over by the 'around' methods
14 314         560 my @_meta_fields;
15              
16             # has_field function which puts the field definitions into the local @_meta_fields
17             my $has_field = sub {
18 486     640   49925 my ( $name, @options ) = @_;
        606      
        522      
        431      
        354      
        254      
        0      
        0      
        0      
        0      
19 486 50       961 return unless $name;
20 486 100       980 my $names = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
21 486         2017 push @_meta_fields, { name => $_, source => $target, @options } for @$names;
22 314         1279 };
23              
24             # function to insert 'around' modifiers into the calling package
25             # install 'has_field' function into the calling package
26 314         1058 _install_coderef "${target}::has_field" => "MuMeta::has_field" => $has_field;
27              
28             # eval the basic functions into the caller package. It does not work to do these
29             # with '_install_coderef' - C3 gets confused, and they could get cleaned away
30             # 'maybe::next::method' necessary to get it to walk the tree
31             # Note: _field_packages isn't actually used in MuForm code, but is left here
32             # for possible diagnostic use. It will return an array of the packages
33             # into which this code was imported.
34 314         23245 eval "package ${target};
  0            
  0            
  0            
  0            
35             sub _meta_fields { shift->maybe::next::method(\@_) }
36             sub _field_packages { shift->maybe::next::method(\@_) }";
37              
38             # get the 'around' function from the caller
39 314         2399 my $around = $target->can('around');
40             # function to create the 'around' functions. Closes around @_meta_fields.
41             my $apply_modifiers = sub {
42             $around->(
43             _meta_fields => sub {
44 458         15467 my ($orig, $self) = (shift, shift);
45 458         7424 return ($self->$orig(), @_meta_fields);
46             }
47 314     314   1220 );
48             $around->(
49             _field_packages => sub {
50 0         0 my ($orig, $self) = (shift, shift);
51 0         0 my $package = $target;
52 0         0 return ($self->$orig(), $package);
53             }
54 314         290145 );
55 314         1015 };
56             # actually install the around modifiers in the caller
57 314         554 $apply_modifiers->();
58              
59             }
60              
61             1;
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Data::MuForm::Meta - Meta magic to create 'has_fields'
72              
73             =head1 VERSION
74              
75             version 0.04
76              
77             =head1 SYNOPSIS
78              
79             This file imports the 'has_field' sugar into the MuForm form and field
80             packages.
81              
82             Thanks to Matt Trout who provided this method and assistance in
83             getting it working.
84              
85             =head1 NAME
86              
87             Data::MuForm::Meta
88              
89             =head1 AUTHOR
90              
91             Gerda Shank
92              
93             =head1 COPYRIGHT AND LICENSE
94              
95             This software is copyright (c) 2017 by Gerda Shank.
96              
97             This is free software; you can redistribute it and/or modify it under
98             the same terms as the Perl 5 programming language system itself.
99              
100             =cut