From: Rafael Kitover Date: Mon, 15 Jun 2009 22:23:03 +0000 (-0700) Subject: first release X-Git-Tag: 0.04~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad1917d79f5490b78c921ce0e7ae708c300d97e4;p=gitmo%2FMooseX-AlwaysCoerce.git first release --- diff --git a/Changes b/Changes index 2bd3c03..9622bc3 100644 --- a/Changes +++ b/Changes @@ -1 +1,4 @@ Revision history for MooseX-AlwaysCoerce + +0.01 2009-06-15 22:18:34 + - releasing... diff --git a/Makefile.PL b/Makefile.PL index da68f02..0ee133f 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -8,6 +8,8 @@ license 'perl'; test_requires 'Test::More'; requires 'Moose'; +requires 'namespace::autoclean'; +requires 'MooseX::ClassAttribute'; auto_provides; auto_install; diff --git a/lib/MooseX/AlwaysCoerce.pm b/lib/MooseX/AlwaysCoerce.pm index 6ab536e..b8ad4f3 100644 --- a/lib/MooseX/AlwaysCoerce.pm +++ b/lib/MooseX/AlwaysCoerce.pm @@ -3,6 +3,15 @@ package MooseX::AlwaysCoerce; use strict; use warnings; +use namespace::autoclean; +use Moose (); +use Moose::Exporter; +use Carp; + +Moose::Exporter->setup_import_methods ( + with_caller => [ 'has', 'class_has' ] +); + =head1 NAME MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes @@ -20,10 +29,35 @@ our $VERSION = '0.01'; package MyClass; use Moose; + use MooseX::ClassAttribute; use MooseX::AlwaysCoerce; use MyTypeLib 'SomeType'; - has foo => (is => 'rw', isa => SomeType); # will be coerced + has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added + + # same, but you must load MooseX::ClassAttribute *BEFORE* + # MooseX::AlwaysCoerce + class_has bar => (is => 'rw', isa => SomeType); + +=head1 DESCRIPTION + +Have you ever spent an hour or more trying to figure out "WTF, why did my +coercion not run?" only to find out that you forgot C<< coerce => 1 >> ? + +Just load this module in your L class and C<< coerce => 1 >> will be +enabled for every attribute automatically. + +=cut + +sub has { + push @_, (coerce => 1); + goto &Moose::has; +} + +sub class_has { + push @_, (coerce => 1); + goto &MooseX::ClassAttribute::class_has; +} =head1 AUTHOR diff --git a/t/01-basic.t b/t/01-basic.t new file mode 100644 index 0000000..f056dd9 --- /dev/null +++ b/t/01-basic.t @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test::More tests => 3; + +{ + package MyClass; + use Moose; + use MooseX::ClassAttribute; + use MooseX::AlwaysCoerce; + use Moose::Util::TypeConstraints; + + subtype 'MyType', as 'Int'; + coerce 'MyType', from 'Str', via { length $_ }; + + has foo => (is => 'rw', isa => 'MyType'); + + class_has bar => (is => 'rw', isa => 'MyType'); +} + +ok( (my $instance = MyClass->new), 'instance' ); + +eval { $instance->foo('bar') }; +ok( (!$@), 'attribute coercion ran' ); + +eval { $instance->bar('baz') }; +ok( (!$@), 'class attribute coercion ran' );