1. Shlomi Fish
  2. p5-moox-late

Commits

Toby Inkster  committed 5dff112

get type constraints to inflate properly

  • Participants
  • Parent commits f1187ab
  • Branches default

Comments (0)

Files changed (2)

File lib/MooX/late.pm

View file
 				CodeRef   => sub { ref($_[0]) eq 'CODE' },
 				RegexpRef => sub { ref($_[0]) eq 'Regexp' },
 				GlobRef   => sub { ref($_[0]) eq 'GLOB' },
-				FileHandle=> sub { Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa('IO::Handle') },
+				FileHandle=> sub {
+					Scalar::Util::openhandle($_[0]) or
+					blessed($_[0]) && $_[0]->isa('IO::Handle');
+				},
 				Object    => sub { blessed($_[0]) },
 				ClassName => sub { is_module_name($_[0]) },
 				RoleName  => sub { is_module_name($_[0]) },
 			{
 				return sub { !defined($_[0]) or $inner->($_[0]) };
 			}
+			if ($outer eq 'ScalarRef')
+			{
+				return sub {
+					return unless ref $_[0] eq 'SCALAR';
+					$inner->(${$_[0]});
+				};
+			}
 			if ($outer eq 'ArrayRef')
 			{
 				return sub {
 					return 1;
 				};
 			}
+			
+			return sub { 1 };
 		}
 		
 		if (is_module_name($tc))
 	
 	sub _fatal_type_constraint
 	{
-		my $tc = _type_constraint(my $tc_name = shift);
-		return sub { 1 } unless $tc;
-		return sub { $tc->($_[0]) or die "value '$_[0]' is not a $tc_name" };
+		my $tc    = _type_constraint(my $tc_name = shift);
+		my $fatal = $tc
+			? sub { $tc->($_[0]) or die "value '$_[0]' is not a $tc_name" }
+			: sub { 1 };
+		
+		# For inflation
+		$Moo::HandleMoose::TYPE_MAP{$fatal} = sub {
+			Moose::Util::TypeConstraints::find_or_parse_type_constraint($tc_name)
+		};
+		
+		return $fatal;
 	}
 }
 

File t/02inflation.t

View file
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN {
+	package Local::Class;
+	use Moo;
+	use MooX::late;
+	has foo => (is => 'ro', isa => 'Str', default => 'foo');
+};
+
+ok not eval {
+	my $obj = Local::Class->new(foo => [])
+};
+
+eval {
+	require Moose;
+	
+	my $foo = Local::Class->meta->get_attribute('foo');
+	is(
+		$foo->type_constraint->name,
+		'Str',
+	);
+};
+
+done_testing;