X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FTypes%2FStructured.pm;h=890f1f131e57eec7edd2a15e1e0e6ba5c7588133;hb=abd193e2f4fab1314a1edffb2768cd6dcf831f70;hp=d534f447792fab19a7a042d8eb19c791f9c65852;hpb=91416e91698e60b3349427b4d8b0b4bc2795f59c;p=gitmo%2FMooseX-Types-Structured.git diff --git a/lib/MooseX/Types/Structured.pm b/lib/MooseX/Types/Structured.pm index d534f44..890f1f1 100644 --- a/lib/MooseX/Types/Structured.pm +++ b/lib/MooseX/Types/Structured.pm @@ -4,6 +4,7 @@ use 5.008; use Moose::Util::TypeConstraints; use MooseX::Meta::TypeConstraint::Structured; +use MooseX::Meta::TypeConstraint::Structured::Optional; use MooseX::Types::Structured::OverflowHandler; use MooseX::Types -declare => [qw(Dict Tuple Optional)]; use Sub::Exporter -setup => { exports => [ qw(Dict Tuple Optional slurpy) ] }; @@ -689,6 +690,32 @@ clean and declarative way. =cut +my $Optional = MooseX::Meta::TypeConstraint::Structured::Optional->new( + name => 'MooseX::Types::Structured::Optional', + package_defined_in => __PACKAGE__, + parent => find_type_constraint('Item'), + constraint => sub { 1 }, + constraint_generator => sub { + my ($type_parameter, @args) = @_; + my $check = $type_parameter->_compiled_type_constraint(); + return sub { + my (@args) = @_; + ## Does the arg exist? Something exists if it's a 'real' value + ## or if it is set to undef. + if(exists($args[0])) { + ## If it exists, we need to validate it + $check->($args[0]); + } else { + ## But it's is okay if the value doesn't exists + return 1; + } + } + } +); + +Moose::Util::TypeConstraints::register_type_constraint($Optional); +Moose::Util::TypeConstraints::add_parameterizable_type($Optional); + Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( MooseX::Meta::TypeConstraint::Structured->new( name => "MooseX::Types::Structured::Tuple" , @@ -718,7 +745,7 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( } } else { ## Test if the TC supports null values - unless($type_constraint->check()) { + unless ($type_constraint->is_subtype_of($Optional)) { $_[2]->{message} = $type_constraint->get_message('NULL') if ref $_[2]; return; @@ -777,7 +804,7 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( } } else { ## Test to see if the TC supports null values - unless($type_constraint->check()) { + unless ($type_constraint->is_subtype_of($Optional)) { $_[2]->{message} = $type_constraint->get_message('NULL') if ref $_[2]; return; @@ -805,34 +832,6 @@ Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( ) ); -OPTIONAL: { - my $Optional = Moose::Meta::TypeConstraint::Parameterizable->new( - name => 'MooseX::Types::Structured::Optional', - package_defined_in => __PACKAGE__, - parent => find_type_constraint('Item'), - constraint => sub { 1 }, - constraint_generator => sub { - my ($type_parameter, @args) = @_; - my $check = $type_parameter->_compiled_type_constraint(); - return sub { - my (@args) = @_; - ## Does the arg exist? Something exists if it's a 'real' value - ## or if it is set to undef. - if(exists($args[0])) { - ## If it exists, we need to validate it - $check->($args[0]); - } else { - ## But it's is okay if the value doesn't exists - return 1; - } - } - } - ); - - Moose::Util::TypeConstraints::register_type_constraint($Optional); - Moose::Util::TypeConstraints::add_parameterizable_type($Optional); -} - sub slurpy ($) { my ($tc) = @_; return MooseX::Types::Structured::OverflowHandler->new(