From: Stevan Little Date: Wed, 9 May 2007 23:10:10 +0000 (+0000) Subject: foo X-Git-Tag: 0_22~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d7611a4a61d371ea60527e4c30e2ae7740cf6441;p=gitmo%2FMoose.git foo --- diff --git a/Changes b/Changes index 3e5846e..219c2ee 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,11 @@ Revision history for Perl extension Moose +0.22 + * Moose::Meta::Method::Accessor + - coerce and lazy now work together correctly, thanks to + merlyn for finding this bug + - tests added for this + 0.21 Thursday, May 2nd, 2007 * Moose - added SUPER_SLOT and INNER_SLOT class hashes to support unimport diff --git a/MANIFEST b/MANIFEST index 2e06a03..a58e266 100644 --- a/MANIFEST +++ b/MANIFEST @@ -85,6 +85,7 @@ t/060_moose_for_meta.t t/070_more_attr_delegation.t t/071_misc_attribute_tests.t t/072_attr_dereference_test.t +t/073_misc_attribute_coerce_lazy.t t/100_subtype_quote_bug.t t/101_subtype_conflict_bug.t t/102_Moose_Object_error.t diff --git a/README b/README index 7d0d1c4..d7f96a7 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Moose version 0.21 +Moose version 0.22 =========================== See the individual module documentation for more information diff --git a/lib/Moose.pm b/lib/Moose.pm index d0ec0af..327b1c8 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -4,7 +4,7 @@ package Moose; use strict; use warnings; -our $VERSION = '0.21'; +our $VERSION = '0.22'; our $AUTHORITY = 'cpan:STEVAN'; use Scalar::Util 'blessed', 'reftype'; diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index a0d2469..491eed5 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -212,6 +212,7 @@ sub initialize_instance_slot { if (!defined $val && $self->has_default) { $val = $self->default($instance); } + if (defined $val) { if ($self->has_type_constraint) { my $type_constraint = $self->type_constraint; diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm index 6c99acc..d02ec24 100644 --- a/lib/Moose/Meta/Method/Accessor.pm +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -6,7 +6,7 @@ use warnings; use Carp 'confess'; -our $VERSION = '0.03'; +our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; use base 'Moose::Meta::Method', @@ -148,6 +148,9 @@ sub _inline_check_lazy { return 'unless (exists $_[0]->{$attr_name}) {' . ' if ($attr->has_default) {' . ' my $default = $attr->default($_[0]);' . + ($attr->should_coerce + ? '$default = $attr->type_constraint->coerce($default);' + : '') . ' (defined($type_constraint->($default)))' . ' || confess "Attribute (" . $attr->name . ") does not pass the type constraint ("' . ' . $attr->type_constraint->name . ") with " . (defined($default) ? "\'$default\'" : "undef")' . diff --git a/t/073_misc_attribute_coerce_lazy.t b/t/073_misc_attribute_coerce_lazy.t new file mode 100644 index 0000000..37e354d --- /dev/null +++ b/t/073_misc_attribute_coerce_lazy.t @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; +use Test::Exception; + +BEGIN { + use_ok('Moose'); +} + +{ + package HTTPHeader; + use Moose; + + has 'array' => (is => 'ro'); + has 'hash' => (is => 'ro'); +} + +{ + package Request; + use Moose; + use Moose::Util::TypeConstraints; + + subtype Header => + => as Object + => where { $_->isa('HTTPHeader') }; + + coerce Header + => from ArrayRef + => via { HTTPHeader->new(array => $_[0]) } + => from HashRef + => via { HTTPHeader->new(hash => $_[0]) }; + + has 'headers' => ( + is => 'rw', + isa => 'Header', + coerce => 1, + lazy => 1, + default => sub { [ 'content-type', 'text/html' ] } + ); +} + +my $r = Request->new; +isa_ok($r, 'Request'); + +lives_ok { + $r->headers; +} '... this coerces and passes the type constraint even with lazy'; + + +