FormHandler/Traits.pm

filter: Statementstmt 91.9% 57 / 62 Branchbran 55.5% 10 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 5 totaltotal 79.5% 78 / 98 CC 11 SCAR 12.5
CC
11
Cov
83%
transform_trait
8.1
with_traits
3.0
_anon_cache_key
2.1
CRAP 3.5
« FormHandler/TraitFor/Captcha.pm ↑ Summary FormHandler/Widget/Wrapper/TableInline.pm »
1 package HTML::FormHandler::Traits;
2 # ABSTRACT: customized replacement for MooseX::Traits
3 149 $HTML::FormHandler::Traits::VERSION = '0.410001';
4 149 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 149 ▶use Class::Load qw/ load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 149 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 149 has '_trait_namespace' => (
9 init_arg => undef,
10 isa => 'Str',
11 is => 'bare',
12 );
13  
14 149 my %COMPOSED_CLASS_INDEX;
15 # {
16 # 'HTML::FormHandler::Field::Text' => { 'Role|Another::Role' => 1 },
17 # 'HTML::FormHandler::Field::Select' => { 'My::Role' => 2,
18 # 'My::Role|Your::Role' => 3 },
19 # }
20 my %COMPOSED_META;
21 149 my $composed_index = 0;
22  
23 sub resolve_traits {
24 1713 ▶ my ( $class, @traits ) = @_;
Subroutine resolve_traits: called CC=2 SCAR=7.1
Pod undocumented
25  
26 return map {
27 1713 my $orig = $_;
28 2845 ▶ if ( !ref $orig ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $orig) { } 2845 0 2845
29 2845 my $transformed = transform_trait( $class, $orig );
30 2845 load_class($transformed);
31 2845 $transformed;
32 }
33 else {
34 0 $orig;
35 }
36 } @traits;
37 }
38  
39 sub transform_trait {
40 2845 ▶ my ( $class, $name ) = @_;
Subroutine transform_trait: called CC=5 SCAR=20.9
Pod undocumented
41 2845 ▶ return $1 if $name =~ /^[+](.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^[+](.+)$/ 0 2845 2845
42  
43 2845 my $namespace = $class->meta->find_attribute_by_name('_trait_namespace');
44 2845 my $base;
45 2845 ▶ if ( $namespace->has_default ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace->has_default) 0 2845 2845
46 0 $base = $namespace->default;
47 0 ▶ if ( ref $base eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $base eq "CODE") 0 0 0
48 0 $base = $base->();
49 }
50 }
51  
52 2845 ▶ return $name unless $base;
Branch: 1 branch
BranchTrueFalseTotal
unless $base 2845 0 2845
53 0 return join '::', $base, $name;
54 }
55  
56 sub composed_class_name {
57 1713 ▶ my (%options) = @_;
Subroutine composed_class_name: called CC=2 SCAR=6.9
Pod undocumented
58  
59 1713 my $class = $options{class};
60 1713 my $cache_key = _anon_cache_key( $options{roles} );
61  
62 1713 my $index = $COMPOSED_CLASS_INDEX{$class}{$cache_key};
63 1713 ▶ if ( defined $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 1101 612 1713
64 1101 return "${class}::$index";
65 }
66 612 $index = ++$composed_index;
67 612 $COMPOSED_CLASS_INDEX{$class}{$cache_key} = $index;
68 612 return "${class}::$index";
69 }
70  
71 sub _anon_cache_key {
72 # Makes something like Role|Role::1
73 1713 ▶ return join( '|', @{ $_[0] || [] } );
Subroutine _anon_cache_key: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or [] 0 1713 1713
74 }
75  
76 sub with_traits {
77 1713 ▶ my ( $class, @traits ) = @_;
Subroutine with_traits: called CC=3 SCAR=11.0
Pod undocumented
78  
79 1713 @traits = resolve_traits( $class, @traits );
80 1713 ▶ return $class->meta unless ( scalar @traits );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @traits 0 1713 1713
81  
82 1713 my $class_name = $class->meta->name;
83 1713 my $new_class_name = composed_class_name( class => $class_name, roles => \@traits, );
84 1713 my $meta;
85 1713 ▶ if ( $meta = $COMPOSED_META{$new_class_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta = $COMPOSED_META{$new_class_name}) { } 1101 612 1713
86 1101 return $meta->name;
87 }
88 else {
89 612 $meta = $class->meta->create(
90 $new_class_name,
91 superclasses => [$class_name],
92 roles => \@traits,
93 );
94 612 $COMPOSED_META{$new_class_name} = $meta;
95 612 return $meta->name;
96 }
97 }
98  
99 sub new_with_traits {
100 9 ▶ my ( $class, %args ) = @_;
Subroutine new_with_traits: called CC=2 SCAR=7.0
Pod undocumented
101  
102 9 ▶ my $traits = delete $args{traits} || [];
Condition50%1/2
delete $args{'traits'} || []
l!l
90
MC/DC0%0/1
delete $args{'traits'} || []
delete $args{'traits'}
Truth table50%1/2
A
A= delete $args{'traits'}
Aresult
11
00
103 9 my $new_class = $class->with_traits(@$traits);
104 9 my $constructor = $new_class->meta->constructor_name;
105 9 return $new_class->$constructor(%args);
106 }
107  
108  
109 149 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 149 1;
111  
112 __END__
113  
114 =pod
115  
116 =encoding UTF-8
117  
118 =head1 NAME
119  
120 HTML::FormHandler::Traits - customized replacement for MooseX::Traits
121  
122 =head1 VERSION
123  
124 version 0.410001
125  
126 =head1 SYNOPSIS
127  
128 Use to get a new composed class with traits:
129  
130 my $class = My::Form->with_traits( 'My::Trait', 'Another::Trait' );
131 my $form = $class->new;
132  
133 =head1 AUTHOR
134  
135 FormHandler Contributors - see HTML::FormHandler
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 This software is copyright (c) 2017 by Gerda Shank.
140  
141 This is free software; you can redistribute it and/or modify it under
142 the same terms as the Perl 5 programming language system itself.
143  
144 =cut
« FormHandler/TraitFor/Captcha.pm ↑ Summary FormHandler/Widget/Wrapper/TableInline.pm »