Skip to content

Instantly share code, notes, and snippets.

@chrisa
Created January 14, 2014 18:21
Show Gist options
  • Save chrisa/8423068 to your computer and use it in GitHub Desktop.
Save chrisa/8423068 to your computer and use it in GitHub Desktop.
~/Projects/venda/app ➔ diff -u venda-siteperl/MooseX/UndefTolerant/Class.pm Class.pm
--- venda-siteperl/MooseX/UndefTolerant/Class.pm 2014-01-14 18:21:11.000000000 +0000
+++ Class.pm 2014-01-14 18:21:01.000000000 +0000
@@ -22,19 +22,24 @@
my $init_arg = $attr->init_arg;
my $type_constraint = $attr->type_constraint;
- my $tc_says_clean = ($type_constraint && !$type_constraint->check(undef) ? 1 : 0);
# FIXME: not properly sanitizing field names - e.g. consider a field name "Z'ha'dum"
- return ($tc_says_clean ? (
- "if ( exists \$params->{'$init_arg'} && defined \$params->{'$init_arg'} ) {",
- ) : (),
- @source,
- $tc_says_clean ? (
- '} else {',
- "delete \$params->{'$init_arg'};",
- '}',
- ) : (),
- );
+
+ if ($type_constraint && !$type_constraint->check(undef)) {
+ unshift @source,
+ "if ( exists \$params->{'$init_arg'} && defined \$params->{'$init_arg'} ) {";
+
+ if (my @default = $self->_inline_init_attr_from_default($attr, $idx)) {
+ push @source, '} else {', @default, '}';
+ }
+ else {
+ push @source, '} else {', "delete \$params->{'$init_arg'};";
+ push @source, $self->_inline_check_required_attr($attr);
+ push @source, '}';
+ }
+ }
+
+ return @source;
};
no Moose::Role;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment