aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/MooseX/AlwaysCoerce.pm36
1 files changed, 35 insertions, 1 deletions
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