From: Matt S Trout Date: Tue, 17 Apr 2012 17:58:12 +0000 (+0000) Subject: handle Moo coercions X-Git-Tag: v0.091000~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0cc17078abbec2de7014d5fb1c8d5849d1a1ac6d;p=gitmo%2FMoo.git handle Moo coercions --- diff --git a/lib/Moo/HandleMoose.pm b/lib/Moo/HandleMoose.pm index 1103126..66f58bd 100644 --- a/lib/Moo/HandleMoose.pm +++ b/lib/Moo/HandleMoose.pm @@ -2,6 +2,7 @@ package Moo::HandleMoose; use strictures 1; use Moo::_Utils; +use B qw(perlstring); our %TYPE_MAP; @@ -11,7 +12,8 @@ sub import { return if $SETUP_DONE; inject_all(); $SETUP_DONE = 1; } sub inject_all { require Class::MOP; - inject_fake_metaclass_for($_) for grep $_ ne 'Moo::Object', keys %Moo::MAKERS; + inject_fake_metaclass_for($_) + for grep $_ ne 'Moo::Object', do { no warnings 'once'; keys %Moo::MAKERS }; inject_fake_metaclass_for($_) for keys %Moo::Role::INFO; require Moose::Meta::Method::Constructor; @Moo::HandleMoose::FakeConstructor::ISA = 'Moose::Meta::Method::Constructor'; @@ -66,6 +68,22 @@ sub inject_real_metaclass_for { ); } }; + die "Aaaargh" if $spec{coerce}; + } elsif (my $coerce = $spec{coerce}) { + my $attr = perlstring($name); + my $tc = Moose::Meta::TypeConstraint->new( + constraint => sub { die "This is not going to work" }, + inlined => sub { 'my $res = $_[42]{'.$attr.'}; $_[42]{'.$attr.'} = 1; $res' }, + #coercion => Moose::Meta::TypeCoercion->new( + # type_coercion_map => [ + # 'Item', $coerce + # ] + #) + ); + $tc->coercion(Moose::Meta::TypeCoercion->new) + ->_compiled_type_coercion($coerce); + $spec{isa} = $tc; + $spec{coerce} = 1; } push @attrs, $meta->add_attribute($name => %spec); } @@ -89,7 +107,7 @@ sub inject_real_metaclass_for { ); } $meta->add_role(Class::MOP::class_of($_)) - for keys %{$Role::Tiny::APPLIED_TO{$name}}; + for do { no warnings 'once'; keys %{$Role::Tiny::APPLIED_TO{$name}} }; $DID_INJECT{$name} = 1; $meta; } diff --git a/xt/moose-accessor-isa.t b/xt/moose-accessor-isa.t index bf67db4..5254948 100644 --- a/xt/moose-accessor-isa.t +++ b/xt/moose-accessor-isa.t @@ -22,6 +22,23 @@ use Moo::HandleMoose; package Bar; use Moose; with 'FrewWithIsa'; + + package OffByOne; + use Moo::Role; + + has off_by_one => (is => 'rw', coerce => sub { $_[0] + 1 }); + + package Baz; + use Moo; + + with 'OffByOne'; + + package Quux; + use Moose; + + with 'OffByOne'; + + __PACKAGE__->meta->make_immutable; } lives_ok { @@ -36,4 +53,19 @@ dies_ok { Bar->new(frooh => 1, frew => 'goose'); } 'creation of invalid Bar validated by quoted sub'; +sub test_off_by_one { + my ($class, $type) = @_; + + my $obo = $class->new(off_by_one => 1); + + is($obo->off_by_one, 2, "Off by one (new) ($type)"); + + $obo->off_by_one(41); + + is($obo->off_by_one, 42, "Off by one (set) ($type)"); +} + +test_off_by_one('Baz', 'Moo'); +test_off_by_one('Quux', 'Moose'); + done_testing;