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 83     295   4061987 use Moo::_Utils;
  83         125  
  83         21354  
6              
7             sub import {
8 311     498   176401 my $class = shift;
9              
10             # the package into which we're importing
11 311         571 my $target = caller;
12              
13             # local meta_fields to package, closed over by the 'around' methods
14 311         512 my @_meta_fields;
15              
16             # has_field function which puts the field definitions into the local @_meta_fields
17             my $has_field = sub {
18 481     635   49804 my ( $name, @options ) = @_;
        601      
        517      
        426      
        354      
        254      
        0      
        0      
        0      
        0      
19 481 50       931 return unless $name;
20 481 100       956 my $names = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
21 481         1998 push @_meta_fields, { name => $_, source => $target, @options } for @$names;
22 311         1302 };
23              
24             # function to insert 'around' modifiers into the calling package
25             # install 'has_field' function into the calling package
26 311         1031 _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 311         21687 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 311         2321 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 456         15399 my ($orig, $self) = (shift, shift);
45 456         7340 return ($self->$orig(), @_meta_fields);
46             }
47 311     311   1238 );
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 311         277979 );
55 311         1022 };
56             # actually install the around modifiers in the caller
57 311         598 $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.03
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