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
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
--- /dev/null
+#!/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' );