| 1 |
package HTML::FormHandler::Widget::ApplyRole; |
| 2 |
# ABSTRACT: role to apply widgets |
| 3 |
$HTML::FormHandler::Widget::ApplyRole::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
use File::Spec; |
| 6 |
use Class::MOP; |
| 7 |
use Try::Tiny; |
| 8 |
use Class::Load qw/ load_optional_class /; |
| 9 |
use namespace::autoclean; |
| 10 |
|
| 11 |
our $ERROR; |
| 12 |
|
| 13 |
sub apply_widget_role { |
| 14 |
my ( $self, $target, $widget_name, $dir ) = @_; |
| 15 |
|
| 16 |
my $render_role = $self->get_widget_role( $widget_name, $dir ); |
| 17 |
$render_role->meta->apply($target) if $render_role; |
| 18 |
} |
| 19 |
|
| 20 |
sub get_widget_role { |
| 21 |
my ( $self, $widget_name, $dir ) = @_; |
| 22 |
my $widget_class = $self->widget_class($widget_name); |
| 23 |
my $ldir = $dir ? '::' . $dir . '::' : '::'; |
| 24 |
|
| 25 |
my $widget_ns = $self->widget_name_space; |
| 26 |
my @name_spaces = @$widget_ns; |
| 27 |
push @name_spaces, ('HTML::FormHandler::Widget', 'HTML::FormHandlerX::Widget'); |
| 28 |
my @classes; |
| 29 |
if ( $widget_class =~ s/^\+// ) |
| 30 |
{ |
| 31 |
push @classes, $widget_class; |
| 32 |
} |
| 33 |
foreach my $ns (@name_spaces) { |
| 34 |
push @classes, $ns . $ldir . $widget_class; |
| 35 |
} |
| 36 |
foreach my $try (@classes) { |
| 37 |
return $try if load_optional_class($try); |
| 38 |
} |
| 39 |
die "Can't find $dir widget $widget_class from " . join(", ", @name_spaces); |
| 40 |
} |
| 41 |
|
| 42 |
# this is for compatibility with widget names like 'radio_group' |
| 43 |
# RadioGroup, Textarea, etc. also work |
| 44 |
sub widget_class { |
| 45 |
my ( $self, $widget ) = @_; |
| 46 |
return unless $widget; |
| 47 |
if($widget eq lc $widget) { |
| 48 |
$widget =~ s/^(\w{1})/\u$1/g; |
| 49 |
$widget =~ s/_(\w{1})/\u$1/g; |
| 50 |
} |
| 51 |
return $widget; |
| 52 |
} |
| 53 |
|
| 54 |
use namespace::autoclean; |
| 55 |
1; |
| 56 |
|
| 57 |
__END__ |
| 58 |
|
| 59 |
=pod |
| 60 |
|
| 61 |
=encoding UTF-8 |
| 62 |
|
| 63 |
=head1 NAME |
| 64 |
|
| 65 |
HTML::FormHandler::Widget::ApplyRole - role to apply widgets |
| 66 |
|
| 67 |
=head1 VERSION |
| 68 |
|
| 69 |
version 0.410002 |
| 70 |
|
| 71 |
=head1 AUTHOR |
| 72 |
|
| 73 |
FormHandler Contributors - see HTML::FormHandler |
| 74 |
|
| 75 |
=head1 COPYRIGHT AND LICENSE |
| 76 |
|
| 77 |
This software is copyright (c) 2017 by Gerda Shank. |
| 78 |
|
| 79 |
This is free software; you can redistribute it and/or modify it under |
| 80 |
the same terms as the Perl 5 programming language system itself. |
| 81 |
|
| 82 |
=cut |