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