You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm a big fan of using roles, but at present (I believe) that Form::Tiny only supports extension via inheritance.
I've emulated roles via Package::Variant. The basic idea is to have a set of pre-packaged form fields (in the following code, I call them form fragments) and use Package::Variant to assemble a Form::Tiny class.
The Package::Variant class looks like this:
package FormGenerate;
use Exporter::Tiny 'mkopt';
use Package::Variant
importing => [ 'Form::Tiny' => [ plugins => ( ... ) ] ], # optional list of plugins
subs => [
'form_field', 'form_hook', 'extends', 'form_filter', 'form_validator', 'option', 'optargs_opts',
];
sub make_variant ( $class, $target_package, $package, @args ) {
for my ( $entry ) ( mkopt( \@args )->@* ) {
my ( $cmd, $args ) = $entry->@*;
no strict 'refs';
$cmd->( ( $args // [] )->@* );
}
}
sub make_variant_package_name ( $class, $package, @args ) {
return $package;
}
1;
The fragments are stored in a separate utility module:
package FormFragments;
use Types::TypeTiny 'BoolLike';
use Types::Common::String 'NonEmptyStr';
use FormGenerate;
use Exporter::Shiny 'FormFactory';
my %fragments = (
Output => [
form_field => [
output => (
type => NonEmptyStr,
required => 1,
),
],
],
PlotDirs => [
form_field => [ dir => ( type => NonEmptyStr ) ],
form_field => [ subdir => ( type => NonEmptyStr ) ],
],
TmpClean => [
form_field => [ 'tmp_clean' => ( type => BoolLike, default => sub { true } ) ],
],
PlotIt => [
form_field => [ 'it' => ( type => BoolLike, default => sub { false } ) ],
],
);
my $FormName = 'GeneratedForm0000';
sub FormFactory ( $base, @fragments ) {
return FormGenerate(
++$FormName,
extends => [ "Form::$base" ],
optargs_opts => [ inherit_optargs => true ],
map { ( $fragments{$_} // croak( "unknown fragment: $_" ) )->@* } @fragments,
)->new;
}
1;
I'm a big fan of using roles, but at present (I believe) that
Form::Tiny
only supports extension via inheritance.I've emulated roles via
Package::Variant
. The basic idea is to have a set of pre-packaged form fields (in the following code, I call them form fragments) and usePackage::Variant
to assemble aForm::Tiny
class.The
Package::Variant
class looks like this:The fragments are stored in a separate utility module:
And the composition looks like this:
To make things easier, my implementation requires a preexisting
Form::Tiny
class which is sub-classed.[The code was extracted and simplified from working code, so might have some syntax issues]
The text was updated successfully, but these errors were encountered: