aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changes3
-rw-r--r--Makefile.PL2
-rw-r--r--lib/MooseX/AlwaysCoerce.pm36
-rw-r--r--t/01-basic.t28
4 files changed, 68 insertions, 1 deletions
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<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' );