| 1 | package HTML::FormHandler::Traits; | ||||||||||||
| 2 | # ABSTRACT: customized replacement for MooseX::Traits | ||||||||||||
| 3 | 150 | $HTML::FormHandler::Traits::VERSION = '0.410002'; | |||||||||||
| 4 | 150 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 150 | ▶ | use Class::Load qw/ load_class /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 150 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 150 | has '_trait_namespace' => ( | |||||||||||
| 9 | init_arg => undef, | ||||||||||||
| 10 | isa => 'Str', | ||||||||||||
| 11 | is => 'bare', | ||||||||||||
| 12 | ); | ||||||||||||
| 13 | |||||||||||||
| 14 | 150 | 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 | 150 | my $composed_index = 0; | |||||||||||
| 22 | |||||||||||||
| 23 | 1719 | ▶ | sub resolve_traits { | ||||||||||
|
Subroutine
resolve_traits: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 24 | 1719 | my ( $class, @traits ) = @_; | |||||||||||
| 25 | |||||||||||||
| 26 | return map { | ||||||||||||
| 27 | 1719 | my $orig = $_; | |||||||||||
| 28 | 2855 | ▶ | if ( !ref $orig ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 2855 | my $transformed = transform_trait( $class, $orig ); | |||||||||||
| 30 | 2855 | load_class($transformed); | |||||||||||
| 31 | 2855 | $transformed; | |||||||||||
| 32 | } | ||||||||||||
| 33 | else { | ||||||||||||
| 34 | 0 | $orig; | |||||||||||
| 35 | } | ||||||||||||
| 36 | } @traits; | ||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 2855 | ▶ | sub transform_trait { | ||||||||||
|
Subroutine
transform_trait: called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||
| 40 | 2855 | my ( $class, $name ) = @_; | |||||||||||
| 41 | 2855 | ▶ | return $1 if $name =~ /^[+](.+)$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | |||||||||||||
| 43 | 2855 | my $namespace = $class->meta->find_attribute_by_name('_trait_namespace'); | |||||||||||
| 44 | 2855 | my $base; | |||||||||||
| 45 | 2855 | ▶ | if ( $namespace->has_default ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 0 | $base = $namespace->default; | |||||||||||
| 47 | 0 | ▶ | if ( ref $base eq 'CODE' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 0 | $base = $base->(); | |||||||||||
| 49 | } | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 2855 | ▶ | return $name unless $base; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | return join '::', $base, $name; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 1719 | ▶ | sub composed_class_name { | ||||||||||
|
Subroutine
composed_class_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 57 | 1719 | my (%options) = @_; | |||||||||||
| 58 | |||||||||||||
| 59 | 1719 | my $class = $options{class}; | |||||||||||
| 60 | 1719 | my $cache_key = _anon_cache_key( $options{roles} ); | |||||||||||
| 61 | |||||||||||||
| 62 | 1719 | my $index = $COMPOSED_CLASS_INDEX{$class}{$cache_key}; | |||||||||||
| 63 | 1719 | ▶ | if ( defined $index ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 1102 | return "${class}::$index"; | |||||||||||
| 65 | } | ||||||||||||
| 66 | 617 | $index = ++$composed_index; | |||||||||||
| 67 | 617 | $COMPOSED_CLASS_INDEX{$class}{$cache_key} = $index; | |||||||||||
| 68 | 617 | return "${class}::$index"; | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 1719 | ▶ | sub _anon_cache_key { | ||||||||||
|
Subroutine
_anon_cache_key: called CC=2 SCAR=7.2
| |||||||||||||
| 72 | # Makes something like Role|Role::1 | ||||||||||||
| 73 | 1719 | ▶ | return join( '|', @{ $_[0] || [] } ); | ||||||||||
|
Condition50%1/2
$_[0] || []
MC/DC0%0/1
$_[0] || []
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 1719 | ▶ | sub with_traits { | ||||||||||
|
Subroutine
with_traits: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 77 | 1719 | my ( $class, @traits ) = @_; | |||||||||||
| 78 | |||||||||||||
| 79 | 1719 | @traits = resolve_traits( $class, @traits ); | |||||||||||
| 80 | 1719 | ▶ | return $class->meta unless ( scalar @traits ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | |||||||||||||
| 82 | 1719 | my $class_name = $class->meta->name; | |||||||||||
| 83 | 1719 | my $new_class_name = composed_class_name( class => $class_name, roles => \@traits, ); | |||||||||||
| 84 | 1719 | my $meta; | |||||||||||
| 85 | 1719 | ▶ | if ( $meta = $COMPOSED_META{$new_class_name} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 1102 | return $meta->name; | |||||||||||
| 87 | } | ||||||||||||
| 88 | else { | ||||||||||||
| 89 | 617 | $meta = $class->meta->create( | |||||||||||
| 90 | $new_class_name, | ||||||||||||
| 91 | superclasses => [$class_name], | ||||||||||||
| 92 | roles => \@traits, | ||||||||||||
| 93 | ); | ||||||||||||
| 94 | 617 | $COMPOSED_META{$new_class_name} = $meta; | |||||||||||
| 95 | 617 | return $meta->name; | |||||||||||
| 96 | } | ||||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 9 | ▶ | sub new_with_traits { | ||||||||||
|
Subroutine
new_with_traits: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 100 | 9 | my ( $class, %args ) = @_; | |||||||||||
| 101 | |||||||||||||
| 102 | 9 | ▶ | my $traits = delete $args{traits} || []; | ||||||||||
|
Condition50%1/2
delete $args{'traits'} || []
MC/DC0%0/1
delete $args{'traits'} || []
delete $args{'traits'}
Truth table50%1/2
A
A= delete $args{'traits'}
| |||||||||||||
| 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 | 150 | ▶ | no Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 110 | 150 | 1; | |||||||||||
| 111 | |||||||||||||
| 112 | __END__ | ||||||||||||