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.02';
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.
51 package MooseX::AlwaysCoerce::Role::Meta::Attribute;
52 use namespace::autoclean;
55 has coerce => (is => 'rw', default => 1);
57 package MooseX::AlwaysCoerce::Role::Meta::Class;
58 use namespace::autoclean;
61 around add_class_attribute => sub {
64 $self->$next(@_, coerce => 1);
71 my $for_class = $options{for_class};
73 MooseX::ClassAttribute->import({ into => $for_class });
75 Moose::Util::MetaRole::apply_metaclass_roles(
76 for_class => $for_class,
77 attribute_metaclass_roles =>
78 ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
80 ['MooseX::AlwaysCoerce::Role::Meta::Class'],
83 return $for_class->meta;
88 Rafael Kitover, C<< <rkitover at cpan.org> >>
92 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
93 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
94 automatically be notified of progress on your bug as I make changes.
98 You can find more information at:
102 =item * RT: CPAN's request tracker
104 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
106 =item * AnnoCPAN: Annotated CPAN documentation
108 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
112 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
116 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
120 =head1 ACKNOWLEDGEMENTS
122 My own stupidity, for inspiring me to write this module.
124 Dave Rolsky, for telling me how to do it the L<Moose> way.
126 =head1 COPYRIGHT & LICENSE
128 Copyright (c) 2009 Rafael Kitover
130 This program is free software; you can redistribute it and/or modify it
131 under the same terms as Perl itself.
135 1; # End of MooseX::AlwaysCoerce