diff options
author | Rafael Kitover <rkitover@cpan.org> | 2009-06-15 15:23:03 -0700 |
---|---|---|
committer | Rafael Kitover <rkitover@cpan.org> | 2009-06-15 15:23:03 -0700 |
commit | ad1917d79f5490b78c921ce0e7ae708c300d97e4 (patch) | |
tree | a340a9fa5c76803e17d4b056977f941443f838e2 | |
parent | 7a603ffaf368e45a344d029e716ea36b10794e40 (diff) | |
download | mx-alwayscoerce-ad1917d79f5490b78c921ce0e7ae708c300d97e4.tar.gz mx-alwayscoerce-ad1917d79f5490b78c921ce0e7ae708c300d97e4.zip |
first release
-rw-r--r-- | Changes | 3 | ||||
-rw-r--r-- | Makefile.PL | 2 | ||||
-rw-r--r-- | lib/MooseX/AlwaysCoerce.pm | 36 | ||||
-rw-r--r-- | t/01-basic.t | 28 |
4 files changed, 68 insertions, 1 deletions
@@ -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<Moose> 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' ); |