Skip Menu |

This queue is for tickets about the MooseX-AlwaysCoerce CPAN distribution.

Report information
The Basics
Id: 58885
Status: resolved
Priority: 0/
Queue: MooseX-AlwaysCoerce

People
Owner: Nobody in particular
Requestors: mschwern [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.04
Fixed in: (no value)



Subject: Only coerce if the type has coercion.
Moose will blow up if you set coerce on a type that does not have coercion defined. This is highly annoying as it means I need to know far more than I care to know about the types I'm using. This also makes MooseX::AlwaysCoerce all or nothing (or I have to explicitly turn off coercion and we're back at square one). So it would be nice if MXAC first checked that the attribute's type can coerce before turning on coercion.
Subject: Re: [rt.cpan.org #58885] Only coerce if the type has coercion.
Date: Sun, 27 Jun 2010 19:04:14 -0400
To: bug-MooseX-AlwaysCoerce [...] rt.cpan.org
From: Rafael Kitover <rkitover [...] io.com>
That worked before, I'll take a look "Michael G Schwern via RT" <bug-MooseX-AlwaysCoerce@rt.cpan.org> wrote: Show quoted text
>Sun Jun 27 18:50:29 2010: Request 58885 was acted upon. >Transaction: Ticket created by MSCHWERN > Queue: MooseX-AlwaysCoerce > Subject: Only coerce if the type has coercion. > Broken in: 0.04 > Severity: Wishlist > Owner: Nobody > Requestors: mschwern@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=58885 > > > >Moose will blow up if you set coerce on a type that does not have >coercion defined. This is highly annoying as it means I need to know >far more than I care to know about the types I'm using. This also makes >MooseX::AlwaysCoerce all or nothing (or I have to explicitly turn off >coercion and we're back at square one). > >So it would be nice if MXAC first checked that the attribute's type can >coerce before turning on coercion.
-- Sent from my Android phone with K-9. Please excuse my brevity.
The attached patches fix this bug. From talking on #moose, the coerce attribute had a problem in that its reader should be "should_coerce" not "rw". It has to be lazy in case coerce is checked before isa. Also, most of #moose was disturbed by the MX::ClassAttribute tie in.
Subject: 0002-rt.cpan.org-58885-Only-coerce-if-the-accessor-s-type.patch
From 815f0e375f73fbb484f101dc9f5f9e0650c32de9 Mon Sep 17 00:00:00 2001 From: Michael G. Schwern <schwern@pobox.com> Date: Sun, 27 Jun 2010 16:50:39 -0700 Subject: [PATCH 2/2] [rt.cpan.org 58885] Only coerce if the accessor's type can coerce. Otherwise the Moose will get angry. --- lib/MooseX/AlwaysCoerce.pm | 14 ++++++++++++-- t/01-basic.t | 8 +++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/MooseX/AlwaysCoerce.pm b/lib/MooseX/AlwaysCoerce.pm index fbb1e1f..d89af63 100644 --- a/lib/MooseX/AlwaysCoerce.pm +++ b/lib/MooseX/AlwaysCoerce.pm @@ -54,18 +54,28 @@ Use C<< coerce => 0 >> to disable a coercion explicitly. use namespace::autoclean; use Moose::Role; - has coerce => (is => 'rw', default => 1); + has "coerce" => ( + lazy => 1, + reader => "should_coerce", + default => sub { + return 1 if shift->type_constraint->has_coercion; + return 0; + } + ); + package MooseX::AlwaysCoerce::Role::Meta::Class; use namespace::autoclean; use Moose::Role; + use Moose::Util::TypeConstraints; around add_class_attribute => sub { my $next = shift; my $self = shift; my ($what, %opts) = @_; - $opts{coerce} = 1 unless exists $opts{coerce}; + my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa}); + $opts{coerce} = 1 if !exists $opts{coerce} and $type->has_coercion; $self->$next($what, %opts); }; diff --git a/t/01-basic.t b/t/01-basic.t index 1f7f3b1..506d08d 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -2,7 +2,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; { package MyClass; @@ -10,9 +10,13 @@ use Test::More tests => 5; use MooseX::AlwaysCoerce; use Moose::Util::TypeConstraints; + subtype 'NoCoerce', as 'Some::Class'; + subtype 'MyType', as 'Int'; coerce 'MyType', from 'Str', via { length $_ }; + has no_coerce => (is => 'rw', isa => 'NoCoerce' ); + has foo => (is => 'rw', isa => 'MyType'); class_has bar => (is => 'rw', isa => 'MyType'); @@ -37,3 +41,5 @@ undef $@; eval { $instance->quux('mtfnpy') }; ok( $@, 'attribute coercion did not run with coerce => 0' ); + +ok eval { $instance->no_coerce( bless {}, "Some::Class" ); } || diag $@; -- 1.7.1
Subject: 0001-Better-diagnostics-when-the-evals-fail.patch
From d2f1a0f6ead86f8d8b6fa1de7c29bd5c7023b5a6 Mon Sep 17 00:00:00 2001 From: Michael G. Schwern <schwern@pobox.com> Date: Sun, 27 Jun 2010 16:50:16 -0700 Subject: [PATCH 1/2] Better diagnostics when the evals fail. --- t/01-basic.t | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/t/01-basic.t b/t/01-basic.t index b3643e5..1f7f3b1 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -25,10 +25,10 @@ use Test::More tests => 5; ok( (my $instance = MyClass->new), 'instance' ); eval { $instance->foo('bar') }; -ok( (!$@), 'attribute coercion ran' ); +is $@, "", 'attribute coercion ran'; eval { $instance->bar('baz') }; -ok( (!$@), 'class attribute coercion ran' ); +is $@, "", 'class attribute coercion ran'; eval { $instance->baz('quux') }; ok( $@, 'class attribute coercion did not run with coerce => 0' ); -- 1.7.1
Applied for 0.05, thank you for the patches!