1 package MooseX::AlwaysCoerce;
6 use namespace::autoclean;
8 use MooseX::ClassAttribute ();
10 use Moose::Util::MetaRole;
13 Moose::Exporter->setup_import_methods;
17 MooseX::AlwaysCoerce - Automatically enable coercions for Moose attributes
25 our $VERSION = '0.04';
32 use MooseX::AlwaysCoerce;
33 use MyTypeLib 'SomeType';
35 has foo => (is => 'rw', isa => SomeType); # coerce => 1 automatically added
37 # same, MooseX::ClassAttribute is automatically applied
38 class_has bar => (is => 'rw', isa => SomeType);
42 Have you ever spent an hour or more trying to figure out "WTF, why did my
43 coercion not run?" only to find out that you forgot C<< coerce => 1 >> ?
45 Just load this module in your L<Moose> class and C<< coerce => 1 >> will be
46 enabled for every attribute and class attribute automatically.
48 Use C<< coerce => 0 >> to disable a coercion explicitly.
53 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
54 use namespace::autoclean;
57 has coerce => (is => 'rw', default => 1);
59 package MooseX::AlwaysCoerce::Role::Meta::Class;
60 use namespace::autoclean;
63 around add_class_attribute => sub {
66 my ($what, %opts) = @_;
68 $opts{coerce} = 1 unless exists $opts{coerce};
70 $self->$next($what, %opts);
77 my $for_class = $options{for_class};
79 MooseX::ClassAttribute->import({ into => $for_class });
81 Moose::Util::MetaRole::apply_metaclass_roles(
82 for_class => $for_class,
83 attribute_metaclass_roles =>
84 ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
86 ['MooseX::AlwaysCoerce::Role::Meta::Class'],
89 return $for_class->meta;
94 Rafael Kitover, C<< <rkitover at cpan.org> >>
98 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
99 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
100 automatically be notified of progress on your bug as I make changes.
104 You can find more information at:
108 =item * RT: CPAN's request tracker
110 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
112 =item * AnnoCPAN: Annotated CPAN documentation
114 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
118 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
122 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
126 =head1 ACKNOWLEDGEMENTS
128 My own stupidity, for inspiring me to write this module.
130 Dave Rolsky, for telling me how to do it the L<Moose> way.
132 =head1 COPYRIGHT & LICENSE
134 Copyright (c) 2009 Rafael Kitover
136 This program is free software; you can redistribute it and/or modify it
137 under the same terms as Perl itself.
141 1; # End of MooseX::AlwaysCoerce