| 1 | package Data::MuForm::Meta; | ||||||||||||||
| 2 | # ABSTRACT: Meta magic to create 'has_fields' | ||||||||||||||
| 3 | |||||||||||||||
| 4 | |||||||||||||||
| 5 | 84 | ▶ | use Moo::_Utils; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | sub import { | ||||||||||||||
| 8 | 314 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.1
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | # the package into which we're importing | ||||||||||||||
| 11 | 314 | my $target = caller; | |||||||||||||
| 12 | |||||||||||||||
| 13 | # local meta_fields to package, closed over by the 'around' methods | ||||||||||||||
| 14 | 314 | my @_meta_fields; | |||||||||||||
| 15 | |||||||||||||||
| 16 | # has_field function which puts the field definitions into the local @_meta_fields | ||||||||||||||
| 17 | my $has_field = sub { | ||||||||||||||
| 18 | 81 | ▶ | my ( $name, @options ) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
Subroutine
has_field: called CC=4 SCAR=14.1
| |||||||||||||||
| 19 | 81 | ▶ | return unless $name; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 20 | 81 | ▶ | my $names = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 21 | 81 | push @_meta_fields, { name => $_, source => $target, @options } for @$names; | |||||||||||||
| 22 | 314 | }; | |||||||||||||
| 23 | |||||||||||||||
| 24 | # function to insert 'around' modifiers into the calling package | ||||||||||||||
| 25 | # install 'has_field' function into the calling package | ||||||||||||||
| 26 | 314 | _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 | ▶ | eval "package ${target}; | ||||||||||||
|
Subroutine
_field_packages: not called CC=1 SCAR=0.0
Subroutine
_meta_fields: not called CC=1 SCAR=0.0
Subroutine
_field_packages: not called CC=1 SCAR=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 | 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 | ▶ | my ($orig, $self) = (shift, shift); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | 458 | return ($self->$orig(), @_meta_fields); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | 314 | ▶ | ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=2.0
| |||||||||||||||
| 48 | $around->( | ||||||||||||||
| 49 | _field_packages => sub { | ||||||||||||||
| 50 | 0 | ▶ | my ($orig, $self) = (shift, shift); | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 51 | 0 | my $package = $target; | |||||||||||||
| 52 | 0 | return ($self->$orig(), $package); | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | 314 | ); | |||||||||||||
| 55 | 314 | }; | |||||||||||||
| 56 | # actually install the around modifiers in the caller | ||||||||||||||
| 57 | 314 | $apply_modifiers->(); | |||||||||||||
| 58 | |||||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 84 | 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 | ||||||||||||||