parameterizable packages with Package::Variant
Yesterday I tried to port a Moose App to Moo but got stuck when I found out it is using MooseX::Role::Parameterizable, a module not available in Moo. A quick visit in channel #moose on perl.irc.org later and I was told to give Package::Variant a try.
Few lines of conversation/debugging later and here is my parameterizable HTML::FormHandler role. Package::Variant is an extremly promising module to me that is really helpful when refactoring your code.
declare the HFH role
package MyApp::Role::Form::Step::Value; use Package::Variant importing => ['HTML::FormHandler::Moose::Role'], subs => [ qw/ has_field requires has around before after with / ]; sub make_variant { my ( $class, $target_package, %arguments ) = @_; my $name = exists $arguments{name} ? $arguments{name} : 'value'; has_field $name => ( type => 'Text', required => 0, noupdate => 1, not_nullable => 1, element_attr => { class => 'detect_whitespace' }, ); around '_build_parameter_fields' => sub { my ( $orig, $self ) = @_; return [ @{ $self->$orig }, $name ]; }; } 1;
And here is how you would use it.
package MyApp::Form::Step::CondSubTest; use HTML::FormHandler::Moose; use MyApp::Role::Form::Step::Value; extends 'MyApp::Form::Step::Base'; with 'MyApp::Role::Form::Step::SubTest', Value( name => 'value_a' ), Value( name => 'value_b' ), ; 1;
Leave a comment