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.16';
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 around should_coerce => sub {
61 my $current_val = $self->$orig(@_);
63 return $current_val if defined $current_val;
65 return 1 if $self->type_constraint && $self->type_constraint->has_coercion;
69 package MooseX::AlwaysCoerce::Role::Meta::Class;
70 use namespace::autoclean;
72 use Moose::Util::TypeConstraints;
74 around add_class_attribute => sub {
77 my ($what, %opts) = @_;
79 if (exists $opts{isa}) {
80 my $type = Moose::Util::TypeConstraints::find_or_parse_type_constraint($opts{isa});
81 $opts{coerce} = 1 if not exists $opts{coerce} and $type->has_coercion;
84 $self->$next($what, %opts);
88 my (undef, undef, $init_meta) = Moose::Exporter->build_import_methods(
90 install => [ qw(import unimport) ],
93 attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'],
94 class => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
98 (Moose->VERSION >= 1.9900
99 ? (applied_attribute => ['MooseX::AlwaysCoerce::Role::Meta::Attribute'])
101 role => ['MooseX::AlwaysCoerce::Role::Meta::Class'],
106 my ($class, %options) = @_;
107 my $for_class = $options{for_class};
109 MooseX::ClassAttribute->import({ into => $for_class });
111 # call generated method to do the rest of the work.
117 Rafael Kitover, C<< <rkitover at cpan.org> >>
121 Schwern: Michael G. Schwern <mschwern@cpan.org>
122 Ether: Karen Etheridge <ether@cpan.org>
126 Please report any bugs or feature requests to C<bug-moosex-alwayscoerce at rt.cpan.org>, or through
127 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-AlwaysCoerce>. I will be notified, and then you'll
128 automatically be notified of progress on your bug as I make changes.
132 You can find more information at:
136 =item * RT: CPAN's request tracker
138 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-AlwaysCoerce>
140 =item * AnnoCPAN: Annotated CPAN documentation
142 L<http://annocpan.org/dist/MooseX-AlwaysCoerce>
146 L<http://cpanratings.perl.org/d/MooseX-AlwaysCoerce>
150 L<http://search.cpan.org/dist/MooseX-AlwaysCoerce/>
154 =head1 ACKNOWLEDGEMENTS
156 My own stupidity, for inspiring me to write this module.
158 Dave Rolsky, for telling me how to do it the L<Moose> way.
160 =head1 COPYRIGHT & LICENSE
162 Copyright (c) 2009-2010 Rafael Kitover
164 This program is free software; you can redistribute it and/or modify it
165 under the same terms as Perl itself.
169 1; # End of MooseX::AlwaysCoerce
170 # vim:et sts=4 sw=4 tw=0: